PCEP-30-02 · Question #66
What will be the output of the following code snippet? x = 2 y = 1 x *= y + 1 print(x)
The correct answer is B. 4. Option B (4) is correct because x = y + 1 expands to x = x (y + 1). Python evaluates the entire right-hand side first - so y + 1 becomes 1 + 1 = 2, then x = 2 2 = 4. Why the distractors fail: C (2) tempts those who ignore the + 1 entirely, as if the statement were just x = y →…
Question
Options
- A1
- B4
- C2
- D3
How the community answered
(55 responses)- A13% (7)
- B78% (43)
- C5% (3)
- D4% (2)
Explanation
Option B (4) is correct because x *= y + 1 expands to x = x * (y + 1). Python evaluates the entire right-hand side first - so y + 1 becomes 1 + 1 = 2, then x = 2 * 2 = 4.
Why the distractors fail:
- C (2) tempts those who ignore the
+ 1entirely, as if the statement were justx *= y→2 * 1 = 2. - D (3) catches those who misapply precedence left-to-right:
(x * y) + 1 = (2 * 1) + 1 = 3- but that's not how*=works; the whole right-hand expression is computed before multiplying. - A (1) has no arithmetic path to reach it; it likely traps those who confuse assignment with comparison or misread the variables.
Memory tip: Think of x *= expr as x = x * (expr) - the right side always gets wrapped in invisible parentheses and evaluated completely before the multiplication happens, just like any compound assignment operator.
Community Discussion
No community discussion yet for this question.