ProgrammingModerateTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[5] = {2, 4, 6, 8, 10}
for (i = 1 to 4)
a[i] = a[i] + a[i - 1]
end for
Print a- A. 2 6 10 14 18
- B. 2 4 6 8 10
- C. 2 6 12 20 28
- D. 2 6 12 20 30
Correct answer: D. 2 6 12 20 30
Each a[i − 1] has already been updated, so the loop builds running (prefix) sums: 2, 2+4, 6+6, 12+8, 20+10. Using the original neighbour each time would give 2, 6, 10, 14, 18.
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 x = 1 for (i = 1 to 4) if (i mod 2 == 1) x = x i else x = x + i end if end…
- What will be the output of the following pseudocode? Integer n = 10, s = 0 for (i = 1 to n) if (n mod i == 0) s = s + i end if end for…
- What will be the output of the following pseudocode? Integer Function f(Integer x, Integer y) if (x > y) return f(x - y, y) if (x < y)…
- What will be the output of the following pseudocode? Integer s = 0, i = 0 while (true) i = i + 1 if (i mod 4 == 0) continue s = s + i if…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Integer d1 = 0, d2 = 0, t = 0 for…
- What will be the output of the following pseudocode? Integer c = 0 Procedure rec(Integer k) c = c + 1 if (k <= 1) return rec(k / 2) rec(k…