Question 2811
Multiple choice
Unit 4: Data Collections
Consider the following recursive method.
public static int score(int[] arr, int k)
{
if (k >= arr.length)
{
return 0;
}
int rest = score(arr, k + 2);
if (arr[k] % 2 == 0)
{
return rest + arr[k];
}
return rest - arr[k];
}
What value is returned by the following code?
int[] values = {5, 2, 4, 7, 6};
score(values, 0);
Pick an answer, then check it. The solution opens in a window.
CCorrect answer
Main Solution
Answer
Answer: C. The method visits indices 0, 2, and 4. Index 4 contributes +6, index 2 contributes +4, and index 0 contributes -5. The total is 5.
Answer: Correct: C
A
15
B
10
C
5
D
-5
Answer images

