Java OCA OCP Practice Question 3247

Question

Which parameter declarations can be inserted at (1) so that the program compiles without warnings?.

interface Printable{}
class Shape implements Printable{}
class Rectangle extends Shape {}
class Square extends Shape {}

public class Main {
  public static void main(String[] args) {
    List<Shape> p = new ArrayList<Shape>();
    List<Rectangle> d = new ArrayList<Rectangle>();
    List<Square> c = new ArrayList<Square>();
    examine(p);// w  w  w  .j a  v  a  2s . c  om
    examine(d);
    examine(c);
  }

  static void examine(______________ pets) {            // (1)
    System.out.print("Your pets need urgent attention.");
  }
}

Select the three correct answers.

  • (a) List<? extends Shape>
  • (b) List<? super Shape>
  • (c) List<? extends Printable>
  • (d) List<? super Printable>
  • (e) List<?>
  • (f) All of the above


(a), (c), (e)

Note

Lists of type Shape, Rectangle, and Square are subtypes of List<? extends Shape>, List<? extends Printable> and List<?>.

List<? super Shape> is a super type for a list of Shape itself or a super type of Shape, e.g., Printable, but not Rectangle and Square.

List<?super Printable> is a super type for a list of Printable itself or a super type of Printable, e.g., Object, but not Shape, Rectangle, and Square.




PreviousNext

Related