ProgrammingModerateProduct-company MCQ pattern

What is printed?

What is printed?
calls = 0
def f(n):
    global calls
    calls += 1
    return 1 if n <= 1 else f(n - 1) + f(n - 2)

print(f(5), calls)
  1. A. 8 8
  2. B. 8 15
  3. C. 8 9
  4. D. 5 15

Correct answer: B. 8 15

f(0) = f(1) = 1, so f(5) = 8. The call count follows C(n) = 1 + C(n-1) + C(n-2) with C(0) = C(1) = 1: C(2) = 3, C(3) = 5, C(4) = 9, C(5) = 15.

More Python Programming questions

Exams that ask this topic