Java OCA OCP Practice Question 2669

Question

What is the result of the following statements?

3:    List list = new ArrayList(); 
4:    list.add("one"); 
5:    list.add("two"); 
6:    list.add(7); 
7:    for (String s: list) 
8:    System.out.print(s); 
A.   onetwo
B.   onetwo7
C.   onetwo followed by an exception
D.   Compiler error on line 6
E.   Compiler error on line 7


E.

Note

The code does not compile.

It attempts to mix generics and legacy code.

Lines 3 through 7 create an ArrayList without generics.

This means that we can put any objects in it.

Line 7 should be looping through a list of Objects rather than Strings since we didn't use generics.




PreviousNext

Related