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] = {{1, 7, 1, 14, 10}, {8, 11, 9, 8, 6}, {8, 17, 10, 17, 18}, {16, 2, 6, 16, 19}, {3, 1, 16, 13, 16}}
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. 77
- B. 87
- C. 78
- D. 76
Correct answer: A. 77
Summing both diagonals of this 5×5 matrix: main diagonal = 54, anti-diagonal = 33, minus the centre cell (10) counted twice, giving 77. Simply adding both sums without removing the double-counted centre gives the trap 87.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 5. What does each of the 4 calls print, in…
- 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…
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 2. What does each of the 4 calls print, in…
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…
- 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? Integer c = 0 for (each i from 1 to 14) for (each j from i + 1 to 14) if ((i + j) mod…