ProgrammingHardAccenture · Cognizant pattern

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 < 64; i = i * 2)
    for (j = i; j < 64; j = j + i)
        c = c + 1
    end for
end for
Print c
  1. A. 120
  2. B. 127
  3. C. 63
  4. D. 126

Correct answer: A. 120

i takes the values 1, 2, 4, 8, 16, 32. For each, j runs over the multiples of i below 64, which is (64/i) - 1 values: 63 + 31 + 15 + 7 + 3 + 1 = 120. Allowing j up to 64 gives 64/i values and the trap 126.

More Pseudocode and Code Tracing questions

Exams that ask this topic