ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode? (Arrays are indexed from 0.)

What will be the output of the following pseudocode? (Arrays are indexed from 0.)
Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Integer s = 0
for (each i from 0 to 2)
    for (each j from 0 to 2)
        if (i + j == 2 || i == j)
            s = s + m[i][j]
        end if
    end for
end for
Print s
  1. A. 15
  2. B. 25
  3. C. 20
  4. D. 30

Correct answer: B. 25

Main diagonal 1 + 5 + 9 = 15 and anti-diagonal 3 + 5 + 7 = 15. The centre 5 meets both conditions but is added only once, because it is a single if with ||. 30 - 5 = 25.

More Pseudocode and Code Tracing questions

Exams that ask this topic