What will the following code snippet print?
def my_decorator(func): def wrapper(): print("Before function call") func() print("After function call") return wrapper @my_decorator def say_hello(): print("Hello!") say_hello()
Hello!
Error
Before function call Hello! After function call
Before function call After function call
Which of the following is a valid way to define a function with both default and keyword arguments in Python?
def my_function(a, b=2, **kwargs): # Function body
def my_function(a=1, *args, b, **kwargs): # Function body
def my_function(a=1, b, c=3): # Function body
def my_function(a, b=2, **kwargs, c=3): # Function body
Which code snippet correctly creates a dictionary where keys are numbers from 1 to 5 and values are their squares using a dictionary comprehension?
{num**2: num for num in range(1, 6)}
(num: num**2 for num in range(1, 6))
[num: num**2 for num in range(1, 6)]
{num: num**2 for num in range(1, 6)}
What is the output of the following code snippet?
data = [[1, 2, 3], [4, 5, 6]] result = [x * 2 for x in data[1]] print(result)
[4, 5, 6]
[8, 10, 12]
[2, 4, 6, 8, 10, 12]
[1, 2, 3, 4, 5, 6]
What is the difference between *args and **kwargs in Python?
*args passes a list of arguments, while **kwargs passes a dictionary of arguments.
*args is used for passing arguments by value, while **kwargs is used for passing arguments by reference.
*args passes a variable number of positional arguments, while **kwargs passes a variable number of keyword arguments.
There is no difference; both *args and **kwargs can be used interchangeably.
What does the csv.reader() function in Python return?
csv.reader()
A string containing the entire CSV file content.
A list of lists, representing rows and cells.
A list of dictionaries.
A list of strings.
In Python, what is the difference between a class variable and an instance variable?
There is no difference; they are interchangeable terms.
Class variables are shared by all instances of a class, while instance variables are unique to each instance.
Class variables can only store integers, while instance variables can store any data type.
Class variables are declared inside a method, while instance variables are declared outside methods.
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
What is the most efficient way to concatenate a large number of strings in Python?
Using the '%s' operator.
It depends on the size of the strings.
Using the '+' operator.
Using the ''.join() method.
How can you handle potential errors when opening a file in Python?
Using an if-else statement.
Using a while loop.
Using a try-except block.
Using a for loop.