Python Programming - Lambda Functions

Exercise : Lambda Functions - General Questions
  • Lambda Functions - General Questions
6.
What is a disadvantage of using lambda functions?
They cannot be used in higher-order functions
They can only contain a single expression
They are less efficient than regular functions
They cannot be assigned to variables
Answer: Option
Explanation:
Lambda functions are limited to a single expression, which may be a disadvantage when more complex logic or multiple statements are required.

7.
What is the main difference between map() and filter() functions when used with lambda functions?
map() applies the lambda function to each element, filter() creates a filter based on a condition
map() creates a filter, filter() applies the lambda function to each element
Both functions create filters based on conditions
Both functions apply the lambda function to each element
Answer: Option
Explanation:
map() applies the lambda function to each element of an iterable, while filter() creates a filter object based on a condition specified by the lambda function.

8.
What is the purpose of the reduce() function when used with lambda functions?
To create a filter based on a condition
To apply the lambda function to each element of an iterable
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 reduce() function is used to accumulate values from an iterable using the lambda function. It successively applies the lambda function to the elements of the iterable.

9.
In Python, can a lambda function have more than one line of code?
Yes, as long as each line is indented
No, lambda functions are limited to a single line
Yes, but only if the lines are separated by commas
No, lambda functions can only contain a single expression
Answer: Option
Explanation:
Lambda functions in Python are restricted to a single expression and cannot span multiple lines of code.

10.
Which of the following is a valid use case for lambda functions?
Defining complex algorithms with multiple statements
Creating short, throwaway functions for a specific task
Implementing class methods in Python
Serving as a replacement for regular functions in all scenarios
Answer: Option
Explanation:
Lambda functions are suitable for short-term, specific tasks where a full function definition is not necessary. They are often used for quick, one-time operations.