10.8.2. Easier 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.
10-7-2-1: Which line has the recursive call?
1public static int factorial(int n)
2{
3 if (n == 0)
4 return 1;
5 else return n * factorial(n-1);
6}
- 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.
10-7-2-2: Which line has the recursive call?
1public String starString(int n)
2{
3 if (n == 0) {
4 return "*";
5 } else {
6 return starString(n - 1) + starString(n - 1);
7 }
8}
- 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
.
10-7-2-3: How many recursive calls does the following method contain?
1public static int fibonacci(int n)
2{
3 if (n == 0)
4 return 0;
5 else if (n == 1)
6 return 1;
7 else return fibonacci(n-1) + fibonacci(n-2);
8 }
- 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
?
10-7-2-4: How many recursive calls does the following method contain?
1public static int multiplyEvens(int n)
2{
3 if (n == 1) {
4 return 2;
5 } else {
6 return 2 * n * multiplyEvens(n - 1);
7 }
8}
You have attempted of activities on this page