Data Structure Multiple Choice Questions on “Recursion”.
1. Recursion is a method in which the solution of a problem depends on ____________ Answer: c 2. Which of the following problems can’t be solved using recursion? Answer: d 3. Recursion is similar to which of the following? Answer: b 4. In recursion, the condition for which the function will stop calling itself is ____________ Answer: c 5. What will happen when the below code snippet is executed? a) The code will be executed successfully and no output will be generated Answer: d 6. What is the output of the following code? a) 10 Answer: d 7. What is the base case for the following code? a) return 8. How many times is the recursive function called, when the following code is executed? a) 9 Answer: c 9. What does the following recursive code do? a) Prints the numbers from 10 to 1 Answer: c 10. Which of the following statements is true? Answer: b 11. What will be the output of the following code? a) 123456789 Answer: d 12. What will be the output of the following code? a) True Answer: b 13. What is the output of the following code? a) 6 Answer: a 14. What is the output of the following code? a) 3
a) Larger instances of different problems
b) Larger instances of the same problem
c) Smaller instances of the same problem
d) Smaller instances of different problems
Clarification: In recursion, the solution of a problem depends on the solution of smaller instances of the same problem.
a) Factorial of a number
b) Nth fibonacci number
c) Length of a string
d) Problems without base case
Clarification: Problems without base case leads to infinite recursion call. In general, we will assume a base case to avoid infinite recursion call. Problems like finding Factorial of a number, Nth Fibonacci number and Length of a string can be solved using recursion.
a) Switch Case
b) Loop
c) If-else
d) if elif else
Clarification: Recursion is similar to a loop.
a) Best case
b) Worst case
c) Base case
d) There is no such condition
Clarification: 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.void my_recursive_function()
{
my_recursive_function();
}
int main()
{
my_recursive_function();
return 0;
}
b) The code will be executed successfully and random output will be generated
c) The code will show a compile time error
d) The code will run for some time and stop when the stack overflows
Clarification: 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.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;
}
b) 1
c) 10 9 8 … 1 0
d) 10 9 8 … 1
Clarification: The program prints the numbers from 10 to 1.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;
}
b) printf(“%d “, n)
c) if(n == 0)
d) my_recursive_function(n-1)
Answer: c
Clarification: For the base case, the recursive function is not called. So, “if(n == 0)” is the base case.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;
}
b) 10
c) 11
d) 12
Clarification: The recursive function is called 11 times.void my_recursive_function(int n)
{
if(n == 0)
return;
my_recursive_function(n-1);
printf("%d ",n);
}
int main()
{
my_recursive_function(10);
return 0;
}
b) Prints the numbers from 10 to 0
c) Prints the numbers from 1 to 10
d) Prints the numbers from 0 to 10
Clarification: The above code prints the numbers from 1 to 10.
a) Recursion is always better than iteration
b) Recursion uses more memory compared to iteration
c) Recursion uses less memory compared to iteration
d) Iteration is always better and simpler than recursion
Clarification: Recursion uses more memory compared to iteration because every time the recursive function is called, the function call is stored in stack.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;
}
b) 10
c) 0
d) 9
Clarification: The program prints the number of digits in the number 123456789, which is 9.void my_recursive_function(int n)
{
if(n == 0)
{
printf("False");
return;
}
if(n == 1)
{
printf("True");
return;
}
if(n%2==0)
my_recursive_function(n/2);
else
{
printf("False");
return;
}
}
int main()
{
my_recursive_function(100);
return 0;
}
b) False
Clarification: The function checks if a number is a power of 2. Since 100 is not a power of 2, it prints false.int cnt = 0;
void my_recursive_function(char *s, int i)
{
if(s[i] == ' ')
return;
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
cnt++;
my_recursive_function(s,i+1);
}
int main()
{
my_recursive_function("thisisrecursion",0);
printf("%d",cnt);
return 0;
}
b) 9
c) 5
d) 10
Clarification: The function counts the number of vowels in a string. In this case the number is vowels is 6.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;
}
b) 4
c) 5
d) 6
Answer: b
Clarification: 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.