ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c[0..5] = {1, 0, 0, 0, 0, 0}
for (each i from 1 to 5)
for (each j from 1 to i)
c[j] = c[j] + c[j - 1]
end for
end for
Print c[2]- A. 10
- B. 6
- C. 21
- D. 14
Correct answer: D. 14
Going forward, c[j] picks up the already-updated c[j − 1], so on pass i, c[1] becomes i and c[2] grows by that new c[1]: c[2] = 0, 2, 5, 9, 14. The usual right-to-left loop would give C(5, 2) = 10.
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? (C-style switch: a case without break falls through to the next one) Integer x = 2, s…
- What will be the output of the following pseudocode? Integer i = 1, c = 0 while (i <= 100) i = i 2 + c c = c + 1 end while Print c, i
- What will be the output of the following pseudocode? // list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, head points to node 1 Node prev = NULL, cur =…
- What will be the output of the following pseudocode? (a loop whose start is greater than its end runs zero times) Integer count = 0…
- What will be the output of the following pseudocode? Integer stars = 0 Procedure p(Integer n) stars = stars + 1 for (each i from 1 to n)…
- What will be the output of the following pseudocode? (& is bitwise AND on two's-complement integers) Integer n = 360 while (n > 0) Print n…