Java OCA OCP Practice Question 2851

Question

Given:

2. class Shape { }  
3. class Square extends Shape { }  
4. class Rectangle extends Shape { }  
5. public class Main<A extends Shape> {  
6.   public <C extends Rectangle> Main<? super Square> useMe(A a, C c) {  
7.     //Insert Code Here  
8. } } 

Which, inserted independently at line 7, compile? (Choose all that apply.)

  • A. return null;
  • B. return new Main<Square>();
  • C. return new Main<Shape>();
  • D. return new Main<A>();
  • E. return new Main<a>();
  • F. return new Main<c>();


A, B, and C are correct.

Note

B and C are correct because the return type of the method is Main<? super Square>, which means a Main object with a generic type that is either a Square or a super type of Square.

A is correct because null is acceptable despite the reference type.

D, E, and F are incorrect because you must specify an exact generic class type when instantiating a generic class.




PreviousNext

Related