ProgrammingHardAmazon OA · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer g[3][4] = {{1, 1, 1, 0}, {1, 1, 1, 1}, {0, 1, 1, 1}}
Integer c = 0
for (each i from 0 to 1)
    for (each j from 0 to 2)
        if (g[i][j] + g[i][j + 1] + g[i + 1][j] + g[i + 1][j + 1] == 4)
            c = c + 1
        end if
    end for
end for
Print c
  1. A. 6
  2. B. 5
  3. C. 4
  4. D. 2

Correct answer: C. 4

There are 2 × 3 = 6 possible 2 × 2 blocks. Those made entirely of 1's: top row at columns 0–1 and 1–2; bottom row at columns 1–2 and 2–3. c = 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic