Menu
Topics Index
...
`

ENUM and Switch


If you need explanation Read this topic

If you need Answer Take test on this topic

ENUM and Switch
What will be the output of the following program?
enum Employees {
    TCS, WIPRO, Cerone, Infosys, Polaris
}
public class SwitchCase {
    public static void main(String args[]) {
        Employees student;
        student = Employees.Cerone;
        switch (student) {
            case TCS :
                System.out.print("Ravi is working here.");
                break;
            case WIPRO :
                System.out.print("Raghu is working here.");
                break;
            case Cerone :
                System.out.print("Ramesh is working here.");
                break;
            case Infosys :
                System.out.print("Rakesh is working here.");
                break;
            case Polaris :
                System.out.print("Ram is working here.");
                break;
            case HCL :
                System.out.print("Ram is working here.");
                break;
        }
    }
}

A. Ramesh is working here.
B. Some other output
C. Compilation Error because the Employeeenum declared outside the class SwitchCase.
D. Compilation Error or Runtime Error for some other reason.
Topic: Java Enumeration

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.

A
enum is user defined type
enum members have equivalent integer values (by default starts from 0)
TCS=0 WIPRO=1 Cerone=2.. and so on
Also conversion from enum-->to-->int is automatic
(int-->enum requires casting)
output will be : Ramesh is working (i.e.case 2)
break; will take control flow out

Posted by Tushar Ghude    2014-07-30 09:37:20


A

Posted by Arul Palanisamy    2014-07-30 10:52:28


A

Posted by Mani Deep    2014-07-30 13:06:40


Ans is A as employees.cerone value is assigned to student i.e 2 and case cerone is executed and break stmt is executed and sos stmt is printed (Tushar has already won this week )

Posted by Bharath Yelchuri    2014-07-30 14:19:44


^LOL

Posted by Tushar Ghude    2014-07-30 15:35:39


No winner for this dose. Correct option is 'D' :(.For more details about Enum Constructors, Switching on Enum go to-
http://java.meritcampus.com/t/353/Enum-constructors--switching-on-enum

Posted by Merit Campus    2014-07-31 04:43:54


Merit Campus you said The answer that is fine.. but Please elaborate 1 or 2 points in the comments itself rather than giving direct link..

Posted by Mani Deep    2014-07-31 06:53:44


^ enum can take only one of the 5 defined values HCL is not defined

Posted by Tushar Ghude    2014-07-31 13:21:01


As Tushar Ghude pointed out, it fails to compile because HCL is not defined.

Posted by Merit Campus    2014-07-31 15:23:03


Thanks for the feedback Mani Deep, we will try to give more details when rejecting the answer.

Posted by Merit Campus    2014-07-31 15:23:32


© meritcampus 2019

All Rights Reserved.

Open In App