Question 46
What is the primary condition required in a recursive function to prevent infinite recursion?
Detailed Solution
A base case provides the termination condition where the function returns without making further recursive calls.
What is the primary condition required in a recursive function to prevent infinite recursion?
A base case provides the termination condition where the function returns without making further recursive calls.
What will be the output of the recursive function call 'fun(3)'?
int fun(int n) {
if (n <= 0) return 0;
return n + fun(n - 1);
}fun(3) returns 3 + fun(2) = 3 + (2 + fun(1)) = 3 + 2 + (1 + fun(0)) = 3 + 2 + 1 + 0 = 6.
What memory structure is used by the execution environment to track active recursive function calls?
Each recursive call pushes an activation record (stack frame) onto the call stack.
What is tail recursion?
In tail recursion, the recursive call is the last statement executed, allowing tail-call optimization.
What output is generated by calling 'solve(4)'?
#include <stdio.h>
void solve(int n) {
if (n == 0) return;
solve(n - 1);
printf("%d ", n);
}The recursive call solve(n-1) happens before printing, so numbers print in ascending order during stack unwinding: 1 2 3 4.