ProgrammingHardTCS NQT · Accenture 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] = {{10, 10, 10, 13, 11}, {18, 8, 14, 5, 19}, {10, 14, 17, 4, 11}, {7, 10, 15, 1, 16}, {15, 1, 11, 18, 8}}
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. 86
- B. 85
- C. 84
- D. 102
Correct answer: B. 85
Summing both diagonals of this 5×5 matrix: main diagonal = 44, anti-diagonal = 58, minus the centre cell (17) counted twice, giving 85. Simply adding both sums without removing the double-counted centre gives the trap 102.
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 3. What does each of the 7 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? Integer c = 0 for (each i from 1 to 12) for (each j from i + 1 to 12) if ((i + j) mod…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift.) Integer n = 1958, c = 0 while (n > 0) c = c +…
- What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.) Integer arr[6] = {89…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division. Array is sorted and indexed…