PCEP-30-02 · Question #87
What is the expected output of the following code? ``python x = 2 y = 6 x += y ** 2 x //= y // 2 // 3 print(x) ``
The correct answer is B. 18. There's a problem with this exam question - the code as written does not produce 18 (or any of the other listed choices). Let me walk through it step by step. Step-by-step evaluation: ``python x = 2 y = 6 x += y 2 # y2 = 36; x = 2 + 36 = 38 x //= y // 2 // 3 # y//2 = 3; 3//3 =…
Question
x = 2
y = 6
x += y ** 2
x //= y // 2 // 3
print(x)
Options
- A9
- B18
- C0
- D11
How the community answered
(22 responses)- A5% (1)
- B77% (17)
- C5% (1)
- D14% (3)
Explanation
There's a problem with this exam question - the code as written does not produce 18 (or any of the other listed choices). Let me walk through it step by step.
Step-by-step evaluation:
x = 2
y = 6
x += y ** 2 # y**2 = 36; x = 2 + 36 = 38
x //= y // 2 // 3 # y//2 = 3; 3//3 = 1; x = 38//1 = 38
print(x) # prints 38
Line 4 breakdown - y // 2 // 3:
//is left-associative, so:(y // 2) // 36 // 2 = 3, then3 // 3 = 1x //= 1→x = 38 // 1 = **38**
The actual output is 38, which is not among choices A–D. The stated answer of B=18 is mathematically incorrect for this code.
What the question may have intended:
To produce 18, one possible intended code might have been:
x = y ** 2 // 2 # 36 // 2 = 18
x //= y // 2 // 3 # 18 // 1 = 18
or the initial x should be 0 and the last divisor should be 2 instead of 1.
Recommendation: Verify the source of this question - there appears to be a typo either in the code or in the answer key. You can confirm by running the code yourself; Python will output 38.
Community Discussion
No community discussion yet for this question.