Java OCA OCP Practice Question 3272

Question

Consider the following program:

class Base<T> { }

class Derived<T> { }

class Test {
     public static void main(String []args) {
             // Stmt #1
     }
}

Which statements can be placed in the place of //Stmt#1 and the program remains compilable

(choose two):

  • a) Base<Number> b = new Base<Number>();
  • b) Base<Number> b = new Derived<Number>();
  • c) Base<Number> b = new Derived<Integer>();
  • d) Derived<Number> b = new Derived<Integer>();
  • e) Base<Integer> b = new Derived<Integer>();
  • f) Derived<Integer> b = new Derived<Integer>();


a)
f)

Note

Note that Base and Derived are not related by an inheritance relationship.

Further, for generic type parameters, subtyping doesn't work: you cannot assign a derived generic type parameter to a base type parameter.




PreviousNext

Related