Question 3
Back to Exercise
What is the output of the following Java program?
import java.util.Arrays;
public class Main {
static void change(int[] arr) {
arr[0] = 99;
arr = new int[]{7, 7, 7};
arr[1] = 55;
}
public static void main(String[] args) {
int[] data = {1, 2, 3};
change(data);
System.out.println(Arrays.toString(data));
}
}
- A [99, 2, 3] Correct
- B [7, 55, 7]
- C [1, 2, 3]
- D [99, 55, 3]
Correct Answer: Option A
The first change goes to the caller's array; after arr is re-pointed, changes affect only the new local array.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!