ProgrammingModerateAccenture · Cognizant 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 4)
    for (each j from i to 4)
        c = c + 1
    end for
end for
Print c
  1. A. 4
  2. B. 16
  3. C. 10
  4. D. 6

Correct answer: C. 10

The inner loop runs 4, 3, 2 and 1 times: 4 + 3 + 2 + 1 = 10. Running the full 4 × 4 = 16 is the trap.

More Pseudocode and Code Tracing questions

Exams that ask this topic