Python Programming - Lambda Functions

Exercise : Lambda Functions - General Questions
  • Lambda Functions - General Questions
41.
When using the sorted() function with a lambda function to sort a list of strings, how can the sorting be case-insensitive?
Use the case_insensitive=True parameter.
Use the lower() method inside the lambda function.
Use the sorted(case_insensitive=True) syntax.
It is not possible to achieve case-insensitive sorting with lambda functions.
Answer: Option
Explanation:
Using lower() inside the lambda function converts strings to lowercase, achieving case-insensitive sorting.

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

43.
What does the following lambda function do?
lambda x: x if x % 2 == 0 else None
Doubles even numbers and returns None for odd numbers.
Filters out even numbers and returns None for odd numbers.
Doubles even numbers and returns the original value for odd numbers.
Returns the square of even numbers and None for odd numbers.
Answer: Option
Explanation:
The lambda function filters out even numbers by returning None for them.

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

45.
What is the output of the following code?
items = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 3, items))
[1, 2, 3, 4, 5]
[3, 6, 9, 12, 15]
[1, 4, 9, 16, 25]
[1, 2, 3]
Answer: Option
Explanation:
The map() function applies the lambda function to multiply each item by 3.