ProgrammingHardCapgemini · 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] = {{7, 17, 13}, {12, 6, 8}, {15, 18, 5}}
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. 46
- B. 47
- C. 52
- D. 45
Correct answer: A. 46
Summing both diagonals of this 3×3 matrix: main diagonal = 18, anti-diagonal = 34, minus the centre cell (6) counted twice, giving 46. Simply adding both sums without removing the double-counted centre gives the trap 52.
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 6 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 11) for (each j from i + 1 to 11) if ((i + j) mod…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift.) Integer n = 3784, 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] = {49…