Java OCA OCP Practice Question 2301

Question

Which statement is true about the following code?.

class Fruit {}//from   w ww  .j av  a  2 s.co  m
class Apple extends Fruit {}

public class Main {
  public static void main(String[] args) {
    List<? extends Apple> lst1 = new ArrayList<Fruit>(); // (1)
    List<? extends Fruit> lst2 = new ArrayList<Apple>(); // (2)
    List<? super Apple> lst3 = new ArrayList<Fruit>();   // (3)
    List<? super Fruit> lst4 = new ArrayList<Apple>();   // (4)
    List<?> lst5 = lst1;                                   // (5)
    List<?> lst6 = lst3;                                   // (6)
    List lst7 = lst6;                                      // (7)
    List<?> lst8 = lst7;                                   // (8)
  }
}

Select the one correct answer.

  • (a) (1) will compile, but (2) will not.
  • (b) (3) will compile, but (4) will not.
  • (c) (5) will compile, but (6) will not.
  • (d) (7) will compile, but (8) will not.
  • (e) None of the above.


(b)

Note

ArrayList<Fruit> is not a subtype of List<? extends Apple>, and ArrayList<Apple> is not a subtype of List<? super Fruit>.

Any generic list can be assigned to a raw list reference.

A raw list and an unbounded wildcard list are assignment compatible.




PreviousNext

Related