ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode (summing positive and negative matrix entries separately)?

What will be the output of the following pseudocode (summing positive and negative matrix entries separately)?
Integer mat[3][3] = {{1, -2, 3}, {4, 5, -6}, {-7, 8, 9}}
Integer pos = 0, neg = 0
for (each row in mat)
    for (each v in row)
        if (v > 0)
            pos = pos + v
        else
            neg = neg + v
        end if
    end for
end for
Print pos, neg
  1. A. 30 -15
  2. B. 45 -15
  3. C. 30 -30
  4. D. 15 -30

Correct answer: A. 30 -15

Positive entries (1,3,4,5,8,9) sum to 30. Non-positive entries (-2,-6,-7) sum to -15. Printed as pos, neg: 30 -15.

More Pseudocode and Code Tracing questions

Exams that ask this topic