10.8.2. Easier Multiple Choice QuestionsΒΆ
These problems are easier than most of those that you will usually see on the AP CSA 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.
- 5
- This is a return statement, not a method call.
- 9
- 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 {
5 return 1;
6 }
7 else
8 {
9 return n * factorial(n-1);
10 }
11}
- 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.
- 5
- This is a return statement, not a method call.
- 7
- This is an else which is part of a conditional, not a method call.
- 9
- 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 {
5 return "*";
6 }
7 else
8 {
9 return starString(n - 1) + starString(n - 1);
10 }
11}
- 0
- Look at line 13 more closely.
- 1
- Many recursive methods only have one recursive call. But, this one has two.
- 2
- Line 13 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 {
5 return 0;
6 }
7 else if (n == 1)
8 {
9 return 1;
10 }
11 else
12 {
13 return fibonacci(n-1) + fibonacci(n-2);
14 }
15}
- 0
- Look for a call to the same method in the body of the method.
- 1
- Line 9 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 {
5 return 2;
6 }
7 else
8 {
9 return 2 * n * multiplyEvens(n - 1);
10 }
11}
You have attempted of activities on this page