Q.1 What is recursion in programming?
A function calling itself directly or indirectly
A loop that executes a fixed number of times
A method to store data permanently
A function without parameters
Explanation - Recursion occurs when a function calls itself to solve smaller instances of the same problem until a base condition is met.
Correct answer is: A function calling itself directly or indirectly
Q.2 Which of the following is necessary for a recursive function to terminate?
Global variables
Base condition
Loop counter
Static variables
Explanation - A base condition ensures that recursion stops once a certain condition is met, preventing infinite recursion.
Correct answer is: Base condition
Q.3 What will be the output of the following code? function fun(n) { if (n <= 0) return 0; return n + fun(n-1); } console.log(fun(3));
6
3
0
9
Explanation - fun(3) = 3 + fun(2) = 3 + 2 + fun(1) = 3 + 2 + 1 + fun(0) = 6.
Correct answer is: 6
Q.4 Which problem is best solved using recursion?
Linear search
Binary search in a sorted array
Bubble sort
Finding the maximum element in an array
Explanation - Binary search divides the array into halves recursively, making recursion a natural fit.
Correct answer is: Binary search in a sorted array
Q.5 What is the maximum number of times a recursive function can call itself?
It depends on the system's stack memory
Exactly 100 times
Recursion cannot exceed 10 calls
Recursion has no limit
Explanation - Recursion is limited by stack size; exceeding it causes a stack overflow.
Correct answer is: It depends on the system's stack memory
Q.6 Which of the following is an example of indirect recursion?
Function A calls Function B, and Function B calls Function A
Function A calls itself
A loop inside a function
Function without parameters
Explanation - Indirect recursion occurs when functions call each other in a cycle instead of calling themselves directly.
Correct answer is: Function A calls Function B, and Function B calls Function A
Q.7 What is the output of the following factorial function for input 4? function fact(n) { if (n == 0) return 1; else return n * fact(n-1); }
24
10
4
16
Explanation - Factorial of 4 = 4 * 3 * 2 * 1 = 24.
Correct answer is: 24
Q.8 What will happen if a recursive function has no base case?
Function will execute normally
Stack overflow error
Infinite loop in main program
Nothing, program stops immediately
Explanation - Without a base case, recursion never terminates, consuming all stack memory and causing a stack overflow.
Correct answer is: Stack overflow error
Q.9 Which of the following is a tail recursion?
function f(n) { if(n==0) return 0; return f(n-1)+n; }
function f(n) { if(n==0) return 0; return n+f(n-1); }
function f(n, sum=0) { if(n==0) return sum; return f(n-1, sum+n); }
function f(n) { console.log(n); }
Explanation - Tail recursion occurs when the recursive call is the last operation in the function.
Correct answer is: function f(n, sum=0) { if(n==0) return sum; return f(n-1, sum+n); }
Q.10 In recursive algorithms, what is the purpose of the stack?
Store global variables
Store return addresses and local variables for each function call
Store files
None of the above
Explanation - Each recursive call is stored on the call stack with its parameters and local variables.
Correct answer is: Store return addresses and local variables for each function call
Q.11 Which of the following is NOT a valid recursive function?
A function that calls itself
A function that has a loop and calls itself
A function that never calls itself
A function that calls another function which calls it
Explanation - Recursion requires a function to call itself either directly or indirectly.
Correct answer is: A function that never calls itself
Q.12 What is the output of the following code? function print(n){ if(n==0) return; console.log(n); print(n-1); } print(3);
3 2 1
1 2 3
0 1 2
3 2 1 0
Explanation - The function prints n first, then calls itself with n-1 until the base case is reached.
Correct answer is: 3 2 1
Q.13 Which of the following is a classic problem solved using recursion?
Fibonacci series
Finding minimum in an array using iteration
Linear search using loops
Summing two numbers
Explanation - Fibonacci series can be naturally defined in terms of itself, making recursion suitable.
Correct answer is: Fibonacci series
Q.14 How does recursion help in solving problems?
By reducing problem size at each step
By using loops instead
By using global variables
By skipping base cases
Explanation - Recursion solves complex problems by breaking them into smaller, simpler subproblems until reaching a base case.
Correct answer is: By reducing problem size at each step
Q.15 Which of the following is an example of mutual recursion?
Function A calls B, B calls A
Function A calls A
Function A has a loop
Function A calls print
Explanation - Mutual (indirect) recursion occurs when two or more functions call each other in a cycle.
Correct answer is: Function A calls B, B calls A
Q.16 Which statement is true about recursive solutions?
They are always faster than iterative solutions
They can sometimes be less efficient due to stack overhead
They never use memory
They are always preferred over loops
Explanation - Recursive calls use stack memory; excessive recursion may be slower or cause stack overflow.
Correct answer is: They can sometimes be less efficient due to stack overhead
Q.17 What is the value of fun(2) in the code? function fun(n){ if(n==0) return 1; return n * fun(n-1); }
2
1
0
4
Explanation - fun(2) = 2 * fun(1) = 2 * 1 * fun(0) = 2 * 1 * 1 = 2
Correct answer is: 2
Q.18 What is the output of a recursive Fibonacci function for n=5?
5
8
3
2
Explanation - Fibonacci sequence: 0,1,1,2,3,5... So, 5th element is 5.
Correct answer is: 5
Q.19 Which of the following is a disadvantage of recursion?
Can cause stack overflow
Reduces code readability
Cannot solve complex problems
Uses loops internally
Explanation - Excessive recursion without termination can lead to stack overflow errors.
Correct answer is: Can cause stack overflow
Q.20 Which of these can be replaced with recursion?
Iteration
Switch-case
Variable declaration
File reading
Explanation - Loops and iterative algorithms can often be implemented using recursion.
Correct answer is: Iteration
Q.21 What is the recursive approach used in merge sort?
Divide the array into halves recursively
Loop through the array
Use only one function call
Use no recursion
Explanation - Merge sort divides arrays recursively until size 1, then merges back.
Correct answer is: Divide the array into halves recursively
Q.22 What does a recursive tree represent?
The sequence of recursive calls
The final output only
Loop iterations
Memory allocation
Explanation - A recursion tree helps visualize how recursive calls break down the problem and combine results.
Correct answer is: The sequence of recursive calls
Q.23 What is the space complexity of a simple recursive function calling itself n times?
O(n)
O(1)
O(n^2)
O(log n)
Explanation - Each recursive call adds a frame to the stack, so space complexity is linear in number of calls.
Correct answer is: O(n)
Q.24 Which of the following is a correct recursive definition of factorial?
fact(n) = n * fact(n-1), fact(0)=1
fact(n) = n + fact(n-1), fact(0)=1
fact(n) = n - fact(n-1), fact(1)=0
fact(n) = n / fact(n-1), fact(0)=1
Explanation - Factorial of n is n times factorial of n-1 with base case factorial(0)=1.
Correct answer is: fact(n) = n * fact(n-1), fact(0)=1
Q.25 Recursion is generally implemented using:
Stack
Queue
Heap
Array
Explanation - Function calls are stored in the call stack with local variables and return addresses.
Correct answer is: Stack
