Java OCA OCP Practice Question 3153

Question

Which statement, when inserted independently at (1), will cause either a compile time or a runtime error?

import java.util.ArrayList;
import java.util.Collection;

public class Main {
  public static void main(String[] args) {

    Collection<Integer> intList = new ArrayList<Integer>();
    intList.add(2008); intList.add(2009); intList.add(2010);
    // (1) INSERT STATEMENT HERE!
  }/*from   w  ww .ja  v  a2  s  . c o m*/
}

Select the four correct answers.

(a) Object[] objArray1 = intList.toArray();
(b) Integer[] intArray1 = intList.toArray();
(c) Number[] numArray1 = intList.toArray();
(d)  Object[] objArray2 = intList.toArray(new Object[0]);
(e) Integer[] intArray2 = intList.toArray(new Integer[0]);
(f) Integer[] intArray3 = intList.toArray(new Number[0]);
(g) Number[] numArray2 = intList.toArray(new Number[0]);
(h) Number[] numArray3 = intList.toArray(new Integer[0]);
(i) Number[] numArray4 = intList.toArray(new Long[0]);


(b), (c), (f), and (i)

Note

In (b), (c), and (f), the array type returned by the toArray() method is not a subtype of the array type on the left-hand side, resulting in a compile-time error.

The program will throw an ArrayStoreException in (i), because Integer objects cannot be stored in an array of type Long.




PreviousNext

Related