Java OCA OCP Practice Question 2322

Question

Which statements can be inserted at (1) without the compiler reporting any errors?

public class Main {
  public static void main(String[] args) {
    List<?> lst = new ArrayList<String>();
    // (1) INSERT HERE
  }
}

Select the two correct answers.

  • (a) lst.add(null);
  • (b) lst.add("OK");
  • (c) lst.add(2007);
  • (d) String v1 = lst.get(0);
  • (e) Object v2 = lst.get(0);


(a) and (e)

Note

In (b) and (c), we cannot add any object to the list as the list is of an unknown type.

From a list of an unknown type, we are only guaranteed to get an Object.

In (d) capture-of ? cannot be converted to String.




PreviousNext

Related