Java OCA OCP Practice Question 2388

Question

Which of the following options creates a generic class that can be passed multiple generic types? (Choose all that apply.).


a  class MyClass<A , B> {}

b  class MyClass<A a, B b> {}

c  class MyClass<Aa extends String, Bb extends Object> {
       void add(Aa a) {}
       void add(Bb a) {}
   }//from   w  ww  . j a  v  a  2  s  . c o  m

d  class MyClass<Aa, Bb> {
       void add(Aa a, Bb b) {}
   }


a, c, d

Note

Though Java recommends using single letters like T or V to specify the type, using the letters A and B is correct in option (a) as per the syntax.

Option (b) is incorrect because it uses invalid syntax to specify the type parameters to a class.

To specify multiple type parameters in a class declaration, you need to specify a placeholder for only the type-not its variables.

Option (c) and (d) are correct.

It's acceptable to define the type parameters as a subtype of an existing Java class.

Though not recommended, it's acceptable to use type parameters with more than one letter: Aa and Bb.




PreviousNext

Related