Page 401 - computer science (868) class 11
P. 401
2. Fill in the blanks to return the LCM of two numbers a and b using the recursive method int lcm(int a, int b, int lc) where a=24,
b=36 and lc=1.
void lcm(int a, int b, int lc)
{
if(a. …………………)
b. …………………
else
{
c. …………………
}
}
3. Fill in the blanks to find the frequency of the vowels in a String using recursive method int count(String s, int p) where s=“UKRAINE”
and p=s.length()-1.
int count(String s, int p)
{
if(a. …………………)
b. …………………
else if(c. …………………)
return 1+ e. …………………
else
e. …………………
}
C. Answer the following questions:
1. The following function is a part of some class. Predict the output of the methods with proper working/dry run. [ISC 2014]
void fun1char 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);
}
(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”?
2. The following function is a part of some class. What will the method return if x=29? Show working/dry run.
int generate(int x)
{
if(x<=2)
return x;
else
return generate(x/2)+generate(x/2+1);
}
3. The following function is a part of some class. What will the method print if n=5, i=4 and j=3? Show working/dry run.
void print(int n, int i, int j)
{
399
Recursion 399

