What is the output of the following C-style pseudocode? x = 5 y = x++ z = ++x…
2026
What is the output of the following C-style pseudocode?
x = 5
y = x++
z = ++x
print(x + y + z)- A.
18
- B.
19
- C.
20
- D.
21
Attempted by 346 students.
Show answer & explanation
Correct answer: B
Concept: The increment operator has two forms. In post-increment (v++), the expression yields the variable's old value and the variable is increased by 1 afterward. In pre-increment (++v), the variable is increased by 1 first and the expression yields the new value. In both forms the variable's stored value permanently increases by 1; only the value handed to the surrounding expression differs.
Application: Trace each statement and keep track of the stored value of x after every line.
x = 5: store 5 inx. Now x = 5.y = x++: post-increment, soyreceives the old value y = 5, and afterwardxbecomes x = 6.z = ++x: pre-increment, soxfirst becomes x = 7, and that new value is assigned, giving z = 7.print(x + y + z)uses the current stored values: x = 7, y = 5, z = 7.
Result: x + y + z = 7 + 5 + 7 = 19.
Cross-check: Each increment raises x by 1, so after two increments x rises from 5 to 7 — it does not stay 5. The only value that is captured before an increment is y (the post-increment), which is why y alone keeps the original 5 while both x and z end at 7.