ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Integer d1 = 0, d2 = 0, t = 0
for (i = 0 to 2)
    d1 = d1 + m[i][i]
    d2 = d2 + m[i][2 - i]
    for (j = 0 to 2)
        if (j > i) t = t + m[i][j]
    end for
end for
Print d1, d2, t
  1. A. 15 15 26
  2. B. 15 15 11
  3. C. 15 13 11
  4. D. 15 15 19

Correct answer: B. 15 15 11

Main diagonal 1 + 5 + 9 = 15, anti-diagonal 3 + 5 + 7 = 15, strictly upper triangle (j > i) 2 + 3 + 6 = 11. Using j ≥ i would add the diagonal too (26); the lower triangle would be 4 + 7 + 8 = 19.

More Pseudocode and Code Tracing questions

Exams that ask this topic