Exercise: Python

Questions for: Functions

What does the __closure__ attribute indicate?
A:
The number of closures in a function.
B:
The names of all closed-over variables in a function.
C:
The values of all closed-over variables in a function.
D:
The tuple of cells that contain the bindings of closed-over variables in a function.
Answer: D
The __closure__ attribute in Python indicates the tuple of cells that contain the bindings of closed-over variables in a function.
What is the purpose of the locals function?
A:
To create a local variable.
B:
To access the local namespace.
C:
To convert a dictionary to a list.
D:
To delete a local variable.
Answer: B
The locals function in Python is used to access the local namespace, which contains local variables.
How can you pass a function as an argument to another function?
A:
By using the eval function.
B:
By converting the function to a string.
C:
By using the function name without parentheses.
D:
By using the function name with parentheses.
Answer: C
In Python, you can pass a function as an argument to another function by using the function name without parentheses.
What is the purpose of the __doc__ attribute?
A:
To access the source code of a function.
B:
To check if a function is a decorator.
C:
To access the docstring of a function.
D:
To define default values for function parameters.
Answer: C
The __doc__ attribute is used to access the docstring (documentation string) of a function.
What is the purpose of the enumerate function?
A:
To generate a sequence of numbers.
B:
To iterate over a sequence and keep track of the index.
C:
To convert a list to a set.
D:
To reverse the order of elements in a list.
Answer: B
The enumerate function is used to iterate over a sequence and keep track of the index of the current item.
Ad Slot (Above Pagination)
Quiz