Collections


  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. View Hint View Answer Discuss in Forum

    NA

    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().


  1. What is the output of this program?
    import java.util.*;
    public class Output
    {
    public static void main(String args[])
    {
    ArrayList object = new ArrayList();
    object.add("Y");
    object.add("O");
    object.add("U");
    object.ensureCapacity(3);
    object.trimToSize();
    System.out.println(object.size());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    trimTosize() is used to reduce the size of the array that underlines an ArrayList object.



  1. Map implements collection interface?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Collection interface provides add, remove, search or iterate while map has clear, get, put, remove, etc.


  1. Which of the below does not implement Map interface?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Vector implements AbstractList which internally implements Collection. Others come from implementing the Map interface.



  1. Which of these classes implements Set interface?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.