ProgrammingHardInfosys · Capgemini pattern

What will be the output of the following pseudocode?

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) + h(n - 2)
    return 2 * h(n - 1)
End Function
Print h(6)
  1. A. 32
  2. B. 64
  3. C. 24
  4. D. 36

Correct answer: D. 36

h(0) = h(−1) = 1; h(1) = 1 + 1 = 2; h(2) = 4; h(3) = 4 + 2 = 6; h(4) = 12; h(5) = 12 + 6 = 18; h(6) = 36. Note h(−1) also hits the base case.

More Pseudocode and Code Tracing questions

Exams that ask this topic