QuestionQ28

Working with Selected Classes from the Java API

Given this code fragment:

Question Image

What is the resulting output?

  • A A NullPointerException is thrown at runtime.
  • B [1, 2, 4]
  • C [1, 2, 4, null]
  • D [1, 3, 4, null]
  • E [1, 3, 4]
  • F Compilation fails.
Explanation

For an ArrayList<Integer>, remove(1) invokes the overload that removes the element at index 1, so it removes 2. Calling remove(null) invokes the overload that removes the specified object, removing the null element. The list therefore contains [1, 3, 4].

Community Discussion

No comments yet. Be the first to start the discussion!