14/03/2023, 11:34
Quiz: Quiz 8 - Recursion
https://canvas.asu.edu/courses/142145/quizzes/1063473/take
1/5
Quiz 8 - Recursion
Started: Mar 14 at 11:29am
Quiz Instrucons
1 pts
Question 1
8
3
24
11
5
Use the following recursive method.
public int question(int x, int y)
{
if (x == y)
return 0;
else
return question(x-1, y) + 1;
}
If the method is called as question(8, 3), what is returned?
1 ptsQuestion 2
Spiral
Direct
Indirect
Infinite
__________________ recursion results from the lack of a base case.
14/03/2023, 11:34
Quiz: Quiz 8 - Recursion
https://canvas.asu.edu/courses/142145/quizzes/1063473/take
2/5
1 ptsQuestion 3
The program will throw an ArrayOutOfBoundsException.
The program will run out of memory.
The program will not compile.
The program will hang as though there is an infinite loop.
Which of the following will result from infinite recursion in Java?
1 ptsQuestion 4
Recursion should be used in every program.
Recursion should be avoided all the time.
Solutions that are easily expressed recursively should be programmed iteratively.
Solutions that are easily expressed recursively should be programmed recursively.
Which of the following statements is true?
1 ptsQuestion 5
Use the following recursive method.
public int question(int x, int y)
{ if (x == y)
return 0;
else
return question(x-1, y) + 1;
}
Calling this method will result in infinite recursion if which condition below is initially true?
14/03/2023, 11:34
Quiz: Quiz 8 - Recursion
https://canvas.asu.edu/courses/142145/quizzes/1063473/take
3/5
(x == 0 && y != 0)
( x > y)
(x != y)
( x == y)
(x < y)
1 ptsQuestion 6
short, efficient code
a base case
numerous lines of code
several variables
In a recursive solution, _______________ is(are) always necessary.
1 ptsQuestion 7
9
6
0
Refer to the following recursive factorial method.
public int factorial(int x)
{
if (x > 1)
return x * factorial (x – 1);
else
return 1;
}
What is returned if factorial(3) is called?
14/03/2023, 11:34
Quiz: Quiz 8 - Recursion
https://canvas.asu.edu/courses/142145/quizzes/1063473/take
4/5
1
3
1 ptsQuestion 8
True
False
The Towers of Hanoi puzzle cannot be solved iteratively.
1 ptsQuestion 9
the recursive case should return sum(x – 1) + 1; instead of sum(x – 1) + x;
the recursive case should return sum(x) + 1;
the method should return a boolean instead of an int
the base case condition should be (x <= 0) instead of (x = = 0)
the base case should return 1 instead of 0
What is wrong with the following recursive sum method? The method is supposed to
sum up the values
between 1 and x (for instance, sum(5) should be 5 + 4 + 3 + 2 + 1 = 15).
public int sum(int x)
{
if (x == 0)
return 0;
else
return sum(x – 1) + x;
}
1 ptsQuestion 10
14/03/2023, 11:34
Quiz: Quiz 8 - Recursion
https://canvas.asu.edu/courses/142145/quizzes/1063473/take
5/5
Quiz saved at 11:34am
The number of times char b appears in String a
The char which appears at location i in String a
The length of String a
The length of String a concatenated with char b
Returns 1 if char b appears in String a at least once, and 0 otherwise
What does the following method compute? Assume the method is called initially wi
th i = 0
public int question(String a, char b, int i)
{
if (i == a.length( ))
return 0;
else if (b == a.charAt(i))
return question(a, b, i+1) + 1;
else
return question(a, b, i+1);
}
Submit Quiz
Powered by TCPDF (www.tcpdf.org)