What will be the output of the program?
import java.util.*;
public class Listing51
{
public static void main(String[] args)
{
List<String> a = new ArrayList<String>(
Arrays.asList("x", "y"));
List<String> b = a;
b.add("z");
System.out.println(a.size() + " " + (a == b));
}
}
Answer: C
Assigning a list to another variable copies the reference, not the contents, so both names refer to one object. Adding through b is visible through a, giving a size of 3, and the identity test is true. A copy needs new ArrayList<String>(a).
Please log in or register to report this question.