Question 1 (5 points) Method A invokes method A itself. This is called _________
ID: 3673442 • Letter: Q
Question
Question 1 (5 points)
Method A invokes method A itself. This is called _________.
Question 1 options:
indirect recursion
explicit recursion
direct recursion
one-step recursion
Question 2 (5 points)
What are the base cases in the following recursive method?
public static void recurse(int n)
{
if (n > 0)
{
System.out.print(n % 10);
recurse(n / 10);
}
}
Question 2 options:
n > 0
n <= 0
no base cases
n < 0
Question 3 (5 points)
What will be displayed by the method call recurse(1234)?
public static void recurse(int n)
{
if (n <= 0)
{
System.out.print(n % 10);
recurse(n / 10);
}
}
Question 3 options:
4321
1234
4 3 2 1
Nothing
Save
Question 4 (5 points)
Analyze the following recursive method and indicate which of the following will be true.
public static long factorial(int n)
{
return n * factorial(n - 1);
}
Question 4 options:
The method runs infinitely and causes a StackOverflowError.
Invoking factorial(2) returns 2.
Invoking factorial(1) returns 1.
Invoking factorial(0) returns 0.
Question 5 (5 points)
Fill in the code to complete the following method for computing factorial.
// Return the factorial for a specified index
public static long factorial(int n)
{
if (n == 0)
return 1;
else
return _____________;
}
Question 5 options:
n * (n - 1)
n * factorial(n - 1)
n
factorial(n - 1) * (n - 1)
Question 6 (5 points)
What will be displayed by the method call recurse(6)?
public static int recurse(int n)
{
if (n <= 1)
return 1;
else
return n + recurse(n - 2);
}
Question 6 options:
13
14
11
12
Question 7 (5 points)
The base case _________ the recursion.
Question 7 options:
pauses
starts
breaks
stops
Question 8 (5 points)
What is the output of the following program?
{
public static void main(String[] args)
{
System.out.println(countDown(2, 0));
}
public static int countDown(int n, int result)
{
if (n == 0)
return 0;
else
return countDown(n - 1, n + result);
}
}
Question 8 options:
3
2
1
0
Question 9 (5 points)
Which of the following statements about recursive methods is accurate?
Question 9 options:
They must have exactly 1 base case and exactly 1 recursive case
They must have at least 1 base case and exactly 1 recursive case
They must have exactly 1 base case and at least 1 recursive case
They must have at least 1 base case and at least 1 recursive case
Question 10 (5 points)
Which of the following is a possible disadvantage of recursion?
Question 10 options:
Recursive solutions tend to have more local variables than their iterative counterparts
Recursive solutions are more likely to go into infinite loops than their iterative counterparts
Recursive solutions tend to be longer than their iterative counterparts
Recursive solutions can be less efficient than their iterative counterparts
indirect recursion
explicit recursion
direct recursion
one-step recursion
Explanation / Answer
Please find the answers below :
1) direct recursion
2) n <= 0
3) Nothing
4) The method runs infinitely and causes a StackOverflowError.
5) n * factorial(n - 1)
6) 13
7) breaks
8) 0
9) They must have at least 1 base case and at least 1 recursive case
10) Recursive solutions are more likely to go into infinite loops than their iterative counterparts
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.