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
for (i = 1; i <= 100; i = i * 2)
    for (j = i; j <= 100; j = j + 1)
        c = c + 1
    end for
end for
Print c
  1. A. 707
  2. B. 127
  3. C. 580
  4. D. 600

Correct answer: C. 580

i takes the values 1, 2, 4, 8, 16, 32, 64 (128 > 100), seven values. For each, the inner loop runs 101 − i times. Total = 7 × 101 − (1 + 2 + … + 64) = 707 − 127 = 580. 707 forgets to subtract the starting values; 127 sums the i values themselves.

More Pseudocode and Code Tracing questions

Exams that ask this topic