Big Data

# 30+ MCQs on Python Lambda Functions

Welcome to the Python Lambda Function MCQs! Lambda functions, also known as anonymous functions, are concise and powerful tools in Python for creating small, inline functions. They are often used in scenarios where a small function is needed for a short period of time and a full function definition would be cumbersome. These questions will test your understanding of lambda functions in Python, including their syntax, usage, benefits, and limitations. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s explore the world of Python lambda functions together!

## 30+ MCQs on Python Lambda Function

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

a) A built-in function for performing mathematical calculations

b) A function that is defined with the `def` keyword

c) An anonymous function defined using the `lambda` keyword

d) A function that can only be used with strings

Explanation: Lambda functions in Python are anonymous functions defined with the `lambda` keyword.

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

a) Lambda functions can have multiple expressions

b) Lambda functions can have default arguments

c) Lambda functions cannot have return statements

d) Lambda functions can be named and reused like regular functions

Explanation: Lambda functions can have default arguments, allowing flexibility in their usage.

#### Q3. What is the syntax for a lambda function in Python?

a) `lambda arg1, arg2: expression`

b) `def lambda(arg1, arg2): expression`

c) `lambda function(arg1, arg2): expression`

d) `function(arg1, arg2): expression`

Explanation: The syntax for a lambda function is `lambda arguments: expression`.

#### Q4. When are lambda functions typically used in Python?

a) To define large, complex functions

b) To create anonymous functions for simple tasks

c) To replace built-in functions like `print()` and `input()`

d) To handle exceptions in code

Explanation: Lambda functions are commonly used for creating small, anonymous functions for simple tasks.

#### Q5. Which of the following is a valid use case for a lambda function?

a) Sorting a list of tuples by the second element

b) Defining a class method

c) Handling file I/O operations

d) Performing matrix multiplication

Explanation: Lambda functions are often used for sorting, especially when the sorting key is based on a specific element in a tuple or object.

#### Q6. What is the output of the following code?

``````result = (lambda x: x * 2)(5)
print(result)``````

a) 5

b) 10

c) 25

d) 15

Explanation: The lambda function `(lambda x: x * 2)` multiplies the input `5` by `2`, resulting in `10`.

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

a) They cannot have multiple parameters

b) They are always named

c) They can be used to create recursive functions

d) They can contain multiple lines of code

Explanation: Lambda functions are limited to a single expression, so they typically have a single parameter.

#### Q8. What is the purpose of using lambda functions with `map()` and `filter()` functions in Python?

a) To apply the lambda function to all elements of an iterable

b) To remove all elements from an iterable

c) To sort the elements of an iterable

d) To create a new iterable with selected elements

Explanation: Lambda functions are often used with `map()` and `filter()` to apply a function to all elements of an iterable and filter elements based on a condition, respectively.

#### Q9. What will be the output of the following code?

``````nums = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * x, nums))
print(result)``````

a) [1, 2, 3, 4, 5]

b) [1, 4, 9, 16, 25]

c) [2, 4, 6, 8, 10]

d) [0, 1, 2, 3, 4]

Explanation: The `map()` function applies the lambda function to each element in `nums`, resulting in a new list with squared values.

#### Q10. What is the output of the following code?

``````nums = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, nums))
print(result)``````

a) [1, 3, 5]

b) [2, 4]

c) [1, 2, 3, 4, 5]

d) []

Explanation: The `filter()` function creates a new list with elements that satisfy the condition (even numbers) given by the lambda function.

#### Q11. What will the following code snippet output?

``````f = lambda x, y: x + y
result = f(2, 3)
print(result)``````

a) 2

b) 3

c) 5

d) 6

Explanation: The lambda function `f` adds its two arguments `x` and `y`, so `f(2, 3)` results in `2 + 3 = 5`.

#### Q12. Which of the following is a correct lambda function to calculate the square of a number?

a) `lambda x: x * x`

b) `lambda x: x ^ 2`

c) `lambda x: x ** 2`

d) `lambda x: x + x`

Explanation: The lambda function `lambda x: x ** 2` calculates the square of a number `x`.

