Python Questions and Answers – Function – 3

Python Questions focuses on “Functions”.

Q 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

Show Answer Answer:-A. lambda
Explanation Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called lambda. Lambda functions are restricted to a single expression. They can be used wherever normal functions can be used.


Q 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

Show Answer Answer:-A. 48
Explanation The lambda keyword creates an anonymous function. The x is a parameter, that is passed to the lambda function. The parameter is followed by a colon character. The code next to the colon is the expression that is executed, when the lambda function is called. The lambda function is assigned to the z variable. The lambda function is executed. The number 8 is passed to the anonymous function and it returns 48 as the result. Note that z is not a name for this function. It is only a variable to which the anonymous function was assigned.


Q 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

Show Answer Answer:-C. 125
Explanation


Q 4. Does Lambda contains return statements?
A. True
B. False

Show Answer Answer:-B. False
Explanation lambda definition does not include a return statement. it always contains an expression which is returned. Also note that we can put a lambda definition anywhere a function is expected. We don’t have to assign it to a variable at all.


Q 5. Lambda is a statement.
A. True
B. False

Show Answer Answer:-B. False
Explanation lambda is an anonymous function in Python. Hence this statement is false.


Q 6. Lambda contains block of statements.
A. True
B. False

Show Answer Answer:-B. False


Q 7. What will be the output of the following Python code?

def f(x, y, z): return x + y + z
f(2, 30, 400)
A. 432
B. 24000
C. 430
D. No output

Show Answer Answer:-A. 432

Q 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

Show Answer Answer:-B. Sir Arthur


Q 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

Show Answer Answer:-C. 9 27 81


Q 10. What will be the output of the following Python code?

min = (lambda x, y: x if x < y else y)
min(101*99, 102*98)
A. 9997
B. 9999
C. 9996
D. None of the mentioned

Show Answer Answer:-C. 9996

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page