ProgrammingVery hardTCS 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] = {{8, 1, 2, 5, 12}, {14, 18, 13, 7, 1}, {19, 4, 11, 16, 1}, {7, 5, 15, 15, 4}, {20, 15, 16, 5, 4}}
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. 101
- B. 100
- C. 111
- D. 99
Correct answer: B. 100
Summing both diagonals of this 5×5 matrix: main diagonal = 56, anti-diagonal = 55, minus the centre cell (11) counted twice, giving 100. Simply adding both sums without removing the double-counted centre gives the trap 111.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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.) Integer x, y Set x = 17, y =…
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 10) if (i mod 2 == 0) s = s + i end if end for…
- What will be the output of the following pseudocode? Integer a = 6, b = 4 a = a + b b = a - b a = a - b Print a, b
- What will be the output of the following pseudocode? Integer n = 5, f = 1 while (n > 1) f = f n n = n - 1 end while Print f
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[4] = {4, 9, 2, 7} Integer m = arr[0] for…