ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
int main(void) {
printf("%d", fib(6));
}- A. 13
- B. 5
- C. 21
- D. 8
Correct answer: D. 8
Standard Fibonacci (0,1,1,2,3,5,8,...): fib(6) is the 7th term (0-indexed), which is 8.
More C Programming questions
- What is printed? c int f(int n) { if (n == 0) return 0; printf("%d ", n); if (n % 2 == 0) return f(n / 2); return f(n - 1); } int…
- How many TOTAL calls to fib (including the initial call) are made evaluating fib(4), given this naive recursive implementation? c int…
- What is printed? (recursion where the recursive call's result is deliberately ignored) c int calls = 0; int f(int n) { calls++; if (n <=…
- What is printed? (recursion depth vs. the array being modified on the way back up) c int a[5] = {1, 2, 3, 4, 5}; void f(int i) { if (i >=…
- What is printed? (recursion with an early return skipping the rest of the function) c int f(int n) { if (n < 0) return -1; if (n == 0)…
- What is printed? (binary string of n using recursion, division by 2) c void toBinary(int n) { if (n == 0) return; toBinary(n / 2)…