Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Hard
Question 25
Consider the code of Test.java file:
- package com.udayan.ocp;
- enum Flags {
- TRUE, FALSE;
- Flags() {
- System.out.println("HELLO");
- }
- }
- public class Test {
- public static void main(String[] args) {
- Flags flags = Flags.TRUE;
- }
- }
What will be the result of compiling and executing Test class?
-
A
HELLO is printed twice
-
B
None of the other options
-
C
Exception is thrown at runtime
-
D
HELLO is printed once
Reveal correct answer
Correct answer: A
Explanation
Enum constructor is invoked once for every constant.
For 'Flags.TRUE', enum constructor is invoked for TRUE as well as FALSE.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
