Java OCA OCP Practice Question 3027

Question

Consider the following program:

class Base<T> { }

class Derived<T> { }

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

Which statements can be replaced with //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, subtype doesn't work: you cannot assign a derived generic type parameter to a base type parameter.




PreviousNext

Related