ProgrammingVery hardAccenture · Cognizant pattern
What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If n is odd, the centre cell belongs to both and must not be counted twice.)
What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If n is odd, the centre cell belongs to both and must not be counted twice.)
Integer n = 5
Integer mat[5][5] = {{12, 3, 8, 15, 15}, {8, 11, 7, 19, 17}, {14, 6, 18, 16, 16}, {11, 1, 6, 1, 5}, {8, 3, 12, 14, 1}}
Integer total = 0
for (each i from 0 to n - 1)
total = total + mat[i][i]
if (i != n - 1 - i)
total = total + mat[i][n - 1 - i]
end if
end for
Print total- A. 85
- B. 87
- C. 104
- D. 86
Correct answer: D. 86
Summing both diagonals of this 5×5 matrix: main diagonal = 43, anti-diagonal = 61, minus the centre cell (18) counted twice, giving 86. Simply adding both sums without removing the double-counted centre gives the trap 104.
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? (All variables are integers, so / is integer division.) Function f(Integer n, Integer…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift.) Integer n = 3500, c = 0 while (n > 0) c = c +…
- What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?…
- What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array…
- What will be the output of the following pseudocode? (^ is bitwise XOR.) Integer x = 25, y = 31 x = x ^ y y = x ^ y x = x ^ y Print x, y
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…