PCEP-30-02 · Question #166
What is the output of the following code snippet? ``python def test(x=1, y=2): x = x + y y += 1 print(x, y) test(2, 1) ``
The correct answer is C. 32. Calling test(2, 1) passes x=2 and y=1, overriding the defaults. Then x = x + y becomes 2 + 1 = 3, and y += 1 becomes 1 + 1 = 2, so print(x, y) outputs 3 2 - matching C (32). Why the distractors fail: A (23) reverses the print order, as if y were printed before x. B is wrong…
Question
def test(x=1, y=2):
x = x + y
y += 1
print(x, y)
test(2, 1)
Options
- A23
- BThe code is erroneous.
- C32
- D13
- E33
How the community answered
(56 responses)- A13% (7)
- B2% (1)
- C77% (43)
- D2% (1)
- E7% (4)
Explanation
Calling test(2, 1) passes x=2 and y=1, overriding the defaults. Then x = x + y becomes 2 + 1 = 3, and y += 1 becomes 1 + 1 = 2, so print(x, y) outputs 3 2 - matching C (32).
Why the distractors fail:
- A (23) reverses the print order, as if
ywere printed beforex. - B is wrong because the code is syntactically and semantically valid Python.
- D (13) is the result you'd get if you ignored the call arguments and used the default values (
x=1,y=2) then only appliedy += 1, givingy=3- a common mistake when defaults and call args are confused. - E (33) likely comes from computing
x = 2 + 1 = 3correctly but then mistakenly addingy = x + 1 = 3instead ofy = original_y + 1.
Memory tip: When a function is called with arguments, those arguments replace the defaults - trace the call signature first, then step through each line using those substituted values, not the ones in def.
Community Discussion
No community discussion yet for this question.