Study and learn Interview MCQ Questions and Answers on Java Enum or Enumeration. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Enum before reading these objective questions.
Enumeration and Enum are one and the same in Java.
"enum". All the letters should be in lower case.
Java 1.5
An enum variable can only hold enum constants.
enum Car { SEDAN, HATCHBACK, SUV } public class EnumTest1 { public static void main(String[] args) { Car c1 = Car.SEDAN; System.out.println(c1.name() + ", " + c1); } }
enum Bus { DIESEL, ELECTRIC, HYBRID int mileage = 10; } public class EnumTest2 { public static void main(String[] args) { Bus bus = Bus.ELECTRIC; System.out.println(bus + ", " + bus.mileage); } }
If you want to add code other than enum-constants inside an ENUM class, the constants should end with a semicolon. Otherwise, the semicolon is not required.
enum Bus { DIESEL, ELECTRIC, HYBRID; //semicolon int mileage = 10; }
The STATIC variables and methods are common to all ENUM constants. There will be only one copy of the static variable shared by all enum-constants.
enum Dog { SMALL, MEDIUM, BIG; } Dog dg = Dog.SMALL; System.out.println(dg.ordinal()); //OUTPUT 0
enum Marks { AVERAGE, GOOD, EXCELLENT; } public class EnumTest3 { public static void main(String[] args) { Marks student2 = Marks.GOOD; //MISSING SWITCH STATEMENT } }
switch(student2) { case AVERAGE: System.out.println("Marks: 60"); break; case GOOD: System.out.println("Marks: 80"); break; case EXCELLENT: System.out.println("Marks: 90"); }
switch(student2) { case Marks.AVERAGE: System.out.println("Marks: 60"); break; case Marks.GOOD: System.out.println("Marks: 80"); break; case Marks.EXCELLENT: System.out.println("Marks: 90"); }
switch(student2) { case Marks.AVERAGE: System.out.println("Marks: 60"); break; case GOOD: System.out.println("Marks: 80"); break; case Marks.EXCELLENT: System.out.println("Marks: 90"); }
switch(student2) { case Marks[0]: System.out.println("Marks: 60"); break; case Marks[1]: System.out.println("Marks: 80"); break; case Marks[2]: System.out.println("Marks: 90"); }
The enum-constants should be used directly as switch-case constants without using enum-name.
NO. You can not pass values to enum-constructor from outside.
enum Ship { BOAT(5), SHIP(10), VESSEL(100); int capacity; Ship(int i) { capacity = i; } Ship(){} void show() {System.out.println("Capacity: " + capacity);} } public class EnumTest4 { public static void main(String[] args) { Ship s1 = Ship.BOAT; s1.show(); } }
Each ENUM constant has a separate copy of the instance variable "capacity". The constructor is called 3 times as there are 3 enum-constants inside.
enum Human { KID(10), BOY(30), MAN(60); Human(int weight) { System.out.println("Weight: " + weight); } } public class EnumTest5 { public static void main(String[] args) { Human he = Human.KID; } }
Weight: 10 Weight: 30 Weight: 60
Weight: 10 Weight: 10 Weight: 10
Weight: 0 Weight: 0 Weight: 0
If you try to create at least one Enum variable, the constructor is called for all enum-constants for the first time.
enum FRUIT { ORANGE, PEARS, PLUMS; }
for(FRUIT fr2: FRUIT.values()) System.out.println(fr2);
//new ENUM array FRUIT ary[] = new FRUIT[3]; ary[0] = FRUIT.ORANGE; ary[1] = FRUIT.PEARS; ary[2] = FRUIT.PLUMS; for(FRUIT fr: ary) System.out.println(fr);
You can call values() method to get an array of enum-constants. Here, you are using Advanced FOR Loop or FOR-EACH loop to loop through all constants.
Yes. Example is below.
interface Jam { void show(); } enum FRUIT implements Jam { ORANGE("Orange"), PEARS("Pears"), PLUMS("Plums"); String text=""; FRUIT(String fr){text=fr;} @Override public void show() { System.out.println(text + " Jam"); } } public class EnumTest7 { public static void main(String[] args) { FRUIT fru = FRUIT.ORANGE; fru.show(); } } //OUTPUT Orange Jam