Python Questions and Answers – Function – 3

This set of Python Questions for campus interview 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

Answer: a
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.

2. What will be the output of the following Python code?

  1. y = 6
  2. z = lambda x: x * y
  3. print z(8)

a) 48
b) 14
c) 64
d) None of the mentioned
View Answer

Answer: a
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.
advertisement
advertisement

3. What will be the output of the following Python code?

  1. lamb = lambda x: x ** 3
  2. print(lamb(5))

a) 15
b) 555
c) 125
d) None of the mentioned
View Answer

Answer: c
Explanation: None.
advertisement

4. Does Lambda contains return statements?
a) True
b) False
View Answer

Answer: b
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.

5. Lambda is a statement.
a) True
b) False
View Answer

Answer: b
Explanation: lambda is an anonymous function in Python. Hence this statement is false.
advertisement

6. Lambda contains block of statements.
a) True
b) False
View Answer

Answer: b
Explanation: None.

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

  1. def f(x, y, z): return x + y + z
  2. f(2, 30, 400)

a) 432
b) 24000
c) 430
d) No output
View Answer

Answer: a
Explanation: None.

8. What will be the output of the following Python code?

  1. def writer():
  2. 	title = 'Sir'
  3. 	name = (lambda x:title + ' ' + x)
  4. 	return name
  5.  
  6. who = writer()
  7. who('Arthur')

a) Arthur Sir
b) Sir Arthur
c) Arthur
d) None of the mentioned
View Answer

Answer: b
Explanation: None.

9. What will be the output of the following Python code?

  1. L = [lambda x: x ** 2,
  2.          lambda x: x ** 3,
  3.          lambda x: x ** 4]
  4.  
  5. for f in L:
  6. 	print(f(3))

a)

27
81
343

b)

6
9
12

c)

9
27
81

d) None of the mentioned
View Answer

Answer: c
Explanation: None.

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

  1. min = (lambda x, y: x if x < y else y)
  2.  min(101*99, 102*98)

a) 9997
b) 9999
c) 9996
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

Sanfoundry Global Education & Learning Series – Python.

To practice all campus interview questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.