ProgrammingModerateInfosys · Capgemini pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a = 1, b = 1, t
for (k = 1 to 5)
t = a + b
a = b
b = t
if (b mod 2 == 0) swap(a, b)
end for
Print a, b- A. 10 7
- B. 8 13
- C. 7 10
- D. 13 21
Correct answer: A. 10 7
Step by step (a, b): (1, 2) → swap → (2, 1); (1, 3); (3, 4) → swap → (4, 3); (3, 7); (7, 10) → swap → (10, 7). The swaps break the Fibonacci pattern, so (8, 13) is the trap.
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 Function f(Integer n, Integer acc) if (n == 0) return acc return f(n - 1…
- What will be the output of the following pseudocode? Integer x = 100, c = 0 while (x > 1) if (x mod 2 == 0) x = x / 2 else x = 3 x + 1 c =…
- What will be the output of the following pseudocode? Integer a[] = {1, 3, 5, 7, 9, 11, 13} Integer lo = 0, hi = 6, t = 11, steps = 0, mid…
- What will be the output of the following pseudocode? Integer x = 15, y = 4, r = 0 while (x >= y) x = x - y r = r + 1 end while Print r, x
- What will be the output of the following pseudocode? Integer a = 1, b = 2, c = 3 a = (a < b) ? b : c b = (b < c AND a == 2) ? c : a c = (a…
- What will be the output of the following pseudocode? Integer Function h(Integer n) if (n <= 0) return 1 if (n mod 2 == 1) return h(n - 1)…