Question 501
public static void change(int x, int[] a)
{
x = x + 10;
a[0] = a[0] + 10;
}
int n = 5;
int[] arr = { 5 };
change(n, arr);
System.out.println(n + " " + arr[0]);
What is printed when the code segment executes?Main Solution
Solution:
Answer: B. Java passes every argument by value. For the int parameter x, the copy is of the value 5; reassigning x inside change has no effect on n. For the array parameter a, the copy is of the reference, so a and arr point to the same array object; writing through a[0] is visible through arr[0]. Hence n stays 5 and arr[0] becomes 15.
Answer: Correct: B
Answer images

