Java Enums MCQ - Multiple Choice Questions And Answers
Q. What is an enum in Java?
A. A data structureB. 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. classB. 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. YesB. No
Q. Which of the following is NOT a valid access modifier for enum constructors in Java?
A. publicB. protected
C. private
D. static
Q. Enum constants are implicitly static and final in Java.
A. TrueB. 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. YesB. No
Q. What is the purpose of the Predicate functional interface in Java 8?
A. To produce resultsB. 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" keywordB. 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. YesB. No
Q. How are enum constants accessed in Java?
A. Using the new keywordB. 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 constantsB. 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() methodB. 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() methodB. 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 nullB. 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 superclassB. 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 constructorsB. 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" keywordB. 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" keywordB. 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]); }