What is a closure in Python?
A function that is defined inside another function and has access to the outer function's variables.
A function that returns another function.
A function that takes another function as an argument.
A function that can be accessed from anywhere in the program.
What is the purpose of the 'r' prefix in a regular expression pattern?
It makes the pattern case-insensitive.
It treats the pattern as a raw string, ignoring escape sequences.
It enables multiline matching.
It performs a greedy match.
In a function definition, what is the order in which you should define different types of arguments?
keyword arguments, *args, positional arguments, **kwargs
positional arguments, keyword arguments, *args, **kwargs
**kwargs, keyword arguments, *args, positional arguments
*args, positional arguments, **kwargs, keyword arguments
What is a key difference between instance methods and class methods in Python?
Instance methods are defined using the 'classmethod' decorator, while class methods use the 'staticmethod' decorator.
There is no practical difference; both can be used interchangeably.
Instance methods operate on specific instances of a class, while class methods operate on the class itself.
Class methods can access instance variables, while instance methods cannot access class variables.
What does the csv.reader() function in Python return?
csv.reader()
A list of dictionaries.
A list of strings.
A list of lists, representing rows and cells.
A string containing the entire CSV file content.
Which of the following is a valid way to define a function with both default and keyword arguments in Python?
def my_function(a=1, b, c=3): # Function body
def my_function(a=1, *args, b, **kwargs): # Function body
def my_function(a, b=2, **kwargs): # Function body
def my_function(a, b=2, **kwargs, c=3): # Function body
What happens if no except block matches the raised exception?
except
The program enters an infinite loop.
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
How can you handle potential errors when opening a file in Python?
Using a try-except block.
Using a for loop.
Using an if-else statement.
Using a while loop.
What does the 're.findall()' function return?
The first match of the pattern in the string.
A boolean value indicating whether the pattern is present in the string.
None of the above.
A list of all non-overlapping matches of the pattern in the string.
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.