ProgrammingHardTCS NQT · Infosys
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[4][4], i, j, s = 0
for (i = 0; i < 4; i = i + 1)
for (j = 0; j < 4; j = j + 1)
a[i][j] = i * j + 1
end for
end for
for (i = 0; i < 4; i = i + 1)
s = s + a[i][i] + a[i][3 - i]
end for
Print s- A. 18
- B. 26
- C. 28
- D. 25
Correct answer: B. 26
Main diagonal 1, 2, 5, 10 sums to 18; the anti-diagonal a[0][3], a[1][2], a[2][1], a[3][0] = 1, 3, 3, 1 sums to 8; total 26.
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? Integer calls = 0 Function F(Integer n) calls = calls + 1 if (n < 2) return n end if…
- What will be the output of the following pseudocode? List L = [1, 2, 3, 4, 5, 6, 7] Integer idx = 0, k = 3 while (size(L) > 1) idx = (idx…
- What will be the output of the following pseudocode? Function gcd(Integer a, Integer b) while (b != 0) t = a mod b a = b b = t end while…
- What will be the output of the following pseudocode? String s = "{[()]}(()" Stack st Integer pops = 0, i Boolean ok = true for (i = 0; i <…
- What will be the output of the following pseudocode? Integer a[5] = {2, 5, 9, 14, 20}, i for (i = 1; i < 5; i = i + 1) a[i] = a[i] - a[i…
- What will be the output of the following pseudocode? Integer x = 0, i for (i = 1; i <= 10; i = i + 1) x = ((x << 1) ^ i) & 15 end for…