ProgrammingHardCapgemini · Cognizant GenC Next

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}}
for (each i from 0 to 2)
    for (each j from 0 to 2)
        swap(m[i][j], m[j][i])
    end for
end for
Print m[0][1], m[1][0], m[2][0]
  1. A. 2 4 7
  2. B. 4 2 3
  3. C. 4 2 7
  4. D. 2 4 3

Correct answer: A. 2 4 7

Because j runs over the whole row, every off-diagonal pair is swapped twice (once at (i, j) and again at (j, i)), so the matrix ends up unchanged: 2 4 7. A real transpose needs j to start from i + 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic