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 5)
    for (each j from 1 to 5)
        if (j > i)
            break
        end if
        c = c + 1
    end for
end for
Print c
  1. A. 25
  2. B. 5
  3. C. 10
  4. D. 15

Correct answer: D. 15

break leaves only the inner loop, so for each i the inner loop counts j = 1 to i: 1 + 2 + 3 + 4 + 5 = 15. Thinking break ends both loops gives 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic