- File I/O
- Memoization
- Iteration
Explanation:
- The correct answer is option "c". Memorization is a common optimization method used to avoid redundant calculations in the recursive Fibonacci technique.
- Reusing the results of expensive function calls when the same inputs are received again is known as memorization.
- The function may determine if the result for a given input has already been computed and stored before initiating a recursive call.
- In a recursive Fibonacci function, which of the following is a symptom of stack overflow?
- Compilation error
- Incorrect results
- Slow execution
- Program crash
Explanation:
- The correct answer is option "d". Recursive functions store information about the active function calls in the program's call stack.
- The recursion depth can become excessively deep in a recursive Fibonacci function with no base case or termination condition or with an inefficient implementation (such as one that doesn't use memoization).
- Stack overflow causes the program to crash when the recursion depth exceeds the finite call stack limit.
- Usually, this crash manifests as a runtime fault that causes the program to terminate unexpectedly.
- What will be the output of the following code?
Example
#include <stdio.h>
int fib(int num)
{
if (num <= 1)
return num;
return fib(num-1) + fib(num-2);
}
int main()
{
printf("%d", fib(4));
return 0;
}
Explanation:
- The correct answer is option "b". The nth Fibonacci number may be calculated using the recursive function fib, which is part of the given C program. The fib function returns n in base cases where n is either 0 or 1. For larger values, it returns the sum of fib(n-1) and fib(n-2). The 4th Fibonacci number is printed as a consequence of the main function's call to fib(4). When the program prints, the output is 3, which is the 4th number in the Fibonacci series.
- What does the recursive Fibonacci function's if (n = 1) return n; line purpose?
- To iterate over Fibonacci numbers
- To print Fibonacci numbers
- To handle base cases
- To add two Fibonacci numbers
Explanation:
- The correct answer is option "c". The line that checks to determine if n is 0 or 1 is if (n = 1) returns n. In this example, it doesn't make any further recursive calls because it returns n immediately, thereby giving the correct Fibonacci number.
- For the function to have specified outputs for the basic Fibonacci numbers, it is necessary to terminate the recursion.
- For the naïve recursive approach of the Fibonacci series, which optimization technique is inapplicable?
- Brute force method
- Dynamic programming
- Memoization
- Iteration
Explanation:
- The correct answer is option "a". When a problem is solved using the brute force method, all potential calculations or solutions are tried without any optimization. Since the naïve recursive method repeatedly computes the same numbers without any caching or optimization, it can be considered a brute force method in the context of the Fibonacci sequence.
- As a result, using a brute force method just describes the unoptimized naïve recursive approach and does not provide any optimization.
- What will be the output of the following code?
Example
#include <stdio.h>
int fib(int num)
{
int a = 0, b = 1, c;
if (num == 0) return a;
for (int i = 2; i <= num; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}
int main()
{
int num = 5;
printf("%d", fib(num+1));
return 0;
}
Explanation:
- The accurate choice is option "c". The included C code utilizes an iterative approach to calculate the Fibonacci number at the position num + 1. By iteratively updating the initial values of the first two Fibonacci numbers, a = 0 and b = 1, the Fibonacci sequence is computed up to the given position. The main function displays the result, and the fib function returns the Fibonacci number at the indicated position. As the 6th Fibonacci number is 8, the program correctly determines fib(6) for num = 5 as 8, resulting in an output of 8.