Menu
Topics Index
...
`

Add elements to Empty Set


If you need explanation Read this topic

If you need Answer Take test on this topic

Add elements to Empty Set
What values will set contain at the end of the program?
import java.util.*;

public class ClassMate
{
    public static void main(String args[])
    {
        Set<String> set = Collections.<String>emptySet();
        set.add("Computer");
        set.add("Laptop");
        set.add("Notepad");
        set.add("Tablet");
        set.add("Computer");
    }
}

A. (empty)
B. Computer, Notepad, Laptop, Tablet
C. Computer, Notepad, Laptop, Tablet, Computer
D. Compilation Error
E. Runtime Error
Topic: Java Collection Algorithms

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 E..here we are calling the emptySet() which returns the immutable set means we cant modify these set...Like Strings if we modify the strings than new object will created...

here it will return the immutable string means we  cant perform the modification operations like add(),remove( ) etc..here we are calling the add() so it will throw the UnsupportedOperationException..program will terminate abnormally

Posted by Uday Kumar    2015-01-08 15:38:18


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. 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-09 05:37:15


© meritcampus 2019

All Rights Reserved.

Open In App