ProgrammingModerateWipro · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function f(Integer n)
    if (n <= 1)
        return 1
    end if
    return f(n - 1) + 2 * f(n - 2)
End Function
Print f(5)
  1. A. 11
  2. B. 21
  3. C. 43
  4. D. 15

Correct answer: B. 21

Build up from the base cases f(0) = f(1) = 1: f(2) = 1 + 2 = 3, f(3) = 3 + 2 = 5, f(4) = 5 + 6 = 11, f(5) = 11 + 10 = 21. 11 is f(4); 43 is f(6).

More Pseudocode and Code Tracing questions

Exams that ask this topic