Java OCA OCP Practice Question 2628

Question

Consider the following program:

import java.util.ArrayList;

public class Main {
        public static void main(String []args) {
                ArrayList<Integer> list = new ArrayList<Integer>();
                list.add(new Integer(2));
                list.add(1);/*from  w w w.j a va  2 s .  co  m*/
                list.add(5);
                list.remove(2);         // REMOVE
                System.out.println(list);
        }
}

Which one of the following options correctly describes the behavior of this program?

  • a) When executed, this program prints the following: [2, , 5].
  • b) When executed, this program prints the following: [2, 1].
  • c) When executed, this program prints the following: [1, 5].
  • d) This program results in a compiler error in the line marked with the comment REMOVE.
  • e) This program results in a NoSuchElementException in the line marked with the comment REMOVE.


b)

Note

The remove method in ArrayList removes the element at the specified position in the list, and shifts any subsequent elements in the list to the left.




PreviousNext

Related