# YOU CAN DOWNLOAD 200+ SUBJECTS PDF BOOK FOR COMPETITIVE EXAMINATIONS

## Data Structure Questions and Answers-Fibonacci using Recursion

Question 6 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

Consider the following recursive implementation to find the nth fibonacci number:

int fibo(int n) { if(n == 1) return 0; else if(n == 2) return 1; return .....; } int main() { int n = 5; int ans = fibo(n); printf("%d", ans); return 0; }

Which of the following lines should be inserted to complete the above code?

fibo(n - 1) | |

fibo(n - 1) + fibo(n - 2) | |

fibo(n) + fibo(n - 1) | |

fibo(n - 2) + fibo(n - 1) |

Question 6 Explanation:

The line fibo(n - 1) + fibo(n - 2) should be inserted to complete the above code.

Question 7 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

Consider the following recursive implementation to find the nth fibonnaci number:

int fibo(int n) { if(n == 1) return 0; else if(n == 2) return 1; return fibo(n - 1) + fibo(n - 2); } int main() { int n = 5; int ans = fibo(n); printf("%d", ans); return 0; }

Which of the following is the base case?

if(n == 1) | |

else if(n == 2) | |

return fibo(n - 1) + fibo(n - 2) | |

both if(n == 1) and else if(n == 2) |

Question 7 Explanation:

Both if(n == 1) and else if(n == 2) are the base cases.

Question 8 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

What is the time complexity of the above recursive implementation to find the nth fibonacci number?

O(1) | |

O(2*n) | |

O(n ^{2}) | |

O(2 ^{n}) |

Question 8 Explanation:

The time complexity of the above recursive implementation to find the nth fibonacci number is O(2

^{n}).Question 9 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

What is the space complexity of the above recursive implementation to find the nth fibonacci number?

O(1) | |

O(2*n) | |

O(n ^{2}) | |

O(2 ^{n}) |

Question 9 Explanation:

The space complexity of the above recursive implementation to find the nth fibonacci number is O(1).

Question 10 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

What is the output of the following code?

int fibo(int n) { if(n == 1) return 0; else if(n == 2) return 1; return fibo(n - 1) + fibo(n - 2); } int main() { int n = -1; int ans = fibo(n); printf("%d", ans); return 0; }

0 | |

1 | |

Compile time error | |

Runtime error |

Question 10 Explanation:

Since negative numbers are not handled by the program, the function fibo() will be called infinite times and the program will produce a runtime error when the stack overflows.

There are 10 questions to complete.