What is a closure in Python?
A function that takes another function as an argument.
A function that returns another function.
A function that is defined inside another function and has access to the outer function's variables.
A function that can be accessed from anywhere in the program.
How can you access the constant 'pi' from the 'math' module after importing it?
math.pi
math->pi
math(pi)
pi
How do you create an instance of a class named 'Dog' in Python?
my_dog = Dog()```
Dog = new()```
Dog.new()```
create Dog()```
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: num**2 for num in range(1, 6))
{num: num**2 for num in range(1, 6)}
{num**2: num for num in range(1, 6)}
[num: num**2 for num in range(1, 6)]
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()
Error
Hello!
Before function call After function call
Before function call Hello! After function call
Which regular expression will match any string that starts with 'a' and ends with 'z'?
'^a.*z$'
'a.*z'
'^a.+z$'
'a.+z'
What does inheritance allow in object-oriented programming?
Restricting access to certain data and methods within a class.
Creating objects of one class inside another class.
Defining methods with the same name but different implementations in different classes.
Creating new classes that inherit properties and methods from existing classes.
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]
[2, 4, 6, 8, 10, 12]
[1, 2, 3, 4, 5, 6]
[8, 10, 12]
How can you create a dictionary where the keys are numbers from 1 to 5 and the values are their squares using a dictionary comprehension?
{i: i**2 for i in range(1, 6)}
{i**2: i for i in range(1, 5)}
[i: i**2 for i in range(1, 6)]
(i: i**2 for i in range(1, 6))
What is the purpose of using a default argument in a Python function?
To create a function that can accept any number of arguments.
To make the function return a default value if no arguments are provided.
To specify a value for an argument if it is not passed in the function call.
To define a variable that can only be accessed within the function.