PCEP-30-02 · Question #168
What is the output of the following snippet? ``python def fun(x, y, z): return x + 2 y + 3 z print(fun(0, z=1, y=3)) ``
The correct answer is A. 9. Calling fun(0, z=1, y=3) binds x=0, y=3, and z=1 - Python allows keyword arguments to appear out of order after positional ones, so the mapping is unambiguous. Evaluating the return: 0 + 2×3 + 3×1 = 0 + 6 + 3 = 9, confirming A. B is wrong because the call is perfectly valid…
Question
def fun(x, y, z):
return x + 2 * y + 3 * z
print(fun(0, z=1, y=3))
Options
- A9
- BThe snippet is erroneous.
- C0
- D3
How the community answered
(63 responses)- A79% (50)
- B3% (2)
- C13% (8)
- D5% (3)
Explanation
Calling fun(0, z=1, y=3) binds x=0, y=3, and z=1 - Python allows keyword arguments to appear out of order after positional ones, so the mapping is unambiguous. Evaluating the return: 0 + 2×3 + 3×1 = 0 + 6 + 3 = 9, confirming A.
B is wrong because the call is perfectly valid Python - positional arguments must come before keyword arguments, and that rule is satisfied here (0 fills x positionally before the keywords begin).
C (0) would only result if all arguments were zero; it's a trap for readers who misread x=0 as "the whole function returns 0."
D (3) would be the result if only y contributed (2×3 = 6… actually not even that), making it a plausible-looking but arithmetically incorrect distractor.
Memory tip: When keyword arguments appear out of order, mentally re-sort them to match the function signature first - write out x=0, y=3, z=1, then substitute into the formula. This eliminates ordering confusion before you do any arithmetic.
Community Discussion
No community discussion yet for this question.