ProgrammingModerateAccenture · Cognizant GenC Next

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer m[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}
for (each i from 0 to 2)
    if (i mod 2 == 0)
        for (each j from 0 to 3) Print m[i][j]
    else
        for (each j from 3 down to 0) Print m[i][j]
    end if
end for
  1. A. 1 2 3 4 5 6 7 8 9 10 11 12
  2. B. 1 2 3 4 8 7 6 5 9 10 11 12
  3. C. 4 3 2 1 5 6 7 8 12 11 10 9
  4. D. 1 5 9 10 6 2 3 7 11 12 8 4

Correct answer: B. 1 2 3 4 8 7 6 5 9 10 11 12

Even rows go left to right and odd rows right to left (a snake order): 1 2 3 4, then 8 7 6 5, then 9 10 11 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic