Page 398 - computer science (868) class 11
P. 398
2. Write two disadvantages of recursion.
Ans. Two disadvantages of recursion are as follows:
• It consumes a lot of memory. As the method calls itself repeatedly, every time the variable is allocated with a new memory on
the stack.
• It is a slow process because of its memory stack manipulation.
3. Write any two differences between recursion and iteration.
Ans. Two differences between recursion and iteration are as follows:
Recursion Iteration
Fresh memory space is allocated for each recursive call. It uses the same memory locations for variables and
repeats the same unit of code.
It is a slow process. It is faster than recursion.
4. Predict the output of the following recursive code when n=27. Also, tell what this given code is doing.
class Output1
{
void calling(int nn)
{
int f=2;
show(nn, f);
}
void show(int n, int f)
{
if(n == 1 )
System.out.println();
else if(n % f == 0 )
{
System.out.print(f+" ");
show(n/f, f);
}
else
show(n, f+1);
}
}
Ans. Dry run/working of the given program is as follows:
n 27 27 9 3 1
f 2 3 3 3 3
n==1 false false false false true
n%f==0 27%2==0 false 27%3==0 true 9%3==0 true 3%3==0 true xx
print xx 3 3 3 newline
method call show(27, 3) show(9, 3) show(3, 3) print(1, 3)
Output: 3 3 3
This method is printing the Prime factors of a number.
5. 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? Show the dry run/working. [ISC 2019]
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);
396 396 Touchpad Computer Science-XI

