Java Questions & Answers – Data Type-Enums

This set of Java Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: d
Explanation: The order of appearance of variables in Enum is their natural order (the natural order means the order in which they are declared inside Enum type). However, 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
View Answer

Answer: b
Explanation: No, instances of Enum cannot be created outside of Enum boundary, because Enum does not have a public constructor.

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

advertisement
advertisement
  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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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?

advertisement
  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
View Answer

Answer: b
Explanation: Enum types cannot extend class.
advertisement

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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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()
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

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

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.