Question 351
Consider the following method.
public static void adjust(int[] data, int n)
{
data[n] = data[0] + n;
n = n + 1;
data[0] = data[n - 1];
}
What is printed by the following code?
int[] vals = {3, 5, 7, 9};
int k = 2;
adjust(vals, k);
System.out.println(vals[0] + " " + vals[2] + " " + k);Main Solution
Answer
Answer: C. The call changes data[2] to 3 + 2 = 5. The local variable n changes to 3, but k in the caller remains 2. Then data[0] becomes data[2], which is 5.
Answer: Correct: C
A
3 5 2
B
5 5 3
C
5 5 2
D
7 5 3
Answer images

