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
Options
- A11
- B111
- Cxyz
- DThe code is erroneous.
How the community answered
(22 responses)- A73% (16)
- B5% (1)
- C14% (3)
- D9% (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,zare variable names, not string values; the variables hold the integer1, not the charactersx,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.