0% found this document useful (0 votes)
28 views

Python Lambda Expression

Contains Python Lambda Expression MCQs with options and answers

Uploaded by

shreya0473
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Python Lambda Expression

Contains Python Lambda Expression MCQs with options and answers

Uploaded by

shreya0473
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Python: Lambda Expressions

Lambda expressions, also known as lambda functions, are a concise way to create
small anonymous functions in Python. They allow you to define functions inline,
without the need for a formal function definition. Lambda functions are particularly
useful when you need a simple function for a short period and don't want to go
through the process of defining a separate named function. Here's a detailed
breakdown of lambda expressions in Python:

1.
Syntax: The syntax of a lambda function is quite simple. It consists of the lambda
keyword followed by one or more arguments, a colon :, and then the expression to be
evaluated.
2.
3.
python
4.
5.
Copy code
6.
7.
lambda arguments: expression
8.
9.
10.
Anonymous Functions: Lambda functions are anonymous, meaning they don't have
a name. They are created at runtime and can be used immediately.
11.
12.
Single Expression: Lambda functions are restricted to a single expression. This
expression is evaluated and returned when the lambda function is called.
13.
14.
Usage: Lambda functions are often used as arguments to higher-order functions
(functions that take other functions as arguments), such as map(), filter(), and
sorted().
15.
16.
python
17.
18.
Copy code
19.
20.
# Example: Using lambda function with map()
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))# Output: [1, 4, 9, 16, 25]
21.
22.
23.
Avoiding Defining a Separate Function: Lambda expressions are particularly
handy when the functionality of the function is quite simple and you don't want to
define a separate named function.
24.
25.
python
26.
27.
Copy code
28.
29.
# Example: Sorting a list of tuples by the second element
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
sorted_pairs = sorted(pairs, key=lambda pair: pair[1])# Output: [(4,
'four'), (1, 'one'), (3, 'three'), (2, 'two')]
30.
31.
32.
Limitations: While lambda functions can be useful, they are limited in their
capabilities compared to named functions. They are meant for simple operations and
can't contain statements like return, pass, or even multi-line expressions.
33.
34.
Readability vs. Conciseness: While lambda functions can make code more concise,
they can also make it less readable if overused or used inappropriately. It's important
to strike a balance between conciseness and readability.
35.

In summary, lambda expressions provide a convenient way to create small


anonymous functions in Python. They are especially useful in situations where you
need a simple function for a short period or as an argument to higher-order functions.
However, they should be used judiciously and in situations where their conciseness
improves code readability rather than hampers it.

MCQs

1. What is a lambda expression in Python?


- A) A built-in function
- B) A named function
- C) An anonymous function
- D) A method of defining classes

**Answer: C) An anonymous function**

2. Which keyword is used to define a lambda expression in Python?


- A) def
- B) lambda
- C) function
- D) anonymous

**Answer: B) lambda**

3. Lambda functions in Python are restricted to:


- A) A single line
- B) Multiple lines
- C) Multiple return statements
- D) No restrictions
**Answer: A) A single line**

4. Lambda functions are particularly useful when:


- A) Defining complex functions
- B) Defining functions with multiple arguments
- C) Defining simple functions for short-term use
- D) Defining functions for recursion

**Answer: C) Defining simple functions for short-term use**

5. Which of the following functions accepts lambda functions as arguments?


- A) map()
- B) filter()
- C) sorted()
- D) All of the above

**Answer: D) All of the above**

6. What is the output of the following code snippet?


```python
result = (lambda x: x * 2)(5)
print(result)
```
- A) 10
- B) 5
- C) 25
- D) None

**Answer: A) 10**

7. Lambda functions can be used to create:


- A) Recursive functions
- B) Higher-order functions
- C) Static methods
- D) Class methods

**Answer: B) Higher-order functions**

8. Which function is used to apply a lambda function to each element of an iterable?


- A) map()
- B) apply()
- C) transform()
- D) execute()

**Answer: A) map()**

9. Lambda functions can contain statements like:


- A) return
- B) pass
- C) if-else
- D) All of the above

