What is the output for the below code ? public enum Test { BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45); private int hh; private int mm; Test(int hh, int mm) { assert (hh >= 0 && hh <= 23) : "Illegal hour."; assert (mm >= 0 && mm <= 59) : "Illegal mins."; this.hh = hh; this.mm = mm; } public int getHour() { return hh; } public int getMins() { return mm; } public static void main(String args[]){ Test t = new BREAKFAST; System.out.println(t.getHour() +":"+t.getMins()); } }

  1. 7:30

  2. Compile Error - an enum cannot be instantiated using the new operator

  3. 12:50

  4. 19:45


Correct Option: B

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) 7:30 - This option is incorrect. The code attempts to create an instance of the BREAKFAST enum constant, but it does not compile, so this option cannot be the correct answer.

Option B) Compile Error - an enum cannot be instantiated using the new operator - This option is correct. The code attempts to create an instance of the BREAKFAST enum constant using the new operator, which is not allowed for enums. Enums are implicitly final and their instances are predefined and finite. They cannot be instantiated using the new operator.

Option C) 12:50 - This option is incorrect. The code does not create an instance of the LUNCH enum constant, so this option cannot be the correct answer.

Option D) 19:45 - This option is incorrect. The code does not create an instance of the DINNER enum constant, so this option cannot be the correct answer.

The correct answer is: B. Compile Error - an enum cannot be instantiated using the new operator. This option is correct because the code attempts to create an instance of an enum constant using the new operator, which is not allowed for enums.

Find more quizzes: