ProgrammingVery hardCapgemini · Infosys 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 = 3
Integer mat[3][3] = {{1, 17, 4}, {13, 2, 4}, {9, 20, 13}}
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. 31
- B. 29
- C. 28
- D. 30
Correct answer: B. 29
Summing both diagonals of this 3×3 matrix: main diagonal = 16, anti-diagonal = 15, minus the centre cell (2) counted twice, giving 29. Simply adding both sums without removing the double-counted centre gives the trap 31.
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 4. 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 16) for (each j from i + 1 to 16) if ((i + j) mod…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift.) Integer n = 2169, 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[8] = {45…