Output of Java Programs | Set 54 (Vectors)

Prerequisite : Vectors in Java Basics

1. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo1 {
    public static void main(String[] args)
    {
        Vector v = new Vector(20);
        System.out.println(v.capacity());
        System.out.println(v.size());
    }
}

chevron_right


Output:

20
0

Explanation:
function – int capacity( ) Returns the capacity of the vector i.e. how many elements it can hold.
function – int size( ) Returns the number of elements currently in the vector. Since no element is added, hence 0.

2. What is the Output Of the following Program



filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo2 {
    public static void main(String[] args)
    {
        Vector v = new Vector(20);
        v.addElement("Geeksforgeeks");
        v.insertElementAt("Java", 2);
        System.out.println(v.firstElement());
    }
}

chevron_right


Output:

It gives no output because of ArrayindexOutOfBound Exception

Explanation:
This is because initially there was only one element at index 0. Next it expects a value to be added at index 1, but we are adding at index 2. Hence an exception is raised.

3. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo3 {
    public static void main(String[] args)
    {
        Vector v = new Vector(20);
        v.addElement("Geeksforgeeks");
        v.insertElementAt("Java", 0);
        System.out.println(v.firstElement());
    }
}

chevron_right


Output:

Java

Explanation:
Void addElement (E) – Element E is added to the vector
void insertElementAt(Object E, int index) – Adds element E to the vector at the location specified by index.
Object firstElement( ) – Returns the first element in the vector.

“Java” becomes the first element and “Geeksforgeeks” becomes 2nd element.

4. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo4 {
    public static void main(String[] args)
    {
        Vector v = new Vector(20);
        v.addElement("Geeksforgeeks");
        System.out.println(v.elementAt(1));
    }
}

chevron_right


Output:

No output because of ArrayIndexOutOfBound Exception

Explanation:
function elementAt(int index) Returns the element at the location specified by index.
Here also, the element is added at index 0 and the size of array is only 1.
So invoking elementAt(1) raises an exception as index no. 1 is not yet existing.

5. What is the Output Of the following Program



filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo5 {
    public static void main(String[] args)
    {
        Vector v = new Vector(40);
        v.addElement("Geeksforgeeks");
        v.addElement("Programming");
        v.addElement("Java");
        System.out.println(v.firstElement());
        System.out.println(v.lastElement());
    }
}

chevron_right


Output:

Geeksforgeeks
Java

Explanation:
Function – firstElement( ) Returns the first element in the vector.
Function – lastElement( ) Returns the last element in the vector.

6. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo6 {
    public static void main(String[] args)
    {
        Vector v = new Vector(30);
        v.addElement("Geeksforgeeks");
        v.insertElementAt("Java", 0);
        System.out.println(v.indexOf("Geeksforgeeks"));
    }
}

chevron_right


Output:

1

Explanation:
function – int indexOf(Object E) Returns the index of the first occurrence of element E.

7. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo7 {
    public static void main(String[] args)
    {
        Vector v = new Vector(30);
        v.addElement("Geeksforgeeks");
        v.addElement("Java");
        v.addElement("C++");
        v.addElement("C");
        v.addElement("Geeksforgeeks");
        System.out.println(v.indexOf("Geeksforgeeks", 2));
    }
}

chevron_right


Output:

4

Explanation:
function – int indexOf(Object E, int start) – Returns the index of the first occurrence of element E at or after start. Hence the index of “Geeksforgeeks” is returned after index ‘2’.

8. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo8 {
    public static void main(String[] args)
    {
        Vector v = new Vector(30);
        v.addElement("Geeksforgeeks");
        v.addElement("Java");
        Vector v1 = new Vector();
        v1 = (Vector)v.clone();
        System.out.println(v1.firstElement());
    }
}

chevron_right


Output:



Geeksforgeeks

Explanation:
Function – Object clone( ) Returns a duplicate of the invoking vector.
v1 is a duplicate of v having 1st element as “Geeksforgeeks”

9. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo9 {
    public static void main(String[] args)
    {
        Vector v = new Vector(30);
        v.addElement("Geeksforgeeks");
        Vector v1 = new Vector();
        v1 = (Vector)v.clone();
        System.out.println(v1.firstElement());
  
        v.insertElementAt("Java", 0);
        System.out.println(v1.firstElement());
    }
}

chevron_right


Output:

Geeksforgeeks
Geeksforgeeks

Explanation:
Initially v1.firstElement() returns the first element of vector v1. Then an element is added to vector v, but it is not added to its duplicate vector v1.
Hence, once cloning is done, the new elements can not be added into the duplicate vector.
so vector v contains “Java” and “Geeksforgeeks” and vector v1 contains only “Geeksforgeeks”.

10. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo10 {
    public static void main(String[] args)
    {
        Vector v = new Vector(30);
        v.addElement("Geeksforgeeks");
        Vector v1 = new Vector();
        v1 = (Vector)v.clone();
        v.insertElementAt("Java", 0);
        System.out.println(v1.contains("Java"));
    }
}

chevron_right


Output:

false

Explanation:
function – boolean contains(Object E) Returns ‘true’ if element E is contained by the vector, and returns false if it is not.

11. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo11 {
    public static void main(String[] args)
    {
        Vector v = new Vector(8);
        v.addElement("Geeksforgeeks");
        v.insertElementAt("Java", 0);
        String array[] = new String[8];
        v.copyInto(array);
  
        for (int i = 0; i < array.length; i++)
            System.out.println(array[i]);
    }
}

chevron_right


Output:

Java
Geeksforgeeks
null
null
null
null
null
null

Explanation:
function – void copyInto(Object array[ ]) copies The elements contained in the invoking vector into the array specified by ‘array’.
Now the array has only 2 elements “Java” and “Geeksforgeeks”, the rest of the outputs are null.



12. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo12 {
    public static void main(String[] args)
    {
        Vector v = new Vector(30);
        v.addElement("Geeksforgeeks");
        v.insertElementAt("Java", 0);
        v.removeAllElements();
        System.out.println(v.size());
  
        v.addElement("Geeksforgeeks");
        v.addElement("Java");
        v.removeElementAt(0);
        System.out.println(v.size());
    }
}

chevron_right


Output:

0
1

Explanation:
function – void removeAllElements( ) Empties the vector and remove all the elements of vector. After this method executes, the size of the vector is zero.
function – void removeElementAt(int index) Removes the element at the location specified by ‘index’.

13. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo13 {
    public static void main(String[] args)
    {
        Vector v = new Vector(30);
        v.addElement("Geeksforgeeks");
        v.addElement("Java");
        v.trimToSize();
        System.out.println(v.size());
        System.out.println(v.capacity());
    }
}

chevron_right


Output:

2
2

Explanation:
function – void trimToSize( ) Sets the vector’s capacity equal to the number of elements that it currently holds.

14. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo14 {
    public static void main(String[] args)
    {
        Vector v = new Vector(30);
        v.addElement("Geeksforgeeks");
        v.addElement("Java");
        v.trimToSize();
  
        v.addElement("C++");
        System.out.println(v.size());
        System.out.println(v.capacity());
    }
}

chevron_right


Output:

3
4

Explanation:
v.trimToSize() makes the size and capacity equal to 2.
After adding another element, the size becomes 3 and capacity gets doubled (by default) i.e. 4

15. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
class demo15 {
    public static void main(String[] args)
    {
        Vector v = new Vector(30);
        v.addElement("Geeksforgeeks");
        v.addElement("Java");
        v.addElement("C++");
        v.addElement("C");
        System.out.println(v.toString());
        v.removeAllElements();
        System.out.println(v.toString());
    }
}

chevron_right


Output:

[Geeksforgeeks, Java, C++, C]
[]

Explanation:
function – toString( ) Returns the string equivalent of the vector.
The first call to toString() displays the string equaivalent of the contents of the vector v.
After removing all the elements, the vector becomes empty and hence the second call to toString() function returns an empty string.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.