Menu
Topics Index
...
`

EnumSet2133


If you need explanation Read this topic

If you need Answer Take test on this topic

EnumSet2133
What will be the output of the following program?
import java.util.*;
public class Nayasa {
    enum HOTEL {
        IDLY, DOSA, PURI, VADA;
    }
    public static void main(String[] args) {
        EnumSet es = EnumSet.of(HOTEL.DOSA);
        es.add(HOTEL.PURI);
        System.out.println(es);
        es = EnumSet.allOf(null);
        System.out.println(es);
    }
}

A. (EMPTY)
B. null
C. [DOSA, PURI] 
null
D. [DOSA, PURI]
[DOSA, PURI]
E. Some other output
F. Compilation Error or Runtime Error
Topic: Java EnumSet

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 C, after printing dosa,puri enum set is set to  null and it is printed.

Posted by Krishna Chaitanya    2014-01-10 03:51:16


Answer is C.... First SOP stmt prints [Dosa,Puri]..bcoz usinf of method dosa and idly are added to the set...then after 1st SOP,allof method is used..this method creates an enum set containing all of the elements in the specified element type(Here it is null)...Thus 2nd SOP will be null....

Posted by Sneha Rapool    2014-01-10 04:21:57


The correct answer is F - Runtime Error. This is because when we pass null to EnumSet.allOf method it throws a NullPointerException, because it can not find which type of enum to return. If we passed Hotel.class instead of null, then it will print '[IDLY, DOSA, PURI, VADA]' as the second line. Unfortunately no winner for this. :-(

Posted by Merit Campus    2014-01-13 13:33:18


© meritcampus 2019

All Rights Reserved.

Open In App