**Answer: D) All of the above**

10. What is the result of the following code snippet?


```python
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x ** 2, numbers))
print(squares)
```
- A) [1, 4, 9, 16, 25]
- B) [1, 2, 3, 4, 5]
- C) [2, 4, 6, 8, 10]
- D) [0, 1, 4, 9, 16]

**Answer: A) [1, 4, 9, 16, 25]**

11. Which of the following is a limitation of lambda functions in Python?


- A) They can't be used with higher-order functions
- B) They can't be assigned to variables
- C) They can only contain a single expression
- D) They can't be used with lists

**Answer: C) They can only contain a single expression**

12. Lambda functions are also known as:


- A) Anonymous functions
- B) Named functions
- C) Static functions
- D) Recursive functions

**Answer: A) Anonymous functions**

13. In lambda syntax, what follows the colon (`:`)?


- A) Arguments
- B) Return statement
- C) Expression to be evaluated
- D) Function body

**Answer: C) Expression to be evaluated**

14. Which of the following is a valid lambda function?


- A) `lambda x, y: x + y`
- B) `lambda x y: x + y`
- C) `lambda x, y -> x + y`
- D) `lambda (x, y): x + y`

**Answer: A) `lambda x, y: x + y`**

15. What is the purpose of using lambda functions?


- A) To improve code readability
- B) To define complex functions
- C) To define functions for recursion
- D) To create simple, short-term functions

**Answer: D) To create simple, short-term functions**

16. Lambda functions are commonly used with which built-in function to sort iterables?
- A) sort()
- B) sorted()
- C) sort_by()
- D) order()

**Answer: B) sorted()**
17. Which of the following is a correct way to pass a lambda function as an argument?
- A) `map(my_lambda_function, my_list)`
- B) `map(my_list, my_lambda_function)`
- C) `map(lambda my_lambda_function, my_list)`
- D) `map(lambda x: my_lambda_function(x), my_list)`

**Answer: D) `map(lambda x: my_lambda_function(x), my_list)`**

18. Lambda functions can be used to perform:


- A) Complex mathematical operations
- B) Simple transformations on data
- C) Control flow operations
- D) Exception handling

**Answer: B) Simple transformations on data**

19. What is the output of the following code snippet?


```python
my_lambda = lambda x: x % 2 == 0
print(my_lambda(5))
```
- A) True
- B) False
- C) Error
- D) None

**Answer: B) False**

20. Which of the following is true regarding lambda functions?


- A) They can't be used with higher-order functions
- B) They can contain multiple expressions
- C) They always have a name
- D) They are declared using the `def` keyword

**Answer: A) They can't be used with higher-order functions**

21. Lambda functions are often used with which built-in function to filter iterables based on a
specified condition?
- A) filter()
- B) select()
- C) extract()
- D) sift()

**Answer: A) filter()**

22. Which of the following is a correct way to define 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 y -> x * y`

**Answer: A) `lambda x, y: x * y`**

23. What is the output of the following code snippet?


```python
my_lambda = lambda x, y: x + y
print(my_lambda(3, 4))
```
- A) 7
- B) 12
- C) 34
- D) None

**Answer: A) 7**

24. Lambda functions can be used to create dictionaries using which built-in function?
- A) dict()
- B) map()
- C) lambda_dict()
- D) convert()

**Answer: A) dict()**

25. Which of the following is a correct way to sort a list of tuples based on the second element using a
lambda function?
- A) `sorted(my_list, key=lambda x: x[1])`
- B) `sort_by(my_list, lambda x: x[1])`
- C) `my_list.sort(key=lambda x: x[1])`
- D) `sort(my_list, lambda x: x[1])`

**Answer: A) `sorted(my_list, key=lambda x: x[1])`**

26. Lambda functions are primarily used to:


- A) Define complex algorithms
- B) Create anonymous functions
- C) Access class attributes
- D) Implement recursion

**Answer: B) Create anonymous functions**

27. In lambda syntax, what precedes the colon (`:`)?


- A) Arguments
- B) Return statement
- C) Expression to be evaluated
- D) Function body

**Answer: A) Arguments**

28. Which of the following is true regarding lambda functions?


- A) They can't be assigned to variables
- B) They can have multiple return statements
- C) They are always less efficient than named functions
- D) They can't contain control flow statements like if-else

**Answer: D) They can't contain control flow statements like if-else**

29. What is the output of the following code snippet?


```python
my_lambda = lambda x: x[0]
print(my_lambda([1, 2, 3]))
```
- A) 1
- B) 2
- C) 3
- D) [1, 2, 3]

**Answer: A) 1**

30. Lambda functions are most suitable for:


- A) Large, complex tasks
- B) Short, simple operations
- C) Object-oriented programming
- D) Parallel processing

**Answer: B) Short, simple operations**

31. Which function is used to apply a lambda function to each element of a list and return a new list?
- A) map()
- B) apply()
- C) transform()
- D) filter()

**Answer: A) map()**

32. What is the output of the following code snippet?


```python
my_lambda = lambda x: x ** 2 if x % 2 == 0 else x
print(my_lambda(4))
```
- A) 16
- B) 8
- C) 4
- D) 2

**Answer: A) 16**

33. Lambda functions can't be used for:


- A) Sorting lists
- B) Filtering iterables
- C) Recursive operations
- D) Simple transformations

**Answer: C) Recursive operations**

34. Which of the following is a correct way to define a lambda function that returns a tuple?
- A) `lambda x, y: (x, y)`
- B) `lambda (x, y): (x, y)`
- C) `lambda x y: (x, y)`
- D) `lambda x y -> (x, y)`

**Answer: A) `lambda x, y: (x, y)`**

35. Lambda functions are particularly useful when you need:


- A) A named function
- B) A complex function
- C) A simple function for a short period
- D) A function with multiple return statements

**Answer: C) A simple function for a short period**


36. Lambda functions can't be used to:
- A) Modify global variables
- B) Create iterators
- C) Access class methods
- D) Perform simple calculations

**Answer: A) Modify global variables**

37. What is the output of the following code snippet?


```python
my_lambda = lambda x, y=2: x * y
print(my_lambda(3))
```
- A) 5
- B) 6
- C) 3
- D) 9

**Answer: D) 9**

38. Which built-in function is used to apply a lambda function to each element of an iterable and
return elements for which the function returns True?
- A) map()
- B) reduce()
- C) filter()
- D) apply()

**Answer: C) filter()**

39. Lambda functions can't contain:


- A) Statements like `return`
- B) Statements like `pass`
- C) Control flow statements like `if-else`
- D) All of the above

**Answer: C) Control flow statements like `if-else`**

40. What is the output of the following code snippet?


```python
my_lambda = lambda x: x[::-1]
print(my_lambda("Python"))
```
- A) "nohtyP"
- B) "Python"
- C) "Pyt"
- D) Error

**Answer: A) "nohtyP"**

41. Lambda functions can be used with which built-in function to calculate the maximum value in an
iterable?
- A) max()
- B) maximum()
- C) largest()
- D) peak()

**Answer: A) max()**
42. Which of the following is true regarding lambda functions in Python?
- A) They are only used with named functions
- B) They can't accept arguments
- C) They are more memory-efficient than named functions
- D) They are less flexible than named functions

**Answer: D) They are less

flexible than named functions**

43. What is the output of the following code snippet?


```python
my_lambda = lambda x: True if x > 0 else False
print(my_lambda(-1))
```
- A) True
- B) False
- C) 1
- D) -1

**Answer: B) False**

44. Lambda functions are most commonly used with:


- A) Strings
- B) Integers
- C) Lists
- D) Dictionaries

**Answer: C) Lists**

45. Which built-in function is used to apply a lambda function to each element of an iterable and
return the result as a list?
- A) list_apply()
- B) lambda_apply()
- C) map()
- D) apply()

**Answer: C) map()**

46. Lambda functions can't contain:


- A) Multiple return statements
- B) Statements like `pass`
- C) Statements like `print`
- D) Control flow statements like `if-else`

**Answer: D) Control flow statements like `if-else`**

47. What is the output of the following code snippet?


```python
my_lambda = lambda x, y: x if x > y else y
print(my_lambda(3, 5))
```
- A) 3
- B) 5
- C) True
- D) False
**Answer: B) 5**

48. Lambda functions can't be used for:


- A) Defining complex algorithms
- B) Creating iterators
- C) Modifying global variables
- D) Accessing class attributes

**Answer: A) Defining complex algorithms**

49. What is the output of the following code snippet?


```python
my_lambda = lambda x: "even" if x % 2 == 0 else "odd"
print(my_lambda(4))
```
- A) "even"
- B) "odd"
- C) 4
- D) True

**Answer: A) "even"**

50. Lambda functions can't be used with:


- A) Higher-order functions
- B) Strings
- C) Lists
- D) Dictionaries

**Answer: B) Strings**

51. Which of the following is a correct way to pass a lambda function as an argument to the sorted()
function?
- A) `sorted(my_list, my_lambda_function)`
- B) `sorted(my_lambda_function, my_list)`
- C) `sorted(my_list, key=my_lambda_function)`
- D) `sorted(key=my_lambda_function, my_list)`

**Answer: C) `sorted(my_list, key=my_lambda_function)`**

52. Lambda functions can't be assigned to:


- A) Variables
- B) Lists
- C) Tuples
- D) Dictionaries

**Answer: A) Variables**

53. What is the output of the following code snippet?


```python
my_lambda = lambda x, y=2: x * y
print(my_lambda(3))
```
- A) 5
- B) 6
- C) 3
- D) 9
**Answer: B) 6**

54. Lambda functions can be used to create:


- A) Recursive functions
- B) Anonymous functions
- C) Higher-order functions
- D) Named functions

**Answer: B) Anonymous functions**

55. Lambda functions are primarily used for:


- A) Object-oriented programming
- B) Functional programming
- C) Imperative programming
- D) Procedural programming

**Answer: B) Functional programming**

56. What is the output of the following code snippet?


```python
my_lambda = lambda x: x[1:]
print(my_lambda("Hello"))
```
- A) "H"
- B) "e"
- C) "Hello"
- D) "ello"

**Answer: D) "ello"**

57. Lambda functions are commonly used with which built-in function to calculate the minimum value
in an iterable?
- A) min()
- B) minimum()
- C) smallest()
- D) low()

**Answer: A) min()**

58. Which of the following is a correct way to use a lambda function with the reduce() function?
- A) `reduce(lambda x, y: x + y, my_list)`
- B) `reduce(my_list, lambda x, y: x + y)`
- C) `reduce(lambda x, y: x + y)`
- D) `reduce(my_list, lambda x, y: x + y, 0)`

**Answer: A) `reduce(lambda x, y: x + y, my_list)`**

59. Lambda functions can be used with which built-in function to calculate the sum of elements in an
iterable?
- A) sum()
- B) add()
- C) total()
- D) accumulate()

**Answer: A) sum()**
60. What is the output of the following code snippet?
```python
my_lambda = lambda x: x % 2 == 0
print(my_lambda(6))
```
- A) True
- B) False
- C) Error
- D) None

**Answer: A) True**

61. Lambda functions can't be used to:


- A) Define complex algorithms
- B) Modify global variables
- C) Create iterators
- D) Access class methods

**Answer: A) Define complex algorithms**

62. Lambda functions can be used to create:


- A) Anonymous functions
- B) Recursive functions
- C) Methods
- D) Static functions

**Answer: A) Anonymous functions**

63. What is the output of the following code snippet?


```python
my_lambda = lambda x: x[0]
print(my_lambda([1, 2, 3]))
```
- A) 1
- B) 2
- C) 3
- D) [1, 2, 3]

**Answer: A) 1**

64. Lambda functions are most suitable for:


- A) Long, complex tasks
- B) Short, simple operations
- C) Object-oriented programming
- D) Parallel processing

**Answer: B) Short, simple operations**

65. Which built-in function is used to apply a lambda function to each element of a list and return a
new list?
- A) map()
- B) apply()
- C) transform()
- D) filter()

**Answer: A) map()**
66. What is the output of the following code snippet?
```python
my_lambda = lambda x: x ** 2 if x % 2 == 0 else x
print(my_lambda(4))

