ProgrammingVery hardProduct company OA

What is printed?

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

Correct answer: B. 9

Counting all invocations of f(4): f(4)->f(3),f(2); f(3)->f(2),f(1); f(2)->f(1),f(0); each of these base calls (n<=1) also counts. Total invocations: f(4),f(3),f(2),f(1),f(0),f(2),f(1),f(0),f(1) = 9 calls.

More C Programming questions

Exams that ask this topic