Menu
Topics Index
...
`

Cloning


If you need explanation Read this topic

If you need Answer Take test on this topic

Cloning
What will be the output of the following program?
import java.util.*;
public class Cloning {
    public static void main(String[] args) {
        TreeSet tree = new TreeSet();
        tree.add("Roots");
        tree.add("Stem");
        tree.add("Branchs");
        TreeSet newtree = (TreeSet) tree.clone();
        newtree.add("Leaves");
        System.out.println(newtree);
    }
}

A. [Roots, Stem, Branchs, Leaves]
B. [Branchs, Leaves, Roots, Stem]
C. [Roots, Stem, Branchs]
D. Compilation Error or Runtime Error
Topic: Java TreeSet - TreeSet Examples in Java

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.

Ans is D.

When we are using clone() method we have to handle CloneNotSupportedException using try-catch or throws keyword otherwise it raises compilation error.

Posted by Gopinath Manchikanti    2015-01-05 14:31:49


ANS IS B

Here TreeSet Data structure is used.

--Here,default Constructor of treeset is created.
--->TreeSet()
Constructs a new, empty tree set, sorted according to the natural ordering of its elements.

--Add() method is used to insert the String elements into this treeset.
--->add(E e)
Adds the specified element to this set if it is not already present.

--Then Clone() method is used which creates copy of this treeset and assigned to another treeset instance.
--->clone()
Returns a shallow copy of this TreeSet instance.

--Then one more string element is added into this newtree object and printed.

Output will be:
[Branchs,Leaves,Roots,Stem]
Ascending order of the elements is maintained.

Posted by Mânïshå Mùlchåndânï    2015-01-05 17:27:20


This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment'. The 'lucky liker' is Maheshwari Natarajan. 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-01-06 05:30:41


© meritcampus 2019

All Rights Reserved.

Open In App