ProgrammingHardWipro · TCS NQT pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0
for (each i from 1 to 16)
    for (each j from i + 1 to 16)
        if ((i + j) mod 4 == 0)
            c = c + 1
        end if
    end for
end for
Print c
  1. A. 28
  2. B. 36
  3. C. 29
  4. D. 27

Correct answer: A. 28

The inner loop runs over strictly j > i (unordered pairs), counting pairs whose sum is a multiple of 4. Tracing i from 1 to 16 gives exactly 28 such pairs. Starting j at i instead of i+1 (also counting i+i) gives the trap 36.

More Pseudocode and Code Tracing questions

Exams that ask this topic