Java OCA OCP Practice Question 2342

Question

Which statements are true about the following code?

class MyClass<V> {
      MyClass()        {System.out.println(this);}            // (1)
      MyClass(V v)     {System.out.println(v);}               // (2)
  <T> MyClass(T t)     {System.out.println(t);}               // (3)
  <T> MyClass(T t, V v){System.out.println(t + ", " + v);}    // (4)
}

Select the two correct answers.

  • (a) The class attempts to declare four constructors.
  • (b) Only one of the two constructors in (2) and (3) can be declared in the class.
  • (c) A generic class cannot declare generic constructors.
  • (d) The compiler reports an error in (3), since the type parameter V is not used.
  • (e) The class compiles without problems.


(a) and (b)

Note

  • (a) The class uses the correct syntax to declare the constructors.
  • (b) The constructors in (2) and (3) have the same erasure and, therefore, only one of them can be declared, i.e., we have a name clash. The compiler reports an error.
  • (c) A generic class can declare generic constructors, as in (3) and (4).
  • (d) A type parameter declared by the class can be ignored in the class body.



PreviousNext

Related