ProgrammingVery hardTCS NQT · Infosys · Capgemini

What will be the output of the following pseudocode? (& is bitwise AND; arrays are 0-indexed.)

What will be the output of the following pseudocode? (& is bitwise AND; arrays are 0-indexed.)
Integer m[4][4]
For i = 0 to 3
    For j = 0 to 3
        m[i][j] = (i * 3 + j * 2) mod 5
    End For
End For
Integer s = 0
For i = 0 to 3
    For j = 0 to 3
        If (i & j) = 0 Then s = s + m[i][j]
    End For
End For
Print s
  1. A. 0
  2. B. 10
  3. C. 13
  4. D. 20

Correct answer: D. 20

The cells with i AND j = 0 are all of row 0, plus (1,0), (1,2), (2,0), (2,1) and (3,0): nine cells whose values 0, 2, 4, 1, 3, 2, 1, 3, 4 add up to 20.

More Pseudocode and Code Tracing questions

Exams that ask this topic