```
- A) 16
- B) 8
- C) 4
- D) 2

**Answer: A) 16**

67. Lambda functions can't contain:


- A) Statements like `return`
- B) Statements like `pass`
- C) Control flow statements like `if-else`
- D) All of the above

**Answer: C) Control flow statements like `if-else`**

68. Which of the following is a correct way to define a lambda function that returns a tuple?
- A) `lambda x, y: (x, y)`
- B) `lambda (x, y): (x, y)`
- C) `lambda x y: (x, y)`
- D) `lambda x y -> (x, y)`

**Answer: A) `lambda x, y: (x, y)`**

69. Lambda functions are primarily used to:


- A) Define complex algorithms
- B) Create anonymous functions
- C) Access class attributes
- D) Implement recursion

**Answer: B) Create anonymous functions**

70. In lambda syntax, what precedes the colon (`:`)?


- A) Arguments
- B) Return statement
- C) Expression to be evaluated
- D) Function body

**Answer: A) Arguments**

71. Which of the following is true regarding lambda functions?


- A) They can't be assigned to variables
- B) They can have multiple return statements
- C) They are always less efficient than named functions
- D) They can't contain control flow statements like if-else

**Answer: D) They can't contain control flow statements like if-else**

72. What is the output of the following code snippet?


```python
my_lambda = lambda x: x[::-1]
print(my_lambda("Python"))
```
- A) "nohtyP"
- B) "Python"
- C) "Pyt"
- D) Error

**Answer: A) "nohtyP"**

73. Lambda functions can be used with which built-in function to calculate the maximum value in an
iterable?
- A) max()
- B) maximum()
- C) largest()
- D) peak()

**Answer: A) max()**

74. Which of the following is a correct way to use a lambda function with the reduce() function?
- A) `reduce(lambda x, y: x + y, my_list)`
- B) `reduce(my_list, lambda x, y: x + y)`
- C) `reduce(lambda x, y: x + y)`
- D) `reduce(my_list, lambda x, y: x + y, 0)`

**Answer: A) `reduce(lambda x, y: x + y, my_list)`**

75. Lambda functions can be used with which built-in function to calculate the sum of elements in an
iterable?
- A) sum()
- B) add()
- C) total()
- D) accumulate()

**Answer: A) sum()**

76. What is the output of the following code snippet?


```python
my_lambda = lambda x: x % 2 == 0
print(my_lambda(6))
```
- A) True
- B) False
- C) Error
- D) None

**Answer: A) True**

77. Lambda functions can't be used to:


- A) Define complex algorithms
- B) Modify global variables
- C) Create iterators
- D) Access class methods

**Answer: A) Define complex algorithms**

78. What is the output of the following code snippet?


```python
my_lambda = lambda x: x[0]
print(my_lambda([1, 2, 3]))
```
- A) 1
- B) 2
- C) 3
- D) [1, 2, 3]

**Answer: A) 1**

79. Lambda functions are most suitable for:


- A) Long, complex tasks
- B) Short, simple operations
- C) Object-oriented programming
- D) Parallel processing

**Answer: B) Short, simple operations**

80. Which built-in function is used to apply a lambda function to each element of a list and return a
new list?
- A) map()
- B) apply()
- C) transform()
- D) filter()

**Answer: A) map()**

81. What is the output of the following code snippet?


```python
my_lambda = lambda x: x ** 2 if x % 2 == 0 else x
print(my_lambda(4))
```
- A) 16
- B) 8
- C) 4
- D) 2

**Answer: A) 16**

82. Lambda functions can't contain:


- A) Statements like `return`
- B) Statements like `pass`
- C) Control flow statements like `if-else`
- D) All of the above

**Answer: C) Control flow statements like `if-else`**

83. Which of the following is a correct way to define a lambda function that returns a tuple?
- A) `lambda x, y: (x, y)`
- B) `lambda (x, y): (x, y)`
- C) `lambda x y: (x, y)`
- D) `lambda x y -> (x, y)`

**Answer: A) `lambda x, y: (x, y)`**

84. Lambda functions are primarily used to:


- A) Define complex algorithms
- B) Create anonymous functions
- C) Access class attributes
- D) Implement recursion

**Answer: B) Create anonymous functions**

85. In lambda syntax, what precedes the colon (`:`)?


- A) Arguments
- B) Return statement
- C) Expression to be evaluated
- D) Function body

**Answer: A) Arguments**

86. Which of the following is true regarding lambda functions?


- A) They can't be assigned to variables
- B) They can have multiple return statements
- C) They are always less efficient than named functions
- D) They can't contain control flow statements like if-else

**Answer: D) They can't contain control flow statements like if-else**

87. What is the output of the following code snippet?


```python
my_lambda = lambda x: x[::-1]
print(my_lambda("Python"))
```
- A) "nohtyP"
- B) "Python"
- C) "Pyt"
- D) Error

**Answer: A) "nohtyP"**

88. Lambda functions can be used with which built-in function to calculate the maximum value in an
iterable

?
- A) max()
- B) maximum()
- C) largest()
- D) peak()

**Answer: A) max()**

89. Which of the following is a correct way to use a lambda function with the reduce() function?
- A) `reduce(lambda x, y: x + y, my_list)`
- B) `reduce(my_list, lambda x, y: x + y)`
- C) `reduce(lambda x, y: x + y)`
- D) `reduce(my_list, lambda x, y: x + y, 0)`

**Answer: A) `reduce(lambda x, y: x + y, my_list)`**

90. Lambda functions can be used with which built-in function to calculate the sum of elements in an
iterable?
- A) sum()
- B) add()
- C) total()
- D) accumulate()

**Answer: A) sum()**

91. What is the output of the following code snippet?


```python
my_lambda = lambda x: x % 2 == 0
print(my_lambda(6))
```
- A) True
- B) False
- C) Error
- D) None

**Answer: A) True**

92. Lambda functions can't be used to:


- A) Define complex algorithms
- B) Modify global variables
- C) Create iterators
- D) Access class methods

**Answer: A) Define complex algorithms**

93. What is the output of the following code snippet?


```python
my_lambda = lambda x: x[0]
print(my_lambda([1, 2, 3]))
```
- A) 1
- B) 2
- C) 3
- D) [1, 2, 3]

**Answer: A) 1**

94. Lambda functions are most suitable for:


- A) Long, complex tasks
- B) Short, simple operations
- C) Object-oriented programming
- D) Parallel processing

**Answer: B) Short, simple operations**

95. Which built-in function is used to apply a lambda function to each element of a list and return a
new list?
- A) map()
- B) apply()
- C) transform()
- D) filter()

**Answer: A) map()**

96. What is the output of the following code snippet?


```python
my_lambda = lambda x: x ** 2 if x % 2 == 0 else x
print(my_lambda(4))
```
- A) 16
- B) 8
- C) 4
- D) 2

**Answer: A) 16**

97. Lambda functions can't contain:


- A) Statements like `return`
- B) Statements like `pass`
- C) Control flow statements like `if-else`
- D) All of the above

**Answer: C) Control flow statements like `if-else`**

98. Which of the following is a correct way to define a lambda function that returns a tuple?
- A) `lambda x, y: (x, y)`
- B) `lambda (x, y): (x, y)`
- C) `lambda x y: (x, y)`
- D) `lambda x y -> (x, y)`

**Answer: A) `lambda x, y: (x, y)`**

99. Lambda functions are primarily used to:


- A) Define complex algorithms
- B) Create anonymous functions
- C) Access class attributes
- D) Implement recursion

**Answer: B) Create anonymous functions**

100. In lambda syntax, what precedes the colon (`:`)?


- A) Arguments
- B) Return statement
- C) Expression to be evaluated
- D) Function body

**Answer: A) Arguments**

You might also like