Monday, November 18, 2024
Google search engine
HomeLanguagesJavaOutput of Java Programs | Set 54 (Vectors)

Output of Java Programs | Set 54 (Vectors)

Prerequisite : Vectors in Java Basics 

1. What is the Output Of the following Program 

Java




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());
    }
}


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 

Java




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());
    }
}


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 

Java




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());
    }
}


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 

Java




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));
    }
}


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 

Java




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());
    }
}


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 

Java




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"));
    }
}


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 

Java




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));
    }
}


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 

Java




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());
    }
}


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 

Java




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());
    }
}


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 

Java




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"));
    }
}


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 

Java




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]);
    }
}


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 

Java




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());
    }
}


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 

Java




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());
    }
}


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 

Java




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());
    }
}


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 

Java




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());
    }
}


Output:

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

Explanation: function – toString( ) Returns the string equivalent of the vector. The first call to toString() displays the string equivalent 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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments