ProgrammingModerateAccenture · Cognizant pattern

How many times is fib called in total (including the first call) when evaluating fib(5)?

How many times is fib called in total (including the first call) when evaluating fib(5)?
int fib(int n) {
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2);
}
  1. A. 25
  2. B. 5
  3. C. 15
  4. D. 9

Correct answer: C. 15

Calls 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. 5 is the value fib(5) returns, not the number of calls.

More C Programming questions

Exams that ask this topic