Question 1
Back to Exercise
What is the output of the following Java program?
enum Signal { RED, AMBER, GREEN }
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (Signal s : Signal.values()) sb.append(s.name().charAt(0)).append(s.ordinal());
System.out.println(sb + " " + Signal.valueOf("GREEN").compareTo(Signal.RED));
}
}
- A R0A1G2 2 Correct
- B R1A2G3 2
- C R0A1G2 -2
- D RAG 2
Correct Answer: Option A
values() returns constants in declaration order; compareTo subtracts ordinals.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!