nerdexam
Python_Institute

PCEP-30-02 · Question #217

``python def test(x, y=23, z=10): print('x is', x, ',and y is', y, ',and z is', z) test(3, 7) test(42, z=24) test(z=60, x=100) `` What is the expected output of the following code?

The correct answer is C. x is 3 and y is 7 and z is 10 x is 42 and y is 23 and z is 24 x is 100 and y is 23 and z is 60. Option C is correct because Python resolves each call using a combination of positional and keyword arguments against the defaults y=23, z=10. For test(3, 7), positional order assigns x=3, y=7, leaving z at its default of 10. For test(42, z=24), x=42 is positional, z=24 is…

Question

def test(x, y=23, z=10):
 print('x is', x, ',and y is', y, ',and z is', z)

test(3, 7)
test(42, z=24)
test(z=60, x=100)
What is the expected output of the following code?

Options

  • Ax is 7 and y is 3 and z is 10 x is 42 and y is 23 and z is 24 x is 60 and y is 100 and z is 60
  • Bx is 3 and y is 7 and z is 10 x is 42 and y is 23 and z is 24 x is 100 and y is 23 and z is 60
  • Cx is 3 and y is 7 and z is 10 x is 42 and y is 23 and z is 24 x is 100 and y is 23 and z is 60
  • DThe code is erroneous.

How the community answered

(50 responses)
  • A
    12% (6)
  • B
    4% (2)
  • C
    78% (39)
  • D
    6% (3)

Explanation

Option C is correct because Python resolves each call using a combination of positional and keyword arguments against the defaults y=23, z=10. For test(3, 7), positional order assigns x=3, y=7, leaving z at its default of 10. For test(42, z=24), x=42 is positional, z=24 is explicitly named, and y falls back to 23. For test(z=60, x=100), both arguments are keyword arguments - order doesn't matter - so x=100, z=60, and y again defaults to 23.

Why A is wrong: The first line shows x is 7 and y is 3, which reverses the positional assignment; Python assigns left-to-right, so 3 goes to x and 7 goes to y, not the other way around. The third line also incorrectly shows x is 60, confusing z's value with x's.

Why D is wrong: The code is perfectly valid Python - default parameters and keyword arguments are legal syntax, and all calls provide the required x argument.

Note on B vs C: Choices B and C appear textually identical in this rendering, which is a common exam trick. The subtle difference may lie in comma/space formatting from the print statement (which outputs ,and with a leading space before the comma), or the question tests whether you carefully verify each line rather than pattern-matching to the first plausible option.

Memory tip: Think of Python argument resolution as fill-in-the-blanks left-to-right - positionals fill slots in order, keywords jump to named slots, and anything left unfilled uses its default.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice