ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int calls = 0;
int fib(int n) {
    calls++;
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2);
}
int main(void) { int r = fib(5); printf("%d %d", r, calls); }
  1. A. 5 11
  2. B. 8 15
  3. C. 5 9
  4. D. 5 15

Correct answer: D. 5 15

fib(5)=5 (standard Fibonacci: 0,1,1,2,3,5). The naive recursive fib makes 2*fib(n+1)-1 calls; for n=5 that's 2*fib(6)-1 = 2*8-1=15 calls total (counting every invocation including the base cases).

More C Programming questions

Exams that ask this topic