nerdexam
Python_Institute

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

What is the output of the following code snippet?
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)
  • A
    13% (7)
  • B
    2% (1)
  • C
    77% (43)
  • D
    2% (1)
  • E
    7% (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 y were printed before x.
  • 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 applied y += 1, giving y=3 - a common mistake when defaults and call args are confused.
  • E (33) likely comes from computing x = 2 + 1 = 3 correctly but then mistakenly adding y = x + 1 = 3 instead of y = 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.

Full PCEP-30-02 Practice