ProgrammingVery hardProduct company OA · code-tracing level

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0, i
for (i = 1; i <= 50; i = i + 1)
    i = i * 2
    c = c + 1
end for
Print c, i
  1. A. 50 51
  2. B. 6 127
  3. C. 5 63
  4. D. 5 62

Correct answer: C. 5 63

The body doubles i and then the loop adds 1, so i runs 1 → 2 → 3, 3 → 6 → 7, 7 → 14 → 15, 15 → 30 → 31, 31 → 62 → 63. After the fifth pass i = 63 > 50, the test fails, and the loop ends with c = 5 and i = 63. '5 62' forgets the final increment, which happens before the test.

More Pseudocode and Code Tracing questions

Exams that ask this topic