What is the result?

Given the code fragment:

What is the result?
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.

Download Printable PDF. VALID exam to help you PASS.

5 thoughts on “What is the result?

  1. Answer B:

    the remove() method has two signatures, one that returns boolean and takes an object parameter, the other takes an integer for the index, and returns the object that has been removed from the supplied index.

    NullPointerException will not be thrown because we are not applying any method on the null object. Instead, we are just removing an “empty” space, so to say, from the arraylist.

    Also, when we say remove(2), the supplied “2” will not be the Integer object “2”, because Integer is a wrapper class and it will be unboxed in this case, giving us a primitive type int 2. So the object “3” will be removed from the index “2”.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.