Exercise: Python

Questions for: Functions

What is the purpose of the **kwargs in a function definition?
A:
To indicate a keyword argument.
B:
To specify default values for arguments.
C:
To allow the function to accept a variable number of keyword arguments.
D:
To define a dictionary of arguments.
Answer: C
The **kwargs in a function definition allows the function to accept a variable number of keyword arguments, which are passed as a dictionary.
What is the purpose of the functools.wraps decorator?
A:
To create wrapper functions.
B:
To wrap an iterable.
C:
To preserve the original metadata of a decorated function.
D:
To wrap a class method.
Answer: C
The functools.wraps decorator in Python is used to preserve the original metadata of a decorated function, such as its name and docstring.
What is the purpose of the os.path.join() function?
A:
To join two strings together.
B:
To concatenate two lists.
C:
To join path components intelligently.
D:
To join elements in a tuple.
Answer: C
The os.path.join() function in Python is used to join path components intelligently, taking care of the correct path separator.
What does the __call__ method do in a Python class?
A:
It calls the constructor of the class.
B:
It calls the destructor of the class.
C:
It allows an instance of the class to be called as a function.
D:
It checks if an instance is callable.
Answer: C
The __call__ method in a Python class allows an instance of the class to be called as if it were a function.
What is the purpose of the functools.lru_cache decorator?
A:
To create a local variable with cache.
B:
To create a global variable with cache.
C:
To cache the results of a function, saving time for repeated calls with the same arguments.
D:
To limit the number of recursive calls in a function.
Answer: C
The functools.lru_cache decorator in Python is used to cache the results of a function, reducing the computation time for repeated calls with the same arguments.
Ad Slot (Above Pagination)
Quiz