# Python Lambda MCQ Questions and Answers

A lambda function in Python is a concise and anonymous function defined using the lambda keyword. It can take any number of arguments but must consist of a single expression. The expression is evaluated and returned as the result when the lambda function is called.

Lambda functions are often used for simple, one-off operations and are commonly employed with functions like map(), filter(), and sorted(). They are a handy way to create small, inline functions without the need for a formal def statement, making code more readable and concise. Here we present 20 multiple-choice questions to test your knowledge of Python Lambda. Each MCQ has the correct answer with an explanation.

## 1. What is a lambda function in Python?

a) A built-in function
b) An anonymous function defined with the lambda keyword
c) A special type of recursive function
d) A function that can only be used once

b) An anonymous function defined with the lambda keyword

### Explanation:

A lambda function in Python is a small anonymous function defined with the lambda keyword.

## 2. What is the correct syntax for a lambda function that adds two numbers, a and b?

a) lambda a, b: a + b
b) lambda (a, b): a + b
c) function(a, b): return a + b
d) (lambda a, b: a + b)

a) lambda a, b: a + b

### Explanation:

The correct syntax for a lambda function is 'lambda' followed by the parameters, a colon, and the expression.

## 3. How do you call a lambda function that multiplies two numbers?

a) (lambda a, b: a * b)(5, 3)
b) lambda a, b: a * b(5, 3)
c) call(lambda a, b: a * b, 5, 3)
d) lambda(5, 3, a * b)

a) (lambda a, b: a * b)(5, 3)

### Explanation:

A lambda function is called by enclosing it and its arguments in parentheses.

## 4. Which of the following is true about lambda functions?

a) They can contain multiple expressions
b) They can only have one parameter
c) They return the result of the expression automatically
d) They must contain a return statement

c) They return the result of the expression automatically

### Explanation:

Lambda functions return the result of the expression without needing a return statement.

## 5. How do you use a lambda function with the map() function in Python?

a) map(lambda x: x * 2, [1, 2, 3])
b) lambda x: x * 2, map([1, 2, 3])
c) map([1, 2, 3], lambda x: x * 2)
d) lambda map(x: x * 2, [1, 2, 3])

a) map(lambda x: x * 2, [1, 2, 3])

### Explanation:

The map() function applies the lambda function to each item of the iterable.

## 6. What does this lambda function do? lambda x: x > 10

b) Multiplies x by 10
c) Checks if x is greater than 10
d) Reduces x by 10

c) Checks if x is greater than 10

### Explanation:

This lambda function returns True if x is greater than 10, else False.

## 7. How do you use a lambda function as a key for sorting a list of tuples by the second element?

a) sorted(my_list, key=lambda x: x[1])
b) lambda x: x[1], sorted(my_list)
c) sorted(my_list, lambda x: x[1])
d) sort(my_list, key=lambda x: x[1])

a) sorted(my_list, key=lambda x: x[1])

### Explanation:

The key parameter in the sorted() function can be a lambda function to customize sorting.

## 8. Can lambda functions capture variables from the enclosing scope?

a) Yes
b) No
c) Only global variables
d) Only if passed as parameters

a) Yes

### Explanation:

Lambda functions can capture variables from the enclosing scope.

## 9. How would you filter out all negative numbers from a list using a lambda function?

a) filter(lambda x: x > 0, my_list)
b) lambda x: x > 0, filter(my_list)
c) filter(my_list, lambda x: x > 0)
d) lambda filter(x: x > 0, my_list)

a) filter(lambda x: x > 0, my_list)

### Explanation:

The filter() function can use a lambda function to filter out items based on a condition.

## 10. What is the limitation of a lambda function in Python?

a) It cannot return values
b) It can only have one expression
c) It can only be used once
d) It cannot be stored in variables

b) It can only have one expression

### Explanation:

Lambda functions in Python are limited to a single expression.

## 11. How is a lambda function typically defined?

a) Using the keyword "define"
b) Using the keyword "function"
c) Using the keyword "lambda"
d) Using the keyword "anonymous"

c) Using the keyword "lambda"

### Explanation:

Lambda functions are defined using the "lambda" keyword.

## 12. How many arguments can a lambda function take?

a) Only one argument
b) Exactly two arguments
c) Any number of arguments
d) Only keyword arguments

c) Any number of arguments

### Explanation:

Lambda functions can take any number of arguments.

## 13. What is the purpose of lambda functions in Python?

a) To define complex functions with multiple expressions
b) To create anonymous functions for simple operations
c) To define functions with default arguments
d) To create global functions

b) To create anonymous functions for simple operations

### Explanation:

Lambda functions are commonly used for short, simple operations.

## 14. Which built-in Python functions are often used with lambda functions?

a) max() and min()
b) sum() and average()
c) map() and filter()
d) sort() and reverse()

c) map() and filter()

### Explanation:

Lambda functions are frequently used with the map() and filter() functions.

## 15. What does a lambda function return?

a) Multiple values
b) None
c) A single value
d) A list of values

c) A single value

### Explanation:

Lambda functions return the result of a single expression.

## 16. Can lambda functions contain multiple expressions?

a) Yes, they can contain as many expressions as needed.
b) No, lambda functions can only contain a single expression.
c) Only if the expressions are enclosed in curly braces.
d) Only if the expressions are separated by semicolons.

b) No, lambda functions can only contain a single expression.

### Explanation:

Lambda functions are limited to a single expression.

## 17. How are lambda functions useful in reducing code verbosity?

a) By allowing for long and descriptive function names
b) By replacing the need for any function definitions
c) By eliminating the need for parentheses in function calls
d) By providing a concise way to define short functions inline

d) By providing a concise way to define short functions inline

### Explanation:

Lambda functions are useful for defining short functions without a full function definition.

## 18. Can lambda functions replace all uses of regular named functions (defined with "def")?

a) Yes, lambda functions can completely replace named functions.
b) No, lambda functions are only suitable for specific use cases.
c) Only if lambda functions are defined with the "def" keyword.
d) Yes, but only if they have a unique name.

b) No, lambda functions are only suitable for specific use cases.

### Explanation:

Lambda functions are not a complete replacement for named functions and are best suited for specific situations.

## 19. Which of the following is a valid use of a lambda function?

a) Defining a complex sorting algorithm
b) Creating a function with multiple expressions
c) Writing a function with a docstring
d) Passing a simple operation as an argument to another function

d) Passing a simple operation as an argument to another function

### Explanation:

Lambda functions are often used to pass simple operations as arguments to other functions.

## 20. What is the primary advantage of using lambda functions in Python?

a) They allow for complex logic and control flow.
b) They are easy to debug and test.
c) They provide a concise and readable way to define short functions.
d) They automatically handle exceptions and errors.

c) They provide a concise and readable way to define short functions.

### Explanation:

Lambda functions offer a concise way to define short functions, improving code readability.