ProgrammingModerateAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer v = 0
for (i = 1 to 5)
    v = (v << 1) | (i & 1)
end for
Print v
  1. A. 31
  2. B. 10
  3. C. 21
  4. D. 11

Correct answer: C. 21

Each step shifts v left and appends the lowest bit of i: 1, 0, 1, 0, 1 → 10101₂ = 21. Using + instead of | gives the same here because the new bit is always free.

More Pseudocode and Code Tracing questions

Exams that ask this topic