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.
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.
[1, 2, 4]
B
B:
# javac Test35.java
# java Test35
[1, 2, 4]
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”.
Answer is B
B