PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Hard
Question 57
Q282 - Functions
What is the expected output of the following code?
- def f(a, b):
- return a(b)
- print(f(lambda x: x and True, 1 > 0))
-
A
- True
-
B
- None
-
C
- False
-
D
- 0
Reveal correct answer
Correct answer: A
Explanation
Topics: lambda def and operator greater than operator
Try it yourself:
- def f(a, b):
- return a(b)
- print(f(lambda x: x and True, 1 > 0)) # True
Explanation:
The lambda function will return
the Boolean conjunction of its argument and True
The f() function will invoke it
and pass its second parameter as an argument.
1 > 0 is True will be the argument which is passed to the lambda function.
True and True is True
and therefore True is returned by the lambda function
and then by the f() function
and finally printed.
Q282 (Please refer to this number, if you want to write me about this question.)
A. The lambda function lambda x: x and True evaluates to True when x is True, and 1 > 0 is True, so the function f returns True.
B. The function f does not return None in this case. It returns a boolean value based on the condition provided in the lambda function.
C. The lambda function lambda x: x and True evaluates to True when x is True, and 1 > 0 is True, so the function f returns True, not False.
D. The lambda function lambda x: x and True does not evaluate to 0. The function f returns a boolean value based on the condition, not an integer.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
