Python Programming - Functions

Exercise : Functions - General Questions
  • Functions - General Questions
56.
How can you create a generator function?
By using the generator keyword.
By using the yield keyword in a function.
By using the iter keyword.
By using the return keyword in a function.
Answer: Option
Explanation:
A generator function in Python is created by using the yield keyword to produce a series of values over time.

57.
What is the purpose of the functools.reduce function?
To apply a function cumulatively to the items of an iterable.
To reduce the size of a function.
To remove duplicates from an iterable.
To concatenate two iterables.
Answer: Option
Explanation:
The functools.reduce function in Python is used to apply a function cumulatively to the items of an iterable, reducing the iterable to a single cumulative result.

58.
What is the purpose of the getattr function?
To get the attribute of an object.
To get the type of an object.
To get the value of a variable.
To get the length of an iterable.
Answer: Option
Explanation:
The getattr function in Python is used to get the value of a named attribute of an object.

59.
What is the purpose of the functools.partialmethod?
To create partial functions with default arguments.
To create partial methods with default arguments.
To create class methods with default arguments.
To create static methods with default arguments.
Answer: Option
Explanation:
The functools.partialmethod in Python is used to create partial methods with default arguments, similar to functools.partial for functions.

60.
How does the locals function differ from globals?
locals returns the local namespace, while globals returns the global namespace.
locals returns the global namespace, while globals returns the local namespace.
Both return the same namespace.
Neither locals nor globals return any namespace.
Answer: Option
Explanation:
The locals function returns the local namespace, while globals returns the global namespace.