[go: up one dir, main page]

0% found this document useful (0 votes)
2K views4 pages

Recursion MCQs and Code Outputs

A stack is used to perform recursion. The output of doSomething(2,3) is 4, as it recursively calls the function, each time decrementing b by 1 until b reaches 1, at which point it returns a. If the base condition is not defined in recursion, it will result in a stack overflow. The output of something(4) is 24, as it recursively multiplies number by the return of the recursive call, until number reaches 0 and returns 1.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views4 pages

Recursion MCQs and Code Outputs

A stack is used to perform recursion. The output of doSomething(2,3) is 4, as it recursively calls the function, each time decrementing b by 1 until b reaches 1, at which point it returns a. If the base condition is not defined in recursion, it will result in a stack overflow. The output of something(4) is 24, as it recursively multiplies number by the return of the recursive call, until number reaches 0 and returns 1.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;
[Link]( 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() ;
[Link]([Link](12));
}
}
a) 0
b) 1
c) Compilation Error
d) Runtime Error

1) Which Data Structure is used to perform Recursion?  
a) Queue 
b) Stack 
c) Linked List 
d) Tree 
 
2) What’s the output o
4) What’s the output of something(4) following code?  
int something(int number) {  
if(number <= 0)  
return 1;
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);
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); 
}

You might also like