ProgrammingHardTCS NQT · Infosys pattern

What is printed?

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

Correct answer: D. 8

f(0)=1. f(1)=f(0)+f(0)=2. f(2)=f(1)+f(1)=4. f(3)=f(2)+f(2)=8. Each level doubles, giving 2^n for f(n), so f(3)=8.

More C Programming questions

Exams that ask this topic