74.7k views
4 votes
Assuming values is a full array of int, what does the following recursive method return (assume it is passed a legal argument, a value between 0 and values.length)?

int mystery(int n)
{
if (n == -1)
return 0;
else
return (values[n] + mystery(n - 1));
}
Answer Choices :
the sum of the values in the array between index n and the start of the array
the number of values in the array between index n and the end of the array
the sum of the values in the array between index n and the end of the array
the number of values in the array between index n and the start of the array

1 Answer

7 votes
The given recursive method returns the sum of the values in the array between index n and the end of the array.

Step-by-step explanation:

The recursive method, mystery(int n), takes an integer argument n. If n is equal to -1, it means that the recursion has reached the start of the array, and the method returns 0. Otherwise, the method adds the value at index n to the sum of the values in the subarray starting from the next index (n-1) till the end of the array, which is returned by the recursive call to mystery(n-1).

Therefore, the recursive method adds up the values in the subarray starting from index n till the end of the array, and returns the sum. So, the correct answer is option (c) - the sum of the values in the array between index n and the end of the array.
User Xjfengck
by
7.5k points