ProgrammingHardCapgemini · Infosys pattern

What will be the output of the following pseudocode? (Matrices are indexed from 0.)

What will be the output of the following pseudocode? (Matrices are indexed from 0.)
Integer mat[3][2] = {{1, 2}, {3, 4}, {5, 6}}
Integer total = 0
for (each i from 0 to 2)
    for (each j from 0 to 1)
        total = total + mat[i][j]
    end for
end for
Print total
  1. A. 15
  2. B. 18
  3. C. 21
  4. D. 12

Correct answer: C. 21

Every one of the 6 entries of this 3x2 matrix is visited exactly once and summed: 1+2+3+4+5+6 = 21.

More Pseudocode and Code Tracing questions

Exams that ask this topic