This set of Python Questions focuses on “Functions”.
1. Python supports the creation of anonymous functions at runtime, using a construct called __________
a) lambda
b) pi
c) anonymous
d) none of the mentioned
View Answer
Explanation: In Python, lambda functions are anonymous, meaning they don’t have a name. They are defined using the lambda keyword and can take any number of arguments but only have one expression. Lambdas are useful for creating small, throwaway functions quickly without formally defining them using def.
2. What will be the output of the following Python code?
y = 6 z = lambda x: x * y print (z(8))
a) 48
b) 14
c) 64
d) None of the mentioned
View Answer
Explanation: The lambda function takes x as input and multiplies it by the variable y which is 6. When z(8) is called, it calculates 8 * 6, resulting in 48. Thus, the output is 48.
3. What will be the output of the following Python code?
lamb = lambda x: x ** 3 print(lamb(5))
a) 15
b) 555
c) 125
d) None of the mentioned
View Answer
Explanation: The lambda function calculates the cube of the input x. So, when lamb(5) is called, it computes 53 = 125. Hence, the output is 125.
4. Does Lambda contains return statements?
a) True
b) False
View Answer
Explanation: In Python, lambda functions do not contain return statements. Unlike regular functions defined using def, lambda functions are anonymous and consist of a single expression. This expression is automatically returned when the lambda is called, making the use of the return keyword unnecessary and invalid within a lambda. For example, lambda x: x * 2 will return the result of x * 2 when invoked. This concise syntax is useful for small, throwaway functions.
5. Lambda is a statement.
a) True
b) False
View Answer
Explanation: Lambda is not a statement; it is an expression in Python. This means it returns a function object and can be used wherever expressions are allowed, such as in function calls or assignments. Since it’s an expression, it cannot include statements like return, pass, or print inside its body.
6. Lambda contains block of statements.
a) True
b) False
View Answer
Explanation: A lambda in Python can only contain a single expression, not a block of statements. This expression is evaluated and returned. You cannot include multiple lines, control flow statements like if, for, or while, or use return within a lambda.
7. What will be the output of the following Python code?
def f(x, y, z): return x + y + z print(f(2, 30, 400))
a) 432
b) 24000
c) 430
d) No output
View Answer
Explanation: The function f(x, y, z) returns the sum of the three arguments. So, f(2, 30, 400) results in: 2 + 30 + 400 = 432.
8. What will be the output of the following Python code?
def writer(): title = 'Sir' name = (lambda x:title + ' ' + x) return name who = writer() print(who('Arthur'))
a) Arthur Sir
b) Sir Arthur
c) Arthur
d) None of the mentioned
View Answer
Explanation: In the code, the function writer() defines a local variable title with the value ‘Sir’. It then creates and returns a lambda function name that takes an argument x and returns the string title + ‘ ‘ + x. Since the lambda function closes over the title variable (a closure), when you call who(‘Arthur’), it returns ‘Sir Arthur’.
9. What will be the output of the following Python code?
L = [lambda x: x ** 2, lambda x: x ** 3, lambda x: x ** 4] for f in L: print(f(3))
a)
27 81 343
b)
6 9 12
c)
9 27 81
d) None of the mentioned
View Answer
Explanation: The list L contains three lambda functions that calculate the square, cube, and fourth power of a number.
- The first computes the square (x ** 2), so 3 ** 2 = 9.
- The second computes the cube (x ** 3), so 3 ** 3 = 27.
- The third computes the fourth power (x ** 4), so 3 ** 4 = 81.
The for loop calls each lambda function with argument 3, printing their respective results.
10. What will be the output of the following Python code?
min = (lambda x, y: x if x < y else y) print(min(101*99, 102*98))
a) 9997
b) 9999
c) 9996
d) None of the mentioned
View Answer
Explanation: The lambda function compares two numbers and returns the smaller one. Since 101 * 99 equals 9999 and 102 * 98 equals 9996, the function returns 9996, which is the smaller value. Hence, the output is 9996.
Sanfoundry Global Education & Learning Series – Python.
To practice all questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Programming Internship
- Apply for Python Internship
- Practice Programming MCQs
- Check Information Technology Books
- Check Python Books