Collections


  1. What is the output of this program?
    import java.util.*;
    public class vector_Example
    {
    public static void main(String args[])
    {
    Vector object = new Vector(6,3);
    object.addElement(new Integer(5));
    object.addElement(new Integer(7));
    object.addElement(new Integer(9));
    System.out.println(object.elementAt(2));
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    object.elementAt(2) returns the value stored at index 2, which is 9.


  1. What is the output of this program?
    import java.util.*;
    public class vector
    {
    public static void main(String args[])
    {
    Vector object = new Vector(6,3);
    object.addElement(new Integer(9));
    object.addElement(new Integer(7));
    object.addElement(new Integer(3));
    System.out.println(object.capacity());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    6



  1. What is the name of a data member of class Vector which is used to store a number of elements in the vector?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    elementCount


  1. What is the output of this program?
    import java.util.*;
    public class vector_Example
    {
    public static void main(String args[])
    {
    Vector object = new Vector(6,3);
    object.addElement(new Integer(10));
    object.addElement(new Integer(20));
    object.addElement(new Integer(50));
    object.removeAll(object);
    System.out.println(object.isEmpty());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    firstly elements 10, 20, 50 are entered in the vector object, but when object.removeAll(object); is executed all the elements are deleted and vector is empty, hence object.isEmpty() returns true.



  1. What is the output of this program?
    import java.util.*;
    public class stack_Example
    {
    public static void main(String args[])
    {
    Stack object = new Stack();
    object.push(new Integer(20));
    object.push(new Integer(12));
    object.pop();
    object.push(new Integer(60));
    System.out.println(object);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    push() and pop() are standard functions of the class stack, push() inserts in the stack and pop removes from the stack. 20 & 12 are inserted using push() the pop() is used which removes 12 from the stack then again push is used to insert 60 hence stack contains elements 20 & 60.