#### Q13. What does the following lambda function do?

``````f = lambda x: x if x % 2 == 0 else None
result = f(5)
print(result)``````

a) Returns the number if it is even, otherwise None

b) Returns None if the number is even, otherwise the number

c) Returns the square of the number if it is even, otherwise None

d) Returns the number if it is odd, otherwise None

Explanation: The lambda function `lambda x: x if x % 2 == 0 else None` returns None if the input number `x` is even, otherwise it returns the number itself.

#### Q14. What will be the output of the following code?

``````greet = lambda: "Hello, World!"
result = greet()
print(result)``````

a) “Hello, World!”

b) “Hello!”

c) None

d) Error: lambda functions require arguments

Explanation: The lambda function `greet` has no arguments and returns the string “Hello, World!”, which is then printed.

#### Q15. What is the output of the following code?

``````func_list = [lambda x: x + 2, lambda x: x * 2, lambda x: x ** 2]
results = [func(5) for func in func_list]
print(results)``````

a) [7, 10, 25]

b) [10, 25, 7]

c) [7, 10, 5]

d) [10, 5, 25]

Explanation: Each lambda function in `func_list` is applied to the value `5`, resulting in `[5 + 2, 5 * 2, 5 ** 2]`.

#### Q16. Which of the following is a correct use of a lambda function with multiple arguments?

a) `lambda x, y: x * y`

b) `lambda (x, y): x * y`

c) `lambda x y: x * y`

d) `lambda x: x * y`

Explanation: The correct syntax for a lambda function with multiple arguments is `lambda x, y: x * y`.

#### Q17. What does the following lambda function do?

``````square_if_positive = lambda x: x ** 2 if x > 0 else None
result = square_if_positive(-3)
print(result)``````

a) Returns the square of a positive number, otherwise None

b) Returns None if the number is positive, otherwise the square of the number

c) Returns the square of the number if it is positive, otherwise None

d) Returns the number if it is positive, otherwise its square

Explanation: The lambda function `square_if_positive` returns the square of the number if it is positive, otherwise it returns None.

#### Q18. What is the output of the following code?

``````nums = [1, 2, 3, 4, 5]
result = list(map(lambda x: x % 2 == 0, nums))
print(result)``````

a) [False, True, False, True, False]

b) [1, 0, 1, 0, 1]

c) [True, False, True, False, True]

d) [0, 1, 0, 1, 0]

Explanation: The `map()` function applies the lambda function to each element in `nums`, resulting in a list of Boolean values indicating whether each element is even.

#### Q19. Which of the following is a valid lambda function to check if a number is negative?

a) `lambda x: x < 0`

b) `lambda x: x > 0`

c) `lambda x: x == 0`

d) `lambda x: x != 0`

Explanation: The lambda function `lambda x: x < 0` checks if a number is negative.

#### Q20. What will the following code snippet output?

``````f = lambda x: "even" if x % 2 == 0 else "odd"
result = f(7)
print(result)``````

a) “even”

b) “odd”

c) 7

d) None

Explanation: The lambda function `f` checks if a number is even or odd and returns the respective string.

#### Q21. What does the following lambda function do?

``````double_or_square = lambda x: x * 2 if x % 2 == 0 else x ** 2
result = double_or_square(3)
print(result)``````

a) Doubles the number if even, squares it if odd

b) Doubles the number if odd, squares it if even

c) Squares the number if even, doubles it if odd

d) Doubles the number if positive, squares it if negative

Explanation: The lambda function `double_or_square` doubles the number if it’s odd and squares it if it’s even.

#### Q22. What is the output of the following code?

``````students = ['Alice', 'Bob', 'Charlie', 'David']
sorted_students = sorted(students, key=lambda x: len(x))
print(sorted_students)``````

a) [‘Bob’, ‘Alice’, ‘David’, ‘Charlie’]

b) [‘Alice’, ‘Bob’, ‘Charlie’, ‘David’]

c) [‘David’, ‘Alice’, ‘Bob’, ‘Charlie’]

