Java OCA OCP Practice Question 3064

Question

Given:

1. public class Main<E extends Object> {  
2.   E m;  //from  ww  w. j ava 2s .  c o  m
3.   public Main(E m) {  
4.     this.m=m;  
5.   }  
6.   public E getMain() {  
7.     return m;  
8.   }  
9.   public static void main(String[] args) {  
10.     Main<Main> m1 = new Main(42);   
11.     Main m2 = new Main<Main>(m1.getMain());  
12.     Main m3 = m1.getMain();   
13. } } 

Which are true? (Choose all that apply).

A.   Compilation fails.
B.   An exception is thrown at runtime.
C.   Line 10 compiles with a warning.
D.   Line 10 compiles without warnings.
E.   Line 11 compiles with a warning.
F.   Line 11 compiles without warnings.


B, C, and F are correct.

Note

Compiler warnings are given when assigning a non-generic object to a reference with generic type.

F is correct and E is incorrect, because the reference type of m2 doesn't have any generic type specification.

Line 12 assigns the return value of m1.getMain() to m3.

It compiles because the compiler assumes that the return value of m1.getMain() will be an Main.

B is correct because at runtime, the actual return type will be an Integer, which causes a ClassCastException to be thrown.




PreviousNext

Related