Menu
Topics Index
...
`

Vector add and print elements


If you need explanation Read this topic

If you need Answer Take test on this topic

Vector add and print elements
What will be the output of the following program?
import java.util.*;

public class Hp00001
{
    public static void main(String[] args)
    {
        Vector vect = new Vector();
        vect.addAll("2", "10", "3", "11", "7", "5", "4", "1", "22", "9", "6", "4", "8", "4", "4");
        Object[] obj = new Object[11];
        vect.copyInto(obj);
        Arrays.sort(obj);
        Collections.reverse(vect);
        for (int index = 0; index < 11; index++)
            if (vect.get(index) != obj[index])
                System.out.print(vect.get(index) + " ");
    }
}

A. 4 4 8 4 6 9 22 1 5 7 11 3 10 2
B. 4 4 8 6 9 22 1 4 5 7 11 3 10 2
C. 4 4 8 4 6 9 22 1 5 7 11 3 10
D. 4 4 8 6 9 22 1 4 5 7 11 3 10
E. Compilation Error or Runtime Error
Topic: Java Vector

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is E.

Vector implements a dynamic array. It is similar to ArrayList, but with two differences:
Vector is synchronized.Vector contains many legacy methods that are not part of the collections framework.

In this program,vect.addAll() adds all the specified elements into the vect object.
copyInto method is used to copy the components of this vector into the specified array.
The array must be big enough to hold all the objects in this vector otherwise an IndexOutOfBoundsException is thrown

Since number of elements in the vector is more than the size of the array, an IndexOutOfBoundsException (which is a RunTimeException)is thrown

Posted by Shashanka Mogaliraju    2015-03-13 09:05:24


Ans is E

Here the concept of vector is used.

--first of all object of vector is created.

--After that some elements are added in that vector by using add all method.

--Then,array of objects with length 11 is created and copyto() method is used to copy vector elements into object array but it throws Compilation Error.

--As number of elements are greater than vector elements so it throws compilation error.

OUTPUT:
Compilation Error

Posted by Mânïshå Mùlchåndânï    2015-03-13 17:08:11


This dose is now closed and the winners are Shashanka Mogaliraju for the 'Popular Comment'. The 'lucky liker' is Cool Pawan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-16 02:04:48


© meritcampus 2019

All Rights Reserved.

Open In App