Java OCA OCP Practice Question 3274

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

(select all that apply)?

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


a)
d)
f)

Note

When <? extends Number> is specified as a type, then you can use any type derived from Number (including Number); hence, options a) and d) are correct.

Option f) is correct since class names are the same in both the sides, and ? in <?> is replaced by Integer, which is allowed.




PreviousNext

Related