PCEP-30-02 · Question #229
What is the expected output of the following code? ``python def func(x=2, y=3): return x * y print(func(y=2)) ``
The correct answer is B. The code is erroneous. Option B appears to be incorrect as stated - and this looks like a flawed answer key. The code is syntactically valid Python: single-space indentation is unconventional but legal, and func(y=2) runs without error. With x taking its default value of 2 and y explicitly set to 2…
Question
def func(x=2, y=3):
return x * y
print(func(y=2))
Options
- A4
- BThe code is erroneous.
- C6
- D2
How the community answered
(26 responses)- A4% (1)
- B73% (19)
- C15% (4)
- D8% (2)
Explanation
Option B appears to be incorrect as stated - and this looks like a flawed answer key. The code is syntactically valid Python: single-space indentation is unconventional but legal, and func(y=2) runs without error. With x taking its default value of 2 and y explicitly set to 2, the function returns 2 * 2 = 4, making A (4) the actual correct answer.
Why each distractor is wrong (relative to A being correct):
- B is wrong because no error occurs - Python accepts any consistent indentation, even one space.
- C (6) would be the result of
func()with both defaults (2 * 3), notfunc(y=2). - D (2) has no arithmetic basis here; it would only appear if the function returned
xoryalone.
Memory tip: When you see a function call with keyword arguments, mentally substitute: replace the named parameter and keep all others at their defaults. Here, func(y=2) → x=2 (default), y=2 (explicit) → 2 * 2 = 4. If you're ever unsure about indentation errors, remember Python only requires consistency within a block - one space works fine.
Heads up: Flag this question to your instructor. The answer key appears to contain an error; the code runs correctly and outputs
4.
Community Discussion
No community discussion yet for this question.