Python Programming - Lambda Functions

Exercise : Lambda Functions - General Questions
  • Lambda Functions - General Questions
21.
What happens if a lambda function has more parameters than arguments provided during its invocation?
It raises a SyntaxError.
The extra parameters are ignored.
It raises a TypeError.
The lambda function automatically assigns default values to the extra parameters.
Answer: Option
Explanation:
If a lambda function is defined with more parameters than provided during invocation, a TypeError is raised.

22.
What is the primary advantage of using a lambda function over a regular function?
Lambda functions support multiple expressions and statements.
Lambda functions can be recursive.
Lambda functions are more memory-efficient.
Lambda functions are concise and can be written in a single line.
Answer: Option
Explanation:
Lambda functions are often used for short, simple tasks due to their concise syntax, allowing them to be written in a single line.

23.
How does the filter() function work when used with a lambda function?
It creates a filter object based on the lambda function's condition.
It sorts the elements of an iterable using the lambda function.
It applies the lambda function to each element of an iterable.
It accumulates values from an iterable using the lambda function.
Answer: Option
Explanation:
The filter() function is used to create a filter object that includes only the elements from an iterable for which the lambda function returns True.

24.
How can a lambda function be used to define a key for sorting a list of tuples by the second element?
lambda x: x[1]
lambda x: x[0]
lambda x: x[2]
lambda x: x[-1]
Answer: Option
Explanation:
The lambda function lambda x: x[1] extracts the second element of a tuple, which is suitable for sorting based on the second element.

25.
What is the purpose of the functools.reduce() function when used with a lambda function?
To apply the lambda function to each element of an iterable.
To create a filter object based on the lambda function's condition.
To accumulate values from an iterable using the lambda function.
To sort the elements of an iterable using the lambda function.
Answer: Option
Explanation:
The functools.reduce() function is used to successively apply the lambda function to the elements of an iterable, accumulating values.