/* */ Click to Join Live Class with Shankar sir Call 9798158723

Data Structure MCQ - Recursion


Q1.Recursion is a method in which the solution of a problem depends on .....
  1. Larger instances of different problems
  2. Larger instances of the same problem
  3. Smaller instances of the same problem
  4. Smaller instances of different problems

Answer:- (C).
Explanations :In recursion, the solution of a problem depends on the solution of smaller instances of the same problem.
Q2.Which of the following problems can be solved using recursion?
  1. Factorial of a number
  2. Nth fibonacci number
  3. Length of a string
  4. All of the mentioned

Answer:- (D).
Explanations :All of the above mentioned problems can be solved using recursion.
Q3.What will happen when the above snippet is executed?
  1. The code will be executed successfully and no output will be generated
  2. The code will be executed successfully and random output will be generated
  3. The code will show a compile time error
  4. The code will run for some time and stop when the stack overflows

Answer:- (D).
Explanations :Every function call is stored in the stack memory. In this case, there is no terminating condition(base case). So, my....recursive....function() will be called continuously till the stack overflows and there is no more space to store the function calls. At this point of time, the program will stop abruptly.
Q4.What is the base case for the following code?
void my....recursive....function(int n) { if(n == 0) return; printf("%d ", n); my....recursive....function(n-1); } int main() { my....recursive....function(10); return 0; }
  1. return
  2. printf("%d ", n)
  3. if(n == 0)
  4. my....recursive....function(n-1)

Answer:- (C).
Explanations :For the base case, the recursive function is not called. So, "if(n == 0)" is the base case.
Q5.Recursion is similar to which of the following?
  1. Switch Case
  2. Loop
  3. If-else
  4. if elif else

Answer:- (B).
Explanations :Recursion is similar to a loop.
Q6.In recursion, the condition for which the function will stop calling itself is ____________
  1. Best case
  2. Worst case
  3. Base case
  4. There is no such condition

Answer:- (C).
Explanations :For recursion to end at some point, there always has to be a condition for which the function will not call itself. This condition is known as base case.
Q7.What will happen when the below code snippet is executed?
void my_recursive_function(int n)
{
    if(n == 0)
    return;
    printf("%d ",n);
    my_recursive_function(n-1);
}
int main()
{
    my_recursive_function(10);
    return 0;
}
  1. 10
  2. 1
  3. 10 9 8 … 1 0
  4. 10 9 8 … 1

Answer:- (D).
Explanations :The program prints the numbers from 10 to 1.
Q8.What will be the output of the following code?
int cnt=0;
void my_recursive_function(int n)
{
     if(n == 0)
     return;
     cnt++;
     my_recursive_function(n/10);
}
int main()
{
     my_recursive_function(123456789);
     printf("%d",cnt);
     return 0;
}
  1. 123456789
  2. 10
  3. 0
  4. 9

Answer:- (D).
Explanations :The program prints the number of digits in the number 123456789, which is 9.
Q9.What is the output of the following code?
void my....recursive....function(int *arr,  int val,  int idx,  int len) { if(idx == len) { printf("-1"); return ; } if(arr[idx] == val) { printf("%d", idx); return; } my....recursive....function(arr, val, idx+1, len); } int main() { int array[10] = {7,  6,  4,  3,  2,  1,  9,  5,  0,  8}; int value = 2; int len = 10; my....recursive....function(array,  value,  0,  len); return 0; }
  1. 3
  2. 4
  3. 5
  4. 6

Answer:- (B).
Explanations :The program searches for a value in the given array and prints the index at which the value is found. In this case, the program searches for value = 2. Since, the index of 2 is 4(0 based indexing), the program prints 4.
Q10.What is the space complexity of the above recursive implementation to find the factorial of a number?
  1. O(1)
  2. O(n)
  3. O(n2)
  4. O(n3)

Answer:- (A).
Explanations :The space complexity of the above recursive implementation to find the factorial of a number is O(1).