Python Programming - Lambda Functions

Exercise : Lambda Functions - General Questions
  • Lambda Functions - General Questions
46.
What is the purpose of the zip() function when used with a lambda function?
To combine two iterables into a single iterable of tuples.
To filter elements from an iterable based on the lambda function's condition.
To create a sorted list based on the lambda function's values.
To apply the lambda function to each element of an iterable.
Answer: Option
Explanation:
zip() combines two iterables into pairs, allowing the lambda function to operate on corresponding elements.

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

48.
What does the following lambda function accomplish?
lambda x: len(x) if isinstance(x, str) else None
Returns the length of a string and None for non-string values.
Filters out strings and returns None for non-string values.
Doubles the length of a string and returns None for non-string values.
Returns the square of the length of a string and None for non-string values.
Answer: Option
Explanation:
The lambda function returns the length of a string and None for non-string values.

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

50.
What is the purpose of the functools.partial() function when used with a lambda function?
To create a partial application of a lambda function.
To create a filter object based on the lambda function's condition.
To apply the lambda function to each element of an iterable.
To sort the elements of an iterable using the lambda function.
Answer: Option
Explanation:
functools.partial() is used to create a partial application of a function, including lambda functions, by fixing certain arguments.