Menu
Topics Index
...
`

Cloning Vectors


If you need explanation Read this topic

If you need Answer Take test on this topic

Cloning Vectors
What will be the output of the following program?
import java.util.*;

public class Cloning
{
    public static void main(String args[])
    {
        Vector<String> vector = new Vector<String>();
        vector.add("One");
        vector.add("Two");
        vector.add("Three");
        vector.add("Four");
        System.out.println("Original vector: " + vector);

        Vector<String> copy = (Vector<String>) vector.clone();
        System.out.print("After cloning: " + copy);
    }
}

A. Original vector: [One, Two, Three, Four]
After cloning: [One, Two, Three, Four]
B. Original vector: [One, Two, Three, Four]
After cloning: [One, Two, Three, Four]
C. Original vector: [One, Two, Three, Four]
After cloning: [Four, Three, Two, One]
D. Compilation Error
E. 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.

Both A and B are same which is the answer...vector can grow and shrink automatically when overflow and deletion happens...
In the program,we are adding the elements into vector and printing the variable prints the elements in same oredr as they are inserted.
clone() method creates and returns a copy of the object, with the same class and with all the fields having the same values..
So in our case,it also prints the all the elements similar to that of vector variable

Posted by Shashanka Mogaliraju    2015-02-26 13:48:47


here option is both A and B
first excecution start from main we creating vector object it holds  only strings
and we adding strings to vector object using add method;
vector.add("One");
vector.add("Two");
vector.add("Three");
vector.add("Four");
means vector object  consist of four strings[One, Two, Three, Four]
and printing the vector object its prints=>orginal vector:[One, Two, Three, Four]
Vector<String> copy = (Vector<String>) vector.clone();
after we creating another Vector reference name copy(which holds only strings)
and assinging a method called vector.clone(); means clone method creates of exact copy of vector object means is creates what is vector object holding
vector object is holding set strings called[One, Two, Three, Four] same set strings was created and refernece will be assigned to Copy
and Copy reference refering strings=>[One, Two, Three, Four]
and printing the copy reference its will prints=> After cloning: [One, Two, Three, Four]

so result is;
Original vector: [One, Two, Three, Four]
After cloning: [One, Two, Three, Four]  and options  A and B are provinding same result both are correct

Posted by Goutham Singarapu    2015-02-26 14:07:37


Ans is A and B  here we have theVector concept,Vector implements the List interface and RandomAccess interface..Vectors are growable in the nature and we can add hetrogenious elements to the vector and Vectors are Thread-safe ,vecctor is legacy class
Execution:
in this program we create the object for the Vector which can store the objects of the Strings and  and next we added some strings objects to the Vector next by using the clone() we are getting the same copy of the object into another Vector object
So when we print the data in the both the cases same data get printed

Posted by Uday Kumar    2015-02-26 16:58:18


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. 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-03 03:20:13


© meritcampus 2019

All Rights Reserved.

Open In App