ProgrammingHardGATE CS · Campus placements
int f(int n) { return n < 2 ? 1 : f(n - 1) + f(n - 2); } How many calls to f happen in total, counting the first call, when evaluating f(5)?
- A. 9
- B. 11
- C. 13
- D. 15
Correct answer: D. 15
Let c(n) be the call count. c(0) = c(1) = 1 and c(n) = 1 + c(n-1) + c(n-2), so c(2)=3, c(3)=5, c(4)=9, c(5)=15. This explosion is why naive recursion is exponential.
More C Programming questions
- A recursive Tower of Hanoi solution moves n disks, one at a time, with the rule that a larger disk never sits on a smaller one. How many…
- The Ackermann function is defined as A(0,n) = n+1, A(m,0) = A(m-1,1), and A(m,n) = A(m-1, A(m,n-1)) for m, n > 0. What is A(2,3)?
- int a[5] = {10, 20, 30, 40, 50}; int p = a + 1, q = a + 4; printf("%td %d", q - p, (p + 2)); prints what?
- void f(int a[10]) { printf("%zu", sizeof a); } Which statement about the result is correct?
- int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (p)[3] = m; printf("%d", ((p + 1) + 2)); prints what?
- Which statement about sizeof is correct when x is an int variable initialised to 5?