nerdexam
Python_Institute

PCEP-30-02 · Question #254

What is the expected output of the following code? 1 z = y = x = 1 2 print(x, y, z, sep='')

The correct answer is A. 1*1*. There is an error in the stated correct answer for this question. Let me explain what the code actually does. The actual correct answer is B (111). z = y = x = 1 is a Python chained assignment that assigns the value 1 to all three variables simultaneously, so x, y, and z all…

Question

What is the expected output of the following code? 1 z = y = x = 1 2 print(x, y, z, sep='')

Options

  • A11
  • B111
  • Cxyz
  • DThe code is erroneous.

How the community answered

(22 responses)
  • A
    73% (16)
  • B
    5% (1)
  • C
    14% (3)
  • D
    9% (2)

Explanation

There is an error in the stated correct answer for this question. Let me explain what the code actually does.

The actual correct answer is B (111).

z = y = x = 1 is a Python chained assignment that assigns the value 1 to all three variables simultaneously, so x, y, and z all equal 1. Then print(x, y, z, sep='') outputs each variable's value with an empty string separator (instead of the default space), producing 111 with no spaces or other characters between the values.

Why the distractors are wrong:

  • A (1*1*) - There are no asterisks anywhere in the code; this output is impossible given these statements.
  • C (xyz) - x, y, z are variable names, not string values; the variables hold the integer 1, not the characters x, y, z.
  • D (erroneous) - The code is perfectly valid Python; chained assignment is legal syntax.

Memory tip: Think of sep='' as "remove the spaces between items." The default sep is ' ' (a single space), so print(1, 1, 1)1 1 1, but sep='' glues them together → 111.

Note for exam takers: The answer key provided marks A as correct, but this appears to be a mistake in the question. If you encounter this on an actual exam, select B (111) - it is the only output this code can produce.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice