Python Programming - Lambda Functions

Exercise : Lambda Functions - General Questions
  • Lambda Functions - General Questions
26.
When is it appropriate to use a lambda function?
For complex tasks requiring multiple expressions.
When defining a function with a detailed docstring.
For short, one-time tasks where a full function definition is not necessary.
When implementing class methods.
Answer: Option
Explanation:
Lambda functions are suitable for short-term, specific tasks where a full function definition is not needed, providing a concise way to express functionality.

27.
What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
print(squared)
[1, 2, 3, 4, 5]
[1, 4, 9, 16, 25]
[2, 4, 6, 8, 10]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Answer: Option
Explanation:
The map() function applies the lambda function to each element of the list, squaring each number.

28.
What does the following lambda function do?
lambda x, y: x if x > y else y
Squares the input.
Returns the sum of x and y.
Returns the larger of the two values.
Multiplies x and y.
Answer: Option
Explanation:
The lambda function returns the larger value between x and y using a conditional expression.

29.
In Python, what is the purpose of the sorted() function when used with a lambda function?
To apply the lambda function to each element of an iterable.
To create a sorted list based on the lambda function's values.
To filter elements from an iterable based on the lambda function's condition.
To map the lambda function to each element of an iterable.
Answer: Option
Explanation:
The sorted() function is used to create a sorted list from the elements of an iterable, with sorting criteria defined by the lambda function.

30.
Which built-in function can be used with a lambda function to find the maximum value in an iterable?
min()
max()
sum()
filter()
Answer: Option
Explanation:
The max() function can be used with a lambda function to find the maximum value in an iterable based on the lambda function's criteria.