ProgrammingHardInfosys · Wipro

What is printed?

What is printed?
#include <stdio.h>
int calls = 0;
int f(int n) {
    calls++;
    if (n < 2) return n;
    return f(n - 1) + f(n - 2);
}
int main(void) {
    int r = f(6);
    printf("%d %d", r, calls);
}
  1. A. 8 15
  2. B. 13 25
  3. C. 8 25
  4. D. 8 24

Correct answer: C. 8 25

f(6) returns fib(6) = 8. Every call costs 1 + calls(n-1) + calls(n-2) with calls(0) = calls(1) = 1, giving 25 calls in total; counting only the internal nodes or using fib(7) gives the traps.

More C Programming questions

Exams that ask this topic