Java OCA OCP Practice Question 3255

Question

Consider the following program:

public class Main {
        interface Printable {}
        interface Writable extends Printable {}
        interface Readable extends Writable {}

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

        public static void main(String []args) {
                foo(new C<Printable>());      // ONE
                foo(new C<Writable>());      // TWO
                foo(new C<Readable>());     // THREE
                foo(new C());          // FOUR
        }/*ww  w  . j  av a 2 s. c o m*/
}

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 Writable or one of its super types.

Option c): The type Readable is not a super type of Writable, 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