ProgrammingHardTCS NQT · Infosys

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[4][4], i, j, s = 0
for (i = 0; i < 4; i = i + 1)
    for (j = 0; j < 4; j = j + 1)
        a[i][j] = i * j + 1
    end for
end for
for (i = 0; i < 4; i = i + 1)
    s = s + a[i][i] + a[i][3 - i]
end for
Print s
  1. A. 18
  2. B. 26
  3. C. 28
  4. D. 25

Correct answer: B. 26

Main diagonal 1, 2, 5, 10 sums to 18; the anti-diagonal a[0][3], a[1][2], a[2][1], a[3][0] = 1, 3, 3, 1 sums to 8; total 26.

More Pseudocode and Code Tracing questions

Exams that ask this topic