Home » JAVA Programming » Collections » Question
  1. What is the output of this program?
    import java.util.*;
    public class Arraylist_Example
    {
    public static void main(String args[])
    {
    ArrayList object = new ArrayList();
    object.add("L");
    object.add("V");
    object.add("E");
    object.add(1, "O");
    System.out.println(object);
    }
    }
    1. [L, O]
    2. [O]
    3. [L, O, V, E]
    4. [L, O, V]
    5. None of these
Correct Option: C

object is an object of class ArrayList hence it is an dynamic array which can increase and decrease its size. object.add(“n”) adds to the array element n and object.add(1,”n”) adds element n at index position 1 in the list, Hence object.add(1,”O”) stores O at index position 1 of obj and shifts the previous value stored at that position by 1.



Your comments will be displayed only after manual approval.