What happens if no except block matches the raised exception?
except
The program terminates with an unhandled exception error.
The program continues executing from the next line after the try...except block.
try...except
The finally block is executed, and then the program continues.
finally
The program enters an infinite loop.
What is the purpose of the OrderedDict class in Python?
OrderedDict
It provides a dictionary that remembers the order in which keys were inserted.
It allows you to create dictionaries with keys as immutable data types only.
It optimizes dictionary lookups for faster retrieval of values.
It prevents duplicate keys from being added to a dictionary.
Which regular expression will match any string that starts with 'a' and ends with 'z'?
'a.+z'
'^a.*z$'
'a.*z'
'^a.+z$'
What is the output of the following Python code?
with open('data.txt', 'w') as file: file.write('Hello, world!') print(file.read())
The code will raise a ValueError.
Hello, world!
The code will raise an IOError.
The code will print nothing.
What is a key difference between OrderedDict and a regular dictionary in Python?
OrderedDict preserves the order of key insertion, while regular dictionaries do not guarantee order.
OrderedDict supports list comprehensions for creating dictionaries, while regular dictionaries do not.
OrderedDict is mutable, while regular dictionaries are immutable.
OrderedDict allows duplicate keys, while regular dictionaries do not.
How can you get the current date and time using the 'datetime' module?
datetime.currentTime()
datetime.datetime.now()
datetime.today()
datetime.now
What does the following code snippet print?
def outer_func(x): def inner_func(y): return x + y return inner_func my_func = outer_func(5) print(my_func(3))
15
8
Error
5
What is the primary advantage of using the with statement for file operations in Python?
with
It provides better performance for large files.
It automatically closes the file, even if exceptions occur.
It improves code readability.
All of the above.
What does the 're.findall()' function return?
A list of all non-overlapping matches of the pattern in the string.
The first match of the pattern in the string.
None of the above.
A boolean value indicating whether the pattern is present in the string.
What is a key characteristic of a closure in Python?
It allows a function to be defined inside a class.
It forces a function to return a value of a specific data type.
It allows a nested function to 'remember' and access variables from its enclosing function's scope even after the outer function has finished executing.
It prevents a function from accessing global variables.