OCA Java SE 8 Core Java APIs - OCA Mock Question Core Java APIs 3-9








Question

What is the result of the following statements?

     1:  List<String> list = new ArrayList<String>(); 
     2:  list.add("A"); 
     3:  list.add("B"); 
     4:  list.add(7); 
     5: for(String s : list)  System.out.print(s); 
  1. AB
  2. AB7
  3. AB followed by an exception
  4. Compiler error on line 4.
  5. Compiler error on line 5.




Answer



D.

Note

The list is instantiated using generics.

Only String objects can be added to list and 7 is an int.