ProgrammingVery hardProduct company OA

What is printed? (recursion where the recursive call's result is deliberately ignored)

What is printed? (recursion where the recursive call's result is deliberately ignored)
int calls = 0;
int f(int n) {
    calls++;
    if (n <= 0) return 0;
    f(n - 1);
    return calls;
}
int main(void) {
    printf("%d", f(4));
}
  1. A. 4
  2. B. 5
  3. C. 10
  4. D. 1

Correct answer: B. 5

f is called for n=4,3,2,1,0 — five invocations total, each incrementing the global calls once (even though most of their return values are discarded by the caller). By the time the outermost call returns, calls is 5.

More C Programming questions

Exams that ask this topic