PCEP-30-02 · Question #193
What is the expected output of the following code? def func(x=2, y=3): return x * y print(func(y=2))
The correct answer is A. 4. Option A (4) is correct because calling func(y=2) passes 2 explicitly for y, while x falls back to its default value of 2 - so the function returns 2 2 = 4. B is wrong because the code is perfectly valid Python; keyword arguments like y=2 are a core language feature that lets…
Question
Options
- A4
- BThe code is erroneous.
- C6
- D2
How the community answered
(29 responses)- A76% (22)
- B7% (2)
- C3% (1)
- D14% (4)
Explanation
Option A (4) is correct because calling func(y=2) passes 2 explicitly for y, while x falls back to its default value of 2 - so the function returns 2 * 2 = 4.
B is wrong because the code is perfectly valid Python; keyword arguments like y=2 are a core language feature that lets callers override specific defaults without touching others. C (6) is the trap - it's what you'd get from func() with no arguments, using both defaults (2 * 3), a mistake made by forgetting that y was overridden. D (2) has no basis in the math; it might tempt someone who only looks at the passed argument value in isolation.
Memory tip: Think of default parameters as "factory settings" - calling with a keyword argument like y=2 only changes that one setting; every untouched parameter keeps its factory default. When in doubt, mentally substitute: x=2 (default, unchanged), y=2 (overridden) → 2 × 2 = 4.
Community Discussion
No community discussion yet for this question.