Python Programming - Lambda Functions

Exercise : Lambda Functions - General Questions
  • Lambda Functions - General Questions
36.
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:
functools.reduce() successively applies the lambda function to accumulate values in an iterable.

37.
What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, numbers))
print(result)
[2, 4]
[1, 3, 5]
[1, 2, 3, 4, 5]
[1, 4]
Answer: Option
Explanation:
The filter() function with the lambda filters even numbers from the list.

38.
Which of the following built-in functions can be used to find the length of a list of strings using a lambda function?
len()
map()
reduce()
filter()
Answer: Option
Explanation:
The len() function can be used with a lambda function to find the length of a list of strings.

39.
What does the following lambda function accomplish?
lambda x: x.upper()
Converts a string to lowercase.
Converts a string to uppercase.
Reverses a string.
Trims leading and trailing whitespaces from a string.
Answer: Option
Explanation:
The lambda function lambda x: x.upper() converts a string to uppercase.

40.
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.