ProgrammingModerateAccenture · 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}, {2, 5, 9}, {4, 6, 0}}
Integer bad = 0
for (each i from 0 to 2)
    for (each j from i + 1 to 2)
        if (m[i][j] != m[j][i])
            bad = bad + 1
        end if
    end for
end for
Print bad
  1. A. 4
  2. B. 3
  3. C. 1
  4. D. 2

Correct answer: D. 2

Only the 3 pairs above the diagonal are checked: (0,1): 2 = 2; (0,2): 3 ≠ 4; (1,2): 9 ≠ 6. bad = 2. Checking every (i, j) would count each mismatch twice (4).

More Pseudocode and Code Tracing questions

Exams that ask this topic