Collections


  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. 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. 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 Result
    {
    public static void main(String args[])
    {
    ArrayList object = new ArrayList();
    object.add("Y");
    object.add("O");
    object.add("U");
    System.out.println(object.size());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    3



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

    NA

    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.