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)

Answer: B. 19Concept: 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…

  1. A.

    18

  2. B.

    19

  3. C.

    20

  4. D.

    21

Attempted by 560 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.

  1. x = 5: store 5 in x. Now x = 5.

  2. y = x++: post-increment, so y receives the old value y = 5, and afterward x becomes x = 6.

  3. z = ++x: pre-increment, so x first becomes x = 7, and that new value is assigned, giving z = 7.

  4. 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.

Explore the full course: Tpsc Assistant Technical Officer

Loading lesson…