Java OCA OCP Practice Question 3030

Question

Given:

2. public class Main<E> {  
3.   void react(E e) { }  
4.   static void main(String[] args) {  
5.     // Main<? extends Main> compound = new Main2<Main>();  
6.     // Main<? super Main2> compound = new Main2<Main>();  
7.     compound.react(new Main());     
8.     compound.react(new Main2());   
9.     compound.react(new Main3());   
10. } }  //from www  . ja v  a  2s . c  o m
11. class Main2<F> extends Main<F> { }  
12. class Main3<G> extends Main2<G> { } 

Which, taken independently, are true? (Choose all that apply.)

  • A. If line 5 is uncommented, compilation fails due to an error at line 7.
  • B. If line 5 is uncommented, compilation fails due to an error at line 8.
  • C. If line 5 is uncommented, compilation fails due to an error at line 9.
  • D. If line 6 is uncommented, compilation fails due to an error at line 7.
  • E. If line 6 is uncommented, compilation fails due to an error at line 8.
  • F. If line 6 is uncommented, compilation fails due to an error at line 9.


A, B, C, and D are correct.

Note

The generic type of the reference <? extends Main> says that the generic type of the instantiation can be either Main, or a subtype of Main.

Since the compiler doesn't know this instantiation generic type (runtime type), it does NOT bind any value to its generic criteria, so A, B, and C are correct.

On the other hand, the generic type of the reference <? super Main2> says that the generic type of the instantiation can be either Main2, or a super type of Main2.

Although the compiler doesn't know the instantiation generic type, it knows that it will be either Main2, or a super type of Main2-such types can bind any value that is either Main2 or a sub type of it.

Therefore, D is correct.




PreviousNext

Related