Java OCA OCP Practice Question 2999

Question

Consider the following program:

class Main {//from   w  w w  .ja v  a2 s . c om
    interface Printable {}
    interface Shape extends Printable {}
    interface Rectangle extends Shape {}

    static class C<T> {}
    static void foo(C<? super Shape> arg) {}

    public static void main(String []args) {
        foo(new C<Printable>());      // ONE
        foo(new C<Shape>());      // TWO
        foo(new C<Rectangle>());     // THREE
        foo(new C());          // FOUR
    }
}

Which of the following options are correct?

  • a) Line marked with comment ONE will result in a compiler error
  • b) Line marked with comment TWO will result in a compiler error
  • c) Line marked with comment THREE will result in a compiler error
  • d) Line marked with comment FOUR will result in a compiler error


c)

Note

options a) and b) For the substitution to succeed, the type substituted for the wildcard ? should be Shape or one of its super types.

option c) the type Rectangle is not a super type of Shape, so it results in a compiler error.

option d) the type argument is not provided, meaning that C is a raw type in the expression new C().

hence, this will elicit a compiler warning, but not an error.




PreviousNext

Related