ProgrammingHardAccenture · TCS · Infosys

What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If n is odd, the centre cell belongs to both and must not be counted twice.)

What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If n is odd, the centre cell belongs to both and must not be counted twice.)
Integer n = 3
Integer mat[3][3] = {{17, 6, 8}, {13, 9, 17}, {18, 7, 17}}
Integer total = 0
for (each i from 0 to n - 1)
    total = total + mat[i][i]
    if (i != n - 1 - i)
        total = total + mat[i][n - 1 - i]
    end if
end for
Print total
  1. A. 68
  2. B. 69
  3. C. 78
  4. D. 70

Correct answer: B. 69

Summing both diagonals of this 3×3 matrix: main diagonal = 43, anti-diagonal = 35, minus the centre cell (9) counted twice, giving 69. Simply adding both sums without removing the double-counted centre gives the trap 78.

More Pseudocode and Code Tracing questions

Exams that ask this topic