d) [‘Alice’, ‘Charlie’, ‘Bob’, ‘David’]

Explanation: The `sorted()` function sorts the list `students` based on the length of each name, resulting in [‘Bob’, ‘Alice’, ‘David’, ‘Charlie’].

#### Q23. What is the purpose of the `lambda` keyword in Python?

a) To define a regular function

b) To define a class method

c) To create anonymous functions

d) To create generator functions

Explanation: The `lambda` keyword is used to create anonymous functions, which are functions without a name.

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

a) Lambda functions can contain multiple lines of code

b) Lambda functions can have docstrings

c) Lambda functions can be decorated

d) Lambda functions can use the `yield` keyword

Explanation: Lambda functions cannot have docstrings, as they are limited to a single expression.

#### Q25. What will the following code snippet output?

``````full_name = lambda first, last: f"{first.capitalize()} {last.capitalize()}"
result = full_name('john', 'doe')
print(result)``````

a) “John Doe”

b) “john doe”

c) “John doe”

d) “john Doe”

Explanation: The lambda function `full_name` capitalizes the first and last names and returns them as a full name.

#### Q26. Which of the following functions is equivalent to the lambda function `lambda x: x ** 3`?

a) `def cube(x): return x * x * x`

b) `def cube(x): return x ** 2`

c) `def cube(x): return x * 3`

d) `def cube(x): return x + 3`

Explanation: The lambda function `lambda x: x ** 3` calculates the cube of `x`, which is equivalent to `def cube(x): return x * x * x`.

#### Q27. What will be the output of the following code?

``````operations = {
'add': lambda x, y: x + y,
'subtract': lambda x, y: x - y,
'multiply': lambda x, y: x * y
}

result_subtract = operations['subtract'](10, 4)
result_multiply = operations['multiply'](2, 6)

a) 8 6 12

b) 3 6 12

c) 8 6 36

d) 3 6 36

Explanation: The dictionary `operations` contains lambda functions for addition, subtraction, and multiplication. The code then calls these functions with different arguments.

#### Q28. Which of the following is a correct lambda function to check if a number is prime?

a) `lambda x: True if x % 2 == 0 else False`

b) `lambda x: True if x % 2 != 0 else False`

c) `lambda x: all(x % i != 0 for i in range(2, x))`

d) `lambda x: any(x % i == 0 for i in range(2, x))`

Explanation: The lambda function `lambda x: all(x % i != 0 for i in range(2, x))` checks if a number is prime by testing all numbers from 2 to `x-1`.

#### Q29. What is the output of the following code?

``````func = lambda x: x[1:]
result = func("Python")
print(result)``````

a) “ython”

b) “Python”

c) “P”

d) “y”

Explanation: The lambda function `func` returns the string `x` without its first character.

#### Q30. Which of the following is a correct lambda function to convert a string to uppercase?

a) `lambda s: s.upper()`

b) `lambda s: s.capitalize()`

c) `lambda s: s.lower()`

d) `lambda s: s.title()`

Explanation: The lambda function `lambda s: s.upper()` converts a string `s` to uppercase.

#### Q31. What does the following lambda function do?

``````add_two = lambda x, y: x + y
print(result)``````

b) Multiplies the two numbers

c) Subtracts the second number from the first

d) Raises the first number to the power of the second

Explanation: The lambda function `add_two` adds its two arguments `x` and `y`, so `add_two(3, 4)` results in `3 + 4 = 7`.

#### Q32. What will be the output of the following code?

``````names = ["Alice", "Bob", "Charlie", "David"]
sorted_names = sorted(names, key=lambda x: x[-1])
print(sorted_names)``````

a) [“Charlie”, “David”, “Alice”, “Bob”]

b) [“Alice”, “David”, “Bob”, “Charlie”]

c) [“Bob”, “Alice”, “David”, “Charlie”]

d) [“Alice”, “Charlie”, “Bob”, “David”]

Explanation: The `sorted()` function sorts the list `names` based on the last character of each name, resulting in [“Alice”, “David”, “Bob”, “Charlie”].