ProgrammingModerateAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 3)
    for (each j from 1 to 3)
        if (i == j)
            continue
        end if
        s = s + i * j
    end for
end for
Print s
  1. A. 11
  2. B. 14
  3. C. 22
  4. D. 36

Correct answer: C. 22

Over all 9 pairs, the sum of i × j is (1 + 2 + 3)² = 36. The skipped diagonal is 1 + 4 + 9 = 14. 36 - 14 = 22. Each unordered pair is counted twice, so 11 is the trap.

More Pseudocode and Code Tracing questions

Exams that ask this topic