ProgrammingHardInfosys · Capgemini pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer i = 1, j, c = 0
while (i <= 16)
    j = i
    while (j <= 16)
        c = c + 1
        j = j * 2
    end while
    i = i * 2
end while
Print c
  1. A. 15
  2. B. 25
  3. C. 16
  4. D. 10

Correct answer: A. 15

i takes 1, 2, 4, 8, 16; for each, j doubles from i up to 16: 5 + 4 + 3 + 2 + 1 = 15 iterations. In general this is O(log² n), not O(n).

More Pseudocode and Code Tracing questions

Exams that ask this topic