PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy
Question 58
Q168 - Functions
What is the expected output of the following code?
- x = lambda a, b: a ** b
- print(x(2, 10))
-
A
1024 -
B
SyntaxError
-
C
2222222222
Reveal correct answer
Correct answer: A
Explanation
Topic: lambda
Try it yourself:
- x = lambda a, b: a ** b
- print(x(2, 10)) # 1024
- def y(a, b):
- return a ** b
- print(y(2, 10)) # 1024
Explanation:
This does work, but you should not do it.
It violates the programming guideline E731:
Do not assign a lambda expression, use a def
Q168 (Please refer to this number, if you want to write me about this question.)
A. The lambda function defined as x takes two arguments a and b, and returns the result of raising a to the power of b. In this case, x(2, 10) will calculate 2 to the power of 10, which is equal to 1024. Therefore, the expected output is 1024.
B. This choice is incorrect because there is no syntax error in the given code. The lambda function is defined correctly, and the function call x(2, 10) is valid. The code will execute without any syntax errors and will output the result of 2 raised to the power of 10, which is 1024.
C. This choice is incorrect because it suggests that the output will be 2222222222, which is not the correct result of the lambda function x(2, 10). The lambda function calculates the power of the first argument (2) to the second argument (10), resulting in 1024, not a repeated digit.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
