Chapter 8: Recursion

For questions 1 - 2, use the following recursive method.
public int question1_2(int x, int y)
{
if (x == y) return 0;
else return question1_2(x-1, y) + 1;
}
If the method is called as question1_2(8, 3), what is returned?
11
8
5
3
24

5

For questions 1 - 2, use the following recursive method.
public int question1_2(int x, int y)
{
if (x == y) return 0;
else return question1_2(x-1, y) + 1;
}
Calling this method will result in infinite recursion if which condition below is initially true?

(x < y)

The following method should return true if the int parameter is even and either positive or 0, and false otherwise. Which set of code should you use to replace ... so that the method works appropriately?
public boolean question3(int x) { ... }
if (x = = 0

if (x = = 0) return true; else if (x < 0) return false; else return question3(x - 2);

For questions 4 - 7, 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?
0
1
3
6
9

6

For questions 4 - 7, 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(0) is called?
0
1
2
nothing, factorial(0) causes infinite recursi

1

For questions 4 - 7, refer to the following recursive factorial method.
public int factorial(int x)
{
if (x > 1) return x * factorial (x - 1);
else return 1;
}
How many times is the factorial method invoked if originally called with factorial(5)? Include

5

For questions 4 - 7, refer to the following recursive factorial method.
public int factorial(int x)
{
if (x > 1) return x * factorial (x - 1);
else return 1;
}
What condition defines the base case for this method?
(x > 1)
(x = = 1)
(x = = 0)
(x <= 0)
(x <

(x <= 1)

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;
}
the bas

the base case condition should be (x <= 0) instead of (x = = 0)

For questions 9 - 13, assume that int[ ] a = {6, 2, 4, 6, 2, 1, 6, 2, 5} and consider the two recursive methods below foo and bar.
public int foo(int[ ] a, int b, int j)
{
if (j < a.length)
if (a[j] != b) return foo (a, b, j+1);
else return foo (a, b, j+1

2

What is the result of calling foo(a, 3, 0);?
0
1
2
3
4

0

What is the result of calling foo(a, 2, 9);?
0
1
2
3
4

0

What is the result of calling bar(a, 0);?
0
5
6
12
34

34

What is the result of bar(a, 8);?
0
5
6
12
34

5

What does the following recursive method determine?
public boolean question16(int[ ]a, int[ ] b, int j)
{
if(j = = a.length) return false;
else if (j = = b.length) return true;
else return question16(a, b, j+1);
}
Returns true if a and b are equal in size

Returns true if a is larger than b, false otherwise

Why is the following method one which has infinite recursion?
public int infiniteRecursion(int n)
{
if (n > 0) return infiniteRecursion(n) + 1;
else return 0;
}
Because there is no base case
Because the base case will never be true
Because the recursive c

Because the recursive call does not move the parameter closer to the base case

If there are 2 disks to move from one Tower to another, how many disk movements would it take to solve the problem using the recursive solution?
0
1
2
3
4

3

If there are 6 disks to move from one Tower to another, how many disk movements would it take to solve the problem using the recursive solution?
6
13
31
63
127

63

The solution to the Towers of Hanoi has a(n) _____ complexity.

exponential

For questions 19 - 21, consider the following representation of grid and the maze code from Chapter 8.
Grid:
1 1 1 1 1 1 0 0
0 0 1 0 0 1 0 0
0 0 1 0 0 1 1 0
0 0 1 1 0 0 1 0
0 0 0 1 1 0 0 0
0 0 0 0 1 1 1 1
Code:
public boolean traverse(int row, int column)

traverse(1, 0);

Assume at some point in processing, grid's row 0 has become 3 3 3 1 1 1 0 0. Which direction will next be tried?
up
down
left
right
none, the recursion ends at this point

right

Which of the following grids would be the result after traverse has completed all of its recursive calls?
1 1 1 1 1 1 0 0
0 0 1 0 0 1 0 0
0 0 1 0 0 1 1 0
0 0 1 1 0 0 1 0
0 0 0 1 1 0 0 0
0 0 0 0 1 1 1 1
3 3 3 3 3 3 0 0
0 0 3 0 0 3 0 0
0 0 3 0 0 3 3 0
0 0 3

7 7 7 3 3 3 0 0
0 0 7 0 0 3 0 0
0 0 7 0 0 3 3 0
0 0 7 7 0 0 3 0
0 0 0 7 7 0 0 0
0 0 0 0 7 7 7 7

Define the magnitude of a number as the location of the decimal point from the left of the number (that is, if a number has 4 digits followed by the decimal point, it will have a magnitude of 4). 100 would then have a magnitude of 3 and 55,555.555 would h

magnitude(x / 10) + 1;

The following method recognizes whether a String parameter consists of a specific pattern and returns true if the String has that pattern, false otherwise. Use this recursive method to answer questions 23 - 25.
public boolean patternRecognizer(String a)
{

abcba

If the method is called as patternRecognizer(x) where x is "aa", what will the result be?
true
false
a NullPointerException
a run-time error
infinite recursion

true

If the statement a.substring(1, a.length( ) - 1) were changed to be (a.substring(1, a.length( )), then the method would
have infinite recursion unless the String were null
always return true
always return false
return true only if all characters of the St

return true only if all characters of the String were the same