ProgrammingHardWipro · Cognizant pattern

What is printed?

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

Correct answer: D. 7

This builds a full binary tree of recursive calls (each level makes TWO separate calls with the same argument, n-1). f(0)=0, f(1)=0+0+1=1, f(2)=1+1+1=3, f(3)=3+3+1=7, following the pattern f(n) = 2^n - 1.

More C Programming questions

Exams that ask this topic