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- A. 30 -15
- B. 45 -15
- C. 30 -30
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (counting distinct pairs whose difference is k)? (Arrays are indexed from 0.) Integer…
- What will be the output of the following pseudocode (2x2 matrix multiplication)? Integer A[2][2] = {{1, 2}, {3, 4}} Integer B[2][2] = {{5…
- What will be the output of the following pseudocode (binary string to decimal)? String bits = "110101" Integer val = 0 for (each b in…
- What will be the output of the following pseudocode (finding the 1-based position of the rightmost set bit)? (All variables are integers…
- What will be the output of the following pseudocode (checking if s3 is an interleaving of s1 and s2, via dynamic programming)? Function…
- What will be the output of the following pseudocode (counting pairs (i,j) with i<=j<=n whose product is divisible by n)? Integer n = 12, c…