ProgrammingHardAmazon OA · Infosys SP

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

Correct answer: D. 7 4 1

A transpose followed by reversing each row rotates the matrix 90° clockwise. The transposed first row 1 4 7 becomes 7 4 1. Reversing the columns instead gives the anticlockwise result 3 6 9.

More Pseudocode and Code Tracing questions

Exams that ask this topic