What will be the output of the following pseudocode? integer a, b, c; set a =…

2024

What will be the output of the following pseudocode?

integer a, b, c;
set a = 11, b = 12, c = 10;
if (b > 0)
    b++
else
    a++
end if
for (each b from 0 to 5)
    a = a + 1
end for
print (a + c)
  1. A.

    26

  2. B.

    22

  3. C.

    24

  4. D.

    20

Attempted by 2 students.

Show answer & explanation

Correct answer: A

Concept: To find the output of a pseudocode block, execute its statements strictly in the order written, updating each variable's value at every step. For a loop written as "for each X from A to B", the standard convention used across these trace-the-code items is that the loop runs (B minus A) times — i.e. the upper bound is exclusive, matching zero-based counting.

  1. Initialize a = 11, b = 12, c = 10.

  2. Evaluate if (b > 0): since b = 12 is greater than 0, the condition is true, so the b++ branch runs, not the else branch a++. This makes b = 13; a is still 11.

  3. Evaluate for (each b from 0 to 5): this reuses "b" purely as the loop counter, running for 5 iterations (0, 1, 2, 3, 4). On every iteration the statement a = a + 1 executes.

  4. After 5 iterations, a = 11 + 5 = 16.

  5. Evaluate print(a + c): a + c = 16 + 10 = 26.

Cross-check: reading the loop bound inclusively instead (0 through 5, 6 iterations) would give a = 17 and a print value of 27, but 27 is not among the offered options at all, confirming that the intended (exclusive-upper-bound) reading of 5 iterations, and therefore 26, is correct.

Explore the full course: Infosys Preparation