Java Enum Interview MCQ Questions and Answers

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.



1) Java Enum is also called ___.
A) Enumeration
B) Numeration
C) Iteration
D) None of the above
Answer [=]
A
Explanation:

Enumeration and Enum are one and the same in Java.

2) Which is the keyword used to create an ENUM type in Java?
A) enum
B) Enum
C) enumeration
D) Enumeration
Answer [=]
A
Explanation:

"enum". All the letters should be in lower case.

3) An ENUM type in Java is like a ____.
A) interface
B) class
C) abstract class
D) None of the above
Answer [=]
B
4) An ENUM class in Java can contain ______.
A) constructors
B) methods
C) variables and constants
D) All the above
Answer [=]
D
5) Which is true about enum constants in Java?
A) Enum constants have index.
B) The index starts with zero (0).
C) Enum constant can be accessed using a DOT (.) operator
D) All the above
Answer [=]
D
6) Java enums are introduced in which version?
A) Java 1.4
B) Java 1.5
C) Java 6
D) Java 8
Answer [=]
B
Explanation:

Java 1.5

7) An enum variable in java can take ___ as the assigned value in Java.
A) Any integer
B) Any non null object
C) Enum constant only
D) None of the above
Answer [=]
C
Explanation:

An enum variable can only hold enum constants.



8) The name of an ENUM or the name of ENUM constant can be ____ in Java.
A) A string of characters that starts with an Underscore, Dollar or Alphabet
B) A string of characters that can contain numbers at any position other than zero
C) A string of characters that can be an existing keyword
D) All the above
Answer [=]
D
9) What is the output of the below Java program with Enums?
enum Car
{
  SEDAN, HATCHBACK, SUV
}
public class EnumTest1
{
  public static void main(String[] args)
  {
    Car c1 = Car.SEDAN;
    System.out.println(c1.name() + ", " + c1);
  }
}
A) SEDAN, 0
B) SEDAN, SEDAN
C) SEDAN, 
D) None of the above
Answer [=]
B
10) What is the output of the below Java program with Enums?
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);
  }
}
A) ELECTRIC, 0
B) ELECTRIC, 10
C) 1, 0
D) Compiler error
Answer [=]
D
Explanation:

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;
}
11) Can you define STATIC variables and STATIC methods inside an ENUM in Java?
A) Yes
B) No
C) -
D) -
Answer [=]
A
Explanation:

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.

12) An ENUM can be defined in the place ____ in Java?
A) Inside a class but outside of methods
B) Outside a class or interface
C) Inside an Interface but outside methods (abstract if any)
D) All the above
Answer [=]
D
13) An ordinal() method of an ENUM returns _____.
A) Hash value
B) Enum constant length
C) Enum constant index which is zero based
D) None of the above
Answer [=]
C
Explanation:
enum Dog
{
  SMALL, MEDIUM, BIG;
}
Dog dg = Dog.SMALL;
System.out.println(dg.ordinal());
//OUTPUT
0
14) Which is the correct way of implementing ENUM constants inside SWITCH statement in Java below?
enum Marks
{
  AVERAGE, GOOD, EXCELLENT;
}
public class EnumTest3
{
  public static void main(String[] args)
  {
    Marks student2 = Marks.GOOD;

    //MISSING SWITCH STATEMENT
  }
}
A)
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");
}
B)
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");
}
C)
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");
}
D)
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");
}
Answer [=]
A
Explanation:

The enum-constants should be used directly as switch-case constants without using enum-name.



15) Can you pass values to ENUM constructors in Java?
A) No
B) Yes
C) -
D) -
Answer [=]
A
Explanation:

NO. You can not pass values to enum-constructor from outside.

16) What is the output of the below Java code with Enums and Constructors?
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();
  }
}
A) Capacity: 5
B) Capacity: 0
C) Capacity: 10
D) Capacity: 100
Answer [=]
A
Explanation:

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.

17) What is the output of the below Java code with constructors?
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;
  }
}
A)
Weight: 10
Weight: 30
Weight: 60
B)
Weight: 10
Weight: 10
Weight: 10
C)
Weight: 0
Weight: 0
Weight: 0
D) No output
Answer [=]
A
Explanation:

If you try to create at least one Enum variable, the constructor is called for all enum-constants for the first time.

18) Which is the correct way of looping through all ENUM constants in Java below?
enum FRUIT
{
  ORANGE, PEARS, PLUMS;
}
A)
for(FRUIT fr2: FRUIT.values())
System.out.println(fr2);
B)
//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);
C) Both A and B
D) None of the above
Answer [=]
C
Explanation:

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.

19) An ENUM can implement an INTERFACE. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

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