Nothing Special   »   [go: up one dir, main page]

Recursion MCQ

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1) Which Data Structure is used to perform Recursion?

a) Queue
b) Stack
c) Linked List
d) Tree

2) What’s the output of doSomething(2,3) in following code ?


int doSomething(int a, int b) {
if (b==1)
return a;
else
return a + doSomething(a,b-1);
}
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) What’s the output of something(4) following code?
int something(int number) {
if(number <= 0)
return 1;
else
return number * something(number-1);
}
a) 12
b) 24
c) 1
d) 0

5) What will be the output of func(3,8) ?


int func(int a, int b){
if(b==0)
return 0;
if(b==1)
return a;
return a + func(a,b-1);
}
a) 11
b) 24
c) 22
d) 21
6) What will be the output of print(12)?
void print(int n) {
if (n == 0)
return;
System.out.println( n%2);
print(n/2);
}
a) 0011
b) 1100
c) 1001
d) 1000

7) What will be the output of sum(8)?


int sum(int n) {
if (n==0) return n;
else
return n + sum(n-1);
}
a) 40
b) 36
c) 8
d) 15
8) What is the value of fun(4, 3)?
int fun(int x, int y)
{
if (x == 0)
return y;
return fun(x - 1, x + y);
}
a) 13
b) 12
c) 9
d) 10

9) What is the output of this program?


class recursion {
int func (int n) {
int result;
result = func (n - 1);
return result;
}
}
class Output {
public static void main(String args[]) {
recursion obj = new recursion() ;
System.out.print(obj.func(12));
}
}
a) 0
b) 1
c) Compilation Error
d) Runtime Error

You might also like