ProgrammingVery hardTCS NQT · Accenture 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 12)
    for (each j from i + 1 to 12)
        if ((i + j) mod 5 == 0)
            c = c + 1
        end if
    end for
end for
Print c
  1. A. 15
  2. B. 12
  3. C. 14
  4. D. 13

Correct answer: D. 13

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

More Pseudocode and Code Tracing questions

Exams that ask this topic