ProgrammingHardAccenture · TCS · Infosys
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 = 3
Integer mat[3][3] = {{17, 6, 8}, {13, 9, 17}, {18, 7, 17}}
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. 68
- B. 69
- C. 78
- D. 70
Correct answer: B. 69
Summing both diagonals of this 3×3 matrix: main diagonal = 43, anti-diagonal = 35, minus the centre cell (9) counted twice, giving 69. Simply adding both sums without removing the double-counted centre gives the trap 78.
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 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…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift.) Integer n = 845, c = 0 while (n > 0) c = c +…
- What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack? push…