This book is now obsolete Please use CSAwesome instead.
12.9. Easy Multiple Choice QuestionsΒΆ
These problems are easier than most of those that you will usually see on the AP CS A exam.
- 1
- This is the method declaration. Look for a call to the same method in the body of the method.
- 3
- This is a conditional, not a method call.
- 4
- This is a return statement, not a method call.
- 5
- This line contains a call to the same method which makes this method recursive.
12-7-1: Which line has the recursive call?
1 2 3 4 5 6 | public static int factorial(int n)
{
if (n == 0)
return 1;
else return n * factorial(n-1);
}
|
- 1
- This is the method declaration. Look for a call to the same method in the body of the method.
- 3
- This is a conditional, not a method call.
- 4
- This is a return statement, not a method call.
- 5
- This is an else which is part of a conditional, not a method call.
- 6
- This line contains a call to the same method which makes this method recursive.
12-7-2: Which line has the recursive call?
1 2 3 4 5 6 7 8 | public String starString(int n)
{
if (n == 0) {
return "*";
} else {
return starString(n - 1) + starString(n - 1);
}
}
|
- 0
- Look at line 7 more closely.
- 1
- Many recursive methods only have one recursive call. But, this one has two.
- 2
- Line 7 has two calls to
fibonacci
. - 3
- There are not 3 calls to
fibonacci
.
12-7-3: How many recursive calls does the following method contain?
1 2 3 4 5 6 7 8 | public static int fibonacci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else return fibonacci(n-1) + fibonacci(n-2);
}
|
- 0
- Look for a call to the same method in the body of the method.
- 1
- Line 6 has one call to
multiplyEvens
. - 2
- Where do you see 2 calls to
multiplyEvens
? - 3
- Where do you see 3 calls to
multiplyEvens
?
12-7-4: How many recursive calls does the following method contain?
1 2 3 4 5 6 7 8 | public static int multiplyEvens(int n)
{
if (n == 1) {
return 2;
} else {
return 2 * n * multiplyEvens(n - 1);
}
}
|
You have attempted of activities on this page