You are on page 1of 4

Ques 1

static int strange(int x,int y)


{
if(x>=y)
{
x=x-y;
return strange(x,y);
}
else
return x;
}
System.out.println(strange(20,7));

Ques 2

void Recur(int n)
{
if (n>1)
{
System.out.print(n+ " ");
if (n%2 !=0)
{
n=3*n+1;
System.out.print(n + " ");
}
Recur (n/2);
}
}
Recur(10)
Ques3
The following functions are part of some class
i) What will be the output of fun1() when the value of s[] ={‘J’,’U’,’N’,’E’} AND X= 1?
ii) What will be the output of fun2() when the value of n =”SCROLL” ?
iii) State in one line what does the function fun1() do apart from recursion
void fun1(char s[], int x)
{
System.out.println(s);
char temp;
if(x<s.length/2)
{
temp=s[x];
s[x] = s[s.length-x-1];
s[s.length-x-1]=temp;
fun1(s, x+1);
}
}
void fun2(String n)
{
char c[]=new char[n.length()];
for(int i=0;i<c.length; i++)
c[i]=n.charAt(i);
fun1(c,0);
}
Ques4
The following is a part of some class. What will be the output of the function mymethod() when the
value of the counter is equal to 3? Show the dry run/working
void mymethod(int counter)
{
if(counter==0)
System.out.println(" ");
else
{
System.out.println("Hello" +counter);
mymethod(--counter);
System.out.println(" "+counter);
}
}
Ques5
The following function Mystery( ) is a part of some class. What will the function Mystery ( ) return
when the value of num=43629, x=3 and y=4 respectively? Show the dry run/ working.
int Mystery( int num, int x, int y)
{
if(num<10)
return num;
else
{
int z = num % 10;
if( z % 2 = = 0 )
return z*x + Mystery( num/10,x,y);
else
return z*y + Mystery( num/10,x,y);
}
}
Ques6
The following function magicfun( ) is a part of some class. What will the function magicfun( ) return,
when the value of n=7 and n=10, respectively? Show the dry run/working:
int magicfun( int n)
{
if ( n= = 0)
return 0;
else
return magicfun(n/2) * 10 + (n % 2);
}

Ques7
The following function is a part of some class. Assume ‘x’ and ‘y’ are positive
integers, greater than 0. Answer the given questions along with dry run / working.
void someFun(int x, int y)
{
if(x>1)
{
if(x%y==0)
{
System.out.print(y+“ ”);
someFun(x/y, y);
}
else
someFun(x, y+1);
}
}
(i) What will be returned bysomeFun(24,2)?
(ii) What will be returned bysomeFun(84,2)?
(iii) State in one line what does the function someFun( )do, apart from recursion?
Ques8
The following function Check( ) is a part of some class. What will the function Check( ) return when
the values of both ‘m’ and ‘n’ are equal to 5? Show the dry run / working
int Check (int m, int n)
{
if (n = = 1)
return − m − −;
else
return + + m + Check (m, −−n);
}
Ques9
int quiz( int n)
{
if ( n <= 1 )
return n;
else
return (--n % 2) + quiz(n/10);
}
(a) What will the function quiz( ) return when the value of n=36922?
(b) State in one line what does the function quiz( ) do, apart from recursion?
Ques 9
void numbers(int n)
{
if(n>0)
{
System.out.print(n+” “);
numbers(n-2);
System.out.print(n+” “);
}
}
String numbers1(int n)
{
if(n<=0)
return “”;
return ( (numbers1(n-1))+n+ ” “);
}
}
numbers(5)?
numbers1(6)?
Ques10

void perform (int p)


{
int x;
for(int i=1;i<=p;i++)
{
x=trial(i);
System.out.println(x+” “);
}
}
int trial(int n)
{
if(n==1)
return 2;
else if(n==2)
return 3;
else
return trial(n-2)+trial(n-1);
}
trial(4) ?
perform(5) ?

Ques 11

int check(int n)
{
if(n<=1)
return 1;
if(n%2==0)
return 1+check(n/2);
else
return 1+check(n/2+1);
}
I) n=25
II) n=10

Q1 factorial
Q2 prime
Q3 power
Q4 count no of digits
Q5 sum of digits
Q6 reverse a number
Q7 hcf
Q8 reverse a string
Q9 Fibonacci series
Q10 print factors
Q11 print prime factors

1. What happens when base case is not defined in a recursive method?

2. What is direct recursion?

3. Which data structure / principle is used to perform recursion?

4. advantages of recursion

You might also like