MCQ on Recursion
Recursion is the most important concept in computer science. In this tutorial, You’ll find lot of practice questions related to recursion which will help you to grasp the concept.
In my previous posts, i have written about binary search implementation using recursion and difference between recursion and iteration.
These MCQ helps you to understand the concept of Recursion. These are some of the tricky questions on recursion which i collected for practice.
1) Which Data Structure is mainly used for implementing the recursive algorithm?
a) Queue
b) Stack
c) Linked List
D) Tree
2) What’s the output of the following code ?
1 2 3 4 5 6 7 8 9 |
int doSomething(int a, int b) { if (b==1) return a; else return a + doSomething(a,b-1); } doSomething(2,3); |
a)4
b)2
c)3
d)6
3) What’s happen if base condition is not defined in recursion ?
a) Stack underflow
b) Stack Overflow
c) None of these
d) Both a and b
4) Select the correct output.
1 2 3 4 5 6 7 8 |
int rec(int num){ return (num) ? num%10 + rec(num/10):0; } main(){ printf("%d",rec(4567)); } |
a) 4
b) 12
c) 22
d) 21
5) Choose correct option.
1 2 3 4 5 6 7 8 9 10 |
int something(int number) { if(number <= 0) return 1; else return number * something(number-1); } something(4); |
a) 12
b) 24
c) 1
d) 0
6)
1 2 3 4 5 6 7 8 9 10 |
int func(int a, int b){ if(b==0) return 0; if(b==1) return a; return a + func(a,b-1); } |
what will be the output of func(3,8) .
a) 11
b) 24
c) 22
d) 21
7)
1 2 3 4 5 6 7 8 9 |
void print(int n) { if (n == 0) return; printf("%d", n%2); print(n/2); } |
What will be the output of print(12).
a) 0011
b) 1100
c) 1001
d) 1000
8)
1 2 3 4 5 6 7 |
int sum(int n) { if (n==0) return n; else return n + sum(n-1); } |
What will be the output of sum(8).
a) 40
b) 36
c) 8
d) 15
9) Choose the correct answer.
a) Recursion is always better than iteration.
b) Recursion uses more memory compared to iteration.
c) Recursion uses less memory compared to iteration.
d) Iterative function is always better and simpler to write than recursion.
10) Recursion is similar to which of the following?
a) Switch Case
b) Loop
c) If-else
d) None
11) Recursion uses more memory compared to iteration.
a) True
b) False
c) Both
d) None of the above
12) Which of the following problem cannot be solved using recursion?
a) Tower of Hanoi
b) Fibonacci series
c) Tree Traversal
d) Problems without base case
13) Which searching can be performed recursively?
a) Linear search
b) Binary search
c) Both
d) None
14) In recursion the condition after which the function will stop calling itself is
a) Base condition
b) Function call
c) Both
d) None
Programming Questions on Recursion – Video Tutorials
Reverse a stack using recursion
Answers
1) (b) Stack data structure is mainly used for implementing the recursive algorithm. Recursion uses system stack for storing the return addresses of the function calls.
So, Stack data structure is used to implement recursive function calls.
2) (d)
Explanation
1 2 3 4 5 6 7 8 |
/* In first function call */ 2 + dosomething(2,2) /* Second call */ 2 + 2 + doSomething(2,1) /* Base condition met */ 2 + 2 + 2 |
3) (b)
When base condition is not defined in recursion, function will call itself infinitely which leads to a stack overflow exception (It is a situation in which the allocated space of a program is completely exhausted due to function calls).
4) (c)
5) (b)
It’s the recursive implementation of a factorial. Explanation of factorial using recursion.
6) (b)
7) (a)
8) (b)
For explanation check find sum of n natural numbers using recursion
9) (b)
Recursion uses more memory compared to iteration.
10) (b)
In recursion, the function will call itself until the base condition is not true. So, Recursion is similar to a loop and it will call itself until the base condition is not true.
11) (a)
Iteration requires less memory than recursion. In recursion, Each function call is stored in a call stack. So, This statement recursion uses more memory compared to iteration is true.
12) (d)
13) (c) We can implement both linear and binary search recursively as well as iteratively.
14) (a) In recursion, when the base condition is true then the function will stop calling itself. It is the terminating condition.