170k views
0 votes
In the following code, what is the printout for list2? class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + " "); } }

A. 1 2 3
B. 1 1 1
C. 0 1 2
D. 0 1 3

User Jacobvdb
by
4.8k points

1 Answer

7 votes

Answer:

C. 0 1 2.

Step-by-step explanation:

As we know that in Java arrays are created dynamically.In this problem we are first assigning the array {1,2,3} and it's reference is assigned to list1. When we write list2=list1 now list will be referencing the same array as the list1. So any changes made to list1 will be reflected to the list2 reference also.

User Xevelion
by
4.9k points