ProgrammingHardCapgemini · Infosys pattern

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Integer n = 16, c = 0, i = n
while (i >= 1)
    for (each j from 0 to i - 1)
        c = c + 1
    end for
    i = i / 2
end while
Print c
  1. A. 31
  2. B. 32
  3. C. 16
  4. D. 15

Correct answer: A. 31

i takes values 16, 8, 4, 2, 1 (then i=0 stops the loop), and the inner loop runs i times each pass: 16+8+4+2+1 = 31.

More Pseudocode and Code Tracing questions

Exams that ask this topic