Data Structure Questions and Answers-Fibonacci using Recursion

YOU CAN DOWNLOAD 200+ SUBJECTS PDF BOOK FOR COMPETITIVE EXAMINATIONS

CLICK HERE TO DOWNLOAD

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?

A
fibo(n - 1)
B
fibo(n - 1) + fibo(n - 2)
C
fibo(n) + fibo(n - 1)
D
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?

A
if(n == 1)
B
else if(n == 2)
C
return fibo(n - 1) + fibo(n - 2)
D
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?
A
O(1)
B
O(2*n)
C
O(n2)
D
O(2n)
Question 8 Explanation: 
The time complexity of the above recursive implementation to find the nth fibonacci number is O(2n).

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?
A
O(1)
B
O(2*n)
C
O(n2)
D
O(2n)
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; }
A
0
B
1
C
Compile time error
D
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.