Java OCA OCP Practice Question 2290

Question

What will be the result of attempting to compile and run the following code?.

public class Main {
  public static void main(String[] args) {
    List<? super Integer> sList = new ArrayList<Number>(); //(1)
    int i = 2007;
    sList.add(i);
    sList.add(++i);                                         //(2)
    Number num = sList.get(0);                              //(3)
  }
}

Select the one correct answer.

  • (a) The code will fail to compile because of an error in (1).
  • (b) The code will fail to compile because of an error in (2).
  • (c) The code will fail to compile because of an error in (3).
  • (d) The code will compile, but throw a ClassCastException at runtime at (3).
  • (e) The code will compile and execute normally.


(c)

Note

We can only get an Object from a List<? super Integer>.

The list could contain Comparable objects, and Number does not implement Comparable.




PreviousNext

Related