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 14)
    for (each j from i + 1 to 14)
        if ((i + j) mod 5 == 0)
            c = c + 1
        end if
    end for
end for
Print c
  1. A. 21
  2. B. 19
  3. C. 18
  4. D. 20

Correct answer: B. 19

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

More Pseudocode and Code Tracing questions

Exams that ask this topic