250+ TOP MCQs on Data Type-Enums and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Data Structures-Enums”.

1. What is the order of variables in Enum?
a) Ascending order
b) Descending order
c) Random order
d) Depends on the order() method

Answer: a
Clarification: The compareTo() method is implemented to order the variable in ascending order.

2. Can we create an instance of Enum outside of Enum itself?
a) True
b) False

Answer: b
Clarification: Enum does not have a public constructor.

3. What will be the output of the following Java code?

  1.     enum Season 
  2.     {
  3.         WINTER, SPRING, SUMMER, FALL
  4.     };
  5.     System.out.println(Season.WINTER.ordinal());

a) 0
b) 1
c) 2
d) 3

Answer: a
Clarification: ordinal() method provides number to the variables defined in Enum.

4. If we try to add Enum constants to a TreeSet, what sorting order will it use?
a) Sorted in the order of declaration of Enums
b) Sorted in alphabetical order of Enums
c) Sorted based on order() method
d) Sorted in descending order of names of Enums

Answer: a
Clarification: Tree Set will sort the values in the order in which Enum constants are declared.

5. What will be the output of the following Java code snippet?

  1. class A
  2. {
  3.  
  4. }
  5.  
  6. enum Enums extends A
  7. {
  8.     ABC, BCD, CDE, DEF;
  9. }

a) Runtime Error
b) Compilation Error
c) It runs successfully
d) EnumNotDefined Exception

Answer: b
Clarification: Enum types cannot extend class.

6. What will be the output of the following Java code snippet?

  1.  enum Levels 
  2. {
  3.     private TOP,
  4.  
  5.     public MEDIUM,
  6.  
  7.     protected BOTTOM;
  8. }

a) Runtime Error
b) EnumNotDefined Exception
c) It runs successfully
d) Compilation Error

  250+ TOP MCQs on Thread class and Answers

Answer: d
Clarification: Enum cannot have any modifiers. They are public, static and final by default.

7. What will be the output of the following Java code snippet?

  1. enum Enums
  2. {
  3.     A, B, C;
  4.  
  5.     private Enums()
  6.     {
  7.         System.out.println(10);
  8.     }
  9. }
  10.  
  11. public class MainClass
  12. {
  13.     public static void main(String[] args)
  14.     {
  15.         Enum en = Enums.B;
  16.     }
  17. }

a)

   10
   10
   10

b) Compilation Error
c)

   10
   10

d) Runtime Exception

Answer: a
Clarification: The constructor of Enums is called which prints 10.

8. Which method returns the elements of Enum class?
a) getEnums()
b) getEnumConstants()
c) getEnumList()
d) getEnum()

Answer: b
Clarification: getEnumConstants() returns the elements of this enum class or null if this Class object does not represent an enum type.

9. Which class does all the Enums extend?
a) Object
b) Enums
c) Enum
d) EnumClass

Answer: c
Clarification: All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.

10. Are enums are type-safe?
a) True
b) False

Answer: a
Clarification: Enums are type-safe as they have own name-space.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top