Java OCA OCP Practice Question 202

Question

Given:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> myList = new ArrayList<String>();
    myList.add("apple");
    myList.add("carrot");
    myList.add("banana");
    myList.add(1, "plum");
    System.out.print(myList);/*from  w  ww .j  av  a  2s  . c om*/
  }
 }

What is the result?

  • A. [apple, banana, carrot, plum]
  • B. [apple, plum, carrot, banana]
  • C. [apple, plum, banana, carrot]
  • D. [plum, banana, carrot, apple]
  • E. [plum, apple, carrot, banana]
  • F. [banana, plum, carrot, apple]
  • G. Compilation fails


B is correct.

Note

ArrayList elements are automatically inserted in the order of entry.

They are not automatically sorted.

ArrayLists use zero-based indexes.

The last add() inserts a new element and shifts the remaining elements back.




PreviousNext

Related