PCEP-30-02 · Question #225
What is the expected output of the following code? ``python def func(x, y, z): return x + 4 y + 5 z print(func(1, z=2, y=3)) ``
The correct answer is C. 23. Option C (23) is correct because Python allows keyword arguments to be passed in any order - func(1, z=2, y=3) binds x=1, y=3, z=2, giving 1 + 4(3) + 5(2) = 1 + 12 + 10 = 23. Why the distractors are wrong: B (24) is the most tempting trap - it results from swapping y and z…
Question
def func(x, y, z):
return x + 4 * y + 5 * z
print(func(1, z=2, y=3))
Options
- A80
- B24
- C23
- DThe code is erroneous.
How the community answered
(48 responses)- A4% (2)
- B10% (5)
- C79% (38)
- D6% (3)
Explanation
Option C (23) is correct because Python allows keyword arguments to be passed in any order - func(1, z=2, y=3) binds x=1, y=3, z=2, giving 1 + 4(3) + 5(2) = 1 + 12 + 10 = 23.
Why the distractors are wrong:
- B (24) is the most tempting trap - it results from swapping
yandz(i.e., treatingy=2, z=3), which is the mistake of assuming keyword args must match the order they appear in the call. - A (80) likely comes from misreading the multipliers or performing operations incorrectly (e.g., multiplying all terms together rather than summing).
- D is wrong because the code is completely valid Python - the only rule is that positional arguments (
1) must come before keyword arguments (z=2, y=3), which is satisfied here.
Memory tip: Think of keyword arguments as labeled boxes - Python doesn't care what order you hand them over, as long as each label matches a parameter name and all positional arguments come first.
Community Discussion
No community discussion yet for this question.