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
  1. A. 10 7
  2. B. 8 13
  3. C. 7 10
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic