Site Search:

Flash Card List 2 -- Card 5

Prev Next


How many lines of the following code does not compile? If remove them, what is the output?
  4     ArrayList<String> l = new ArrayList<>();
  5     l.add("1").add("5").add(1,"3");
  6     l.add("1");
  7     l.add("5");
  8     l.add(1,"3");
  9     l.add(6);
 10     System.out.println(l);


Line 5 doesn't compile, the add method returns a boolean, it can not be chained. Line 6 doesn't compile, the generic type is String, can not cast int to String. If line 5 and 9 is removed, the output is [1, 3, 5]