Search Tutorials


Java Enums MCQ - Multiple Choice Questions And Answers | JavaInUse

Java Enums MCQ - Multiple Choice Questions And Answers

Q. What is an enum in Java?

A. A data structure
B. A class
C. A special data type that enables for a variable to be a set of predefined constants
D. A method

Q. Which keyword is used to declare an enum in Java?

A. class
B. int
C. enum
D. interface

Q. Which of the following is a valid declaration of an enum in Java?

A. enum Colors {RED, GREEN, BLUE};
B. Colors {RED, GREEN, BLUE};
C. enum {RED, GREEN, BLUE};
D. enum Colors(RED, GREEN, BLUE);

Q. Can an enum have methods in Java?

A. Yes
B. No

Q. Which of the following is NOT a valid access modifier for enum constructors in Java?

A. public
B. protected
C. private
D. static

Q. Enum constants are implicitly static and final in Java.

A. True
B. False

Q. Which method is used to get the name of an enum constant in Java?

A. name()
B. value()
C. getName()
D. toString()

Q. Can enums implement interfaces in Java?

A. Yes
B. No

Q. What is the purpose of the Predicate functional interface in Java 8?

A. To produce results
B. To consume values
C. To filter elements based on a condition
D. To transform values

Q. How do you define a field inside an enum in Java?

A. Using the "field" keyword
B. Inside the constructor
C. Using the "=" operator
D. After the enum constants

Q. Can enum constants have different method implementations in an interface they implement?

A. Yes
B. No

Q. How are enum constants accessed in Java?

A. Using the new keyword
B. By calling the name of the enum constant
C. By using the valueOf() method
D. All of the above

Q. Which of the following is NOT a valid operation on an enum in Java?

A. Comparing two enum constants
B. Iterating over enum constants
C. Adding new constants at runtime
D. Switch-case statement based on enum constants

Q. Which of the following is a correct way to compare enum constants in Java?

A. Using the equals() method
B. Using the == operator
C. Using the compareTo() method
D. Using the compare() method

Q. How can you get the ordinal of an enum constant in Java?

A. Using the ordinal() method
B. Using the order() method
C. Using the index() method
D. Using the pos() method

Q. What happens when you use the valueOf() method on a string that does not match any enum constant?

A. It returns null
B. It throws an IllegalArgumentException
C. It throws a NullPointerException
D. It creates a new enum constant with the given String value

Q. Which of the following is true about enums in Java?

A. Enums can have a superclass
B. Enums cannot have constructors
C. Enums can be extended by other classes
D. Enums can implement interfaces

Q. Which of the following is true about enum constructors?

A. Enums can have public and private constructors
B. Enums can only have private constructors
C. Enums cannot have constructors
D. Enums can have default constructors

Q. How do you define constants inside an enum in Java?

A. Using the "const" keyword
B. Inside the enum constructor
C. Using the "enum" keyword
D. After the enum declaration

Q. How do you access enum constants in Java?

A. By using the "new" keyword
B. By calling the name of the enum constant
C. By using the valueOf() method
D. All of the above

Q. What is an Enum in Java?

A.
enum Color {
    RED, GREEN, BLUE
}
B.
class Color {
    public static final Color RED = new Color();
    public static final Color GREEN = new Color();
    public static final Color BLUE = new Color();
}
C.
class Color {
    RED, GREEN, BLUE
}
D.
interface Color {
    RED, GREEN, BLUE
}

Q. How to access an Enum constant in Java?

A.
enum Direction {
    NORTH, SOUTH, EAST, WEST
}

Direction dir = Direction.NORTH;
B.
enum Color {
    RED, GREEN, BLUE
}

Color col = Color.YELLOW;
C.
enum Type {
    SMALL, MEDIUM, LARGE
}

String size = Type.SMALL;
D.
enum Shape {
    CIRCLE, SQUARE, TRIANGLE
}

int sides = Shape.CIRCLE;

Q. Can Enum in Java have constructors?

A.
enum Size {
    SMALL(1), MEDIUM(2), LARGE(3);
    
    private int value;

    Size(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}
B.
enum Month {
    JANUARY, FEBRUARY, MARCH;

    Month() {
        System.out.println("Constructor called");
    }
}
C.
enum Fruit {
    APPLE, BANANA, ORANGE;

    public Fruit() {
        System.out.println("Constructor called");
    }
}
D.
enum Animal {
    DOG, CAT, RABBIT;
}

Q. Which keyword is used to define an Enum in Java?

A.
enum Type {
    SMALL, MEDIUM, LARGE
}
B.
class Size {
    public static final Size SMALL = new Size();
    public static final Size MEDIUM = new Size();
    public static final Size LARGE = new Size();
}
C.
interface Category {
    SMALL, MEDIUM, LARGE
}
D.
module Shape {
    CIRCLE, SQUARE, TRIANGLE
}

Q. How can you iterate over all constants in an Enum in Java?

A.
enum Season {
    SPRING, SUMMER, AUTUMN, WINTER
}

for (Season s : Season.values()) {
    System.out.println(s);
}
B.
enum Country {
    USA, CANADA, UK, FRANCE
}

for (int i = 0; i < Country.values().length; i++) {
    System.out.println(Country.values()[i]);
}
C.
enum Continent {
    NORTH_AMERICA, SOUTH_AMERICA, EUROPE, ASIA
}

for (Continent c : Continent) {
    System.out.println(c);
}
D.
enum Planet {
    MERCURY, VENUS, EARTH, MARS
}

for (int i = 0; i < Planet.values().length(); i++) {
    System.out.println(Planet.values()[i]);
}