ProgrammingHardAmazon OA · Capgemini

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

Correct answer: D. 1 2 4 3 5 7 6 8 9

Cells are visited by anti-diagonal (i + j = d), and within each anti-diagonal by increasing row i: {1}, {2, 4}, {3, 5, 7}, {6, 8}, {9}. Output 1 2 4 3 5 7 6 8 9.

More Pseudocode and Code Tracing questions

Exams that ask this topic