Welcome to the Python Lambda Function **Python Interview Questions**! 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!

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

**Answer:** c

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

keyword.

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

**Answer:** b

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

a) `lambda arg1, arg2: expression`

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

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

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

**Answer:** a

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

.

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

**Answer:** b

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

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

**Answer:** a

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

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

a) 5

b) 10

c) 25

d) 15

**Answer:** b

**Explanation:** The lambda function `(lambda x: x * 2)`

multiplies the input `5`

by `2`

, resulting in `10`

.

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

**Answer: **a

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

`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

**Answer:** a

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

```
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]

**Answer:** b

**Explanation:** The `map()`

function applies the lambda function to each element in `nums`

, resulting in a new list with squared values.

```
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) []

**Answer:** b

**Explanation:** The `filter()`

function creates a new list with elements that satisfy the condition (even numbers) given by the lambda function.

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

a) 2

b) 3

c) 5

d) 6

**Answer:** c

**Explanation:** The lambda function `f`

adds its two arguments `x`

and `y`

, so `f(2, 3)`

results in `2 + 3 = 5`

.

a) `lambda x: x * x`

b) `lambda x: x ^ 2`

c) `lambda x: x ** 2`

d) `lambda x: x + x`

**Answer:** c

**Explanation:** The lambda function `lambda x: x ** 2`

calculates the square of a number `x`

.

```
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

**Answer:** b

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

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

a) “Hello, World!”

b) “Hello!”

c) None

d) Error: lambda functions require arguments

**Answer:** a

**Explanation:** The lambda function `greet`

has no arguments and returns the string “Hello, World!”, which is then printed.

```
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]

**Answer:** a

**Explanation:** Each lambda function in `func_list`

is applied to the value `5`

, resulting in `[5 + 2, 5 * 2, 5 ** 2]`

.

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

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

c) `lambda x y: x * y`

d) `lambda x: x * y`

**Answer:** a

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

.

```
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

**Answer:** c

**Explanation:** The lambda function `square_if_positive`

returns the square of the number if it is positive, otherwise it returns None.

```
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]

**Answer:** a

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

a) `lambda x: x < 0`

b) `lambda x: x > 0`

c) `lambda x: x == 0`

d) `lambda x: x != 0`

**Answer: **a

**Explanation:** The lambda function `lambda x: x < 0`

checks if a number is negative.

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

a) “even”

b) “odd”

c) 7

d) None

**Answer: **b

**Explanation:** The lambda function `f`

checks if a number is even or odd and returns the respective string.

```
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

**Answer:** A

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

```
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’]

**Answer:** a

**Explanation:** The `sorted()`

function sorts the list `students`

based on the length of each name, resulting in [‘Bob’, ‘Alice’, ‘David’, ‘Charlie’].

`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

**Answer: **c

**Explanation:** The `lambda`

keyword is used to create anonymous functions, which are functions without a name.

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

**Answer: **C

**Explanation:** Lambda functions can be decorated. Other options are false.

```
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”

**Answer: **a

**Explanation: **The lambda function `full_name`

capitalizes the first and last names and returns them as a full name.

`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`

**Answer: **a

**Explanation:** The lambda function `lambda x: x ** 3`

calculates the cube of `x`

, which is equivalent to `def cube(x): return x * x * x`

.

```
operations = {
'add': lambda x, y: x + y,
'subtract': lambda x, y: x - y,
'multiply': lambda x, y: x * y
}
result_add = operations['add'](5, 3)
result_subtract = operations['subtract'](10, 4)
result_multiply = operations['multiply'](2, 6)
print(result_add, result_subtract, result_multiply)
```

a) 8 6 12

b) 3 6 12

c) 8 6 36

d) 3 6 36

**Answer:** a

**Explanation:** The dictionary `operations`

contains lambda functions for addition, subtraction, and multiplication. The code then calls these functions with different arguments.

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))`

**Answer: **c

**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`

.

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

a) “ython”

b) “Python”

c) “P”

d) “y”

**Answer:** a

**Explanation:** The lambda function `func`

returns the string `x`

without its first character.

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

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

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

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

**Answer:** a

**Explanation:** The lambda function `lambda s: s.upper()`

converts a string `s`

to uppercase.

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

a) Adds the two numbers

b) Multiplies the two numbers

c) Subtracts the second number from the first

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

**Answer: **a

**Explanation:** The lambda function `add_two`

adds its two arguments `x`

and `y`

, so `add_two(3, 4)`

results in `3 + 4 = 7`

.

```
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”]

**Answer:** C

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

Congratulations on completing the Python Lambda Function MCQs! Lambda functions provide a concise and powerful way to create small functions in Python without the need for a full function definition. By mastering lambda functions, you gain the ability to write more expressive and efficient code for tasks that require short, simple functions. Keep practicing and experimenting with lambda functions to become proficient in using this versatile tool. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!

You can also enroll in our free Python Course Today!

Read our more articles related to MCQs in Python:

- 30+ MCQs on Python OOPs Concepts
- 30+ MCQs on Python Inheritance and Polymorphism
- 30+ MCQs on Python Abstraction and Encapsulation

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

it is too nice blog to revise concept.

In some MCQs answer were incorrect . please correct it