Home » JAVA Programming » Collections » Question
  1. What is the output of this program?
    import java.util.*;
    public class Result
    {
    public static void main(String args[])
    {
    ArrayList object = new ArrayList();
    object.add("Y");
    object.add("O");
    object.add("U");
    object.ensureCapacity(5);
    System.out.println(object.size());
    }
    }
    1. 3
    2. 2
    3. 6
    4. 0
    5. Any Garbage Value
Correct Option: A

Although object.ensureCapacity(5); has manually increased the capacity of object to 5 but the value is stored only at index 3, therefore object.size() returns the total number of elements stored in the object i:e 3, it has nothing to do with ensureCapacity().



Your comments will be displayed only after manual approval.