Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please kindly choose the correct answer of multiple choice question with some ex

ID: 3871108 • Letter: P

Question

Please kindly choose the correct answer of multiple choice question with some explainations on the last 4 questions. Thanks

Whis of the following(s) is/are the benefits of recursive method?

In general, recursive method is faster than simple loop.
Recursive method uses less resources than method using loop.
Recrusive method is always easier than simple loop.
Recursion enables us to write simple, clear solution for inherently recursive problems that would otherwise be difficult to solve.

What is a base case in a recursive method?

A selection condition
A computing condition
A stopping condition
A binding condition

What is a recursive method?

A method contains an if-then-else statement.
A method throws an exceptions.
A method contains static variables.
A method calls itself.

Which of the following(s) is/are needed to construct recursive method?

base case(s)
recursive call(s)
selection statement(s)
exception handling statement(s)

Which of the following(s) is/are required to avoid infinite recursion?

base case
exception handling case
override the method
The problem is eventually reduced to its base case.

How many times is the factorial method in the following code invoked for factorial(6)?

public static long factorial(int n){

if(n==1)

return 1;

else

return n*factorial(n-1);

}

a.4
b.5
c.6
d.7
e.None of the above

How many times is the fib method in the following code invoked for fib(5)?

public static long fib(int n){

if(n==0)

return 0;

else if(n==1)

return 1;

else

return fib(n-1) + fib(n-2);

}

a.10
b.12
c.14
d.15
e.17
h.18
i.None of the above

How many times is the isPal method in the following code invoked for isPal("abcdeghiktkihgeccba")?

public static boolean isPal(String s){

if(s.length() <= 1) return true;

else if(s.charAt(0)!= s.charAt(s.length() -1)) return false;

else return isPal(s.substring(1,s.length()-1));

}

a.5
b.6
c.7
d.8
e.9
h.10
i.None of the above

How many times is the moveDisk method in the following code invoked for moveDisk(5)

public void moveDisks(int n, char fromTower, char toTower, char auxTower){

if(n==1){

System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower + " ");

}else{

moveDisks(n-1, fromTower, auxTower, toTower);

System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower + " ");

moveDisks(n-1, auxTower, toTower, fromTower);

}

}

a.15
b.16
c.18
d.24
e.31
f.63
g.72
h.84
i.127
j.None of the above

Explanation / Answer

1.(d) Recursion enables us to write simple, clear solution for inherently recursive problems that would otherwise be difficult to solve. They are not faster , they take more time and spcae but some problems can be solved easily using recursion.
2.(c) A base case is use to terminate recursion because at some value it has to stop calling itself. So, base case is a A stopping condition
3. (d) Recursion method calls itself until it is terminated by base case.
4.(a,b) Base cases and recursive calls are needed in recursive method . They are the basic elements of recursion. Recursive calls are needed to call itself and base case to terminate recursion.
5. (a) Base case is required to avoid infinite recursion
6. The code will be invoked 5 times after calling factorial(6) . for n=5,4,3,2,1 after that recursion will be terminated by base case n==1.
7. The code will be invoked 14 times after calling fib(5)
fib(5-1)+fib(5-2)------------2
fib(5-1)=fib(4)----fib(4-1)+fib(4-2)--------2
fib(5-2)=fib(3)----fib(3-1)+fib(3-2)--------2
fib(4-1)=fib(3)----fib(3-1)+fib(3-2)--------2
fib(4-2)=fib(2)----fib(2-1)+fib(2-2)--------2
fib(3-1)=fib(2)----fib(2-1)+fib(2-2)--------2
fib(3-2)=fib(1)----return 1
fib(3-1)=fib(2)----fib(2-1)+fib(2-2)--------2
all remaning are either f(0) or f(1)
total----------14