Data Structure MCQ - Recursion
Q1.Recursion is a method in which the solution of a problem depends on .....
- Larger instances of different problems
- Larger instances of the same problem
- Smaller instances of the same problem
- 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.
Explanations :In recursion, the solution of a problem depends on the solution of smaller instances of the same problem.
- Factorial of a number
- Nth fibonacci number
- Length of a string
- All of the mentioned
Answer:- (D).
Explanations :All of the above mentioned problems can be solved using recursion.
Explanations :All of the above mentioned problems can be solved using recursion.
- The code will be executed successfully and no output will be generated
- The code will be executed successfully and random output will be generated
- The code will show a compile time error
- 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.
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.
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; }
- return
- printf("%d ", n)
- if(n == 0)
- 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.
Explanations :For the base case, the recursive function is not called. So, "if(n == 0)" is the base case.
- Switch Case
- Loop
- If-else
- if elif else
Answer:- (B).
Explanations :Recursion is similar to a loop.
Explanations :Recursion is similar to a loop.
- Best case
- Worst case
- Base case
- 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.
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.
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; }
- 10
- 1
- 10 9 8 … 1 0
- 10 9 8 … 1
Answer:- (D).
Explanations :The program prints the numbers from 10 to 1.
Explanations :The program prints the numbers from 10 to 1.
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; }
- 123456789
- 10
- 0
- 9
Answer:- (D).
Explanations :The program prints the number of digits in the number 123456789, which is 9.
Explanations :The program prints the number of digits in the number 123456789, which is 9.
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; }
- 3
- 4
- 5
- 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.
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.
- O(1)
- O(n)
- O(n2)
- O(n3)
Answer:- (A).
Explanations :The space complexity of the above recursive implementation to find the factorial of a number is O(1).
Explanations :The space complexity of the above recursive implementation to find the factorial of a number is O(1).
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.