Question 4
Back to Exercise
What is the output of the following Java program?
enum Grade {
A_PLUS(90, "Outstanding"), A(75, "Excellent"), B(60, "Good"), C(40, "Pass"), F(0, "Fail");
final int min;
final String label;
Grade(int min, String label) { this.min = min; this.label = label; }
static Grade of(int marks) {
for (Grade g : values()) if (marks >= g.min) return g;
throw new IllegalArgumentException();
}
}
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int m : new int[]{95, 75, 74, 41, 12}) sb.append(Grade.of(m)).append(' ');
System.out.println(sb.toString().trim() + " | " + Grade.of(66).label);
}
}
- A A_PLUS A B C F | Good Correct
- B A_PLUS A A C F | Good
- C A_PLUS A B B F | Good
- D A_PLUS A B C F | Excellent
Correct Answer: Option A
Constants are checked from the highest threshold down; 74 falls below A's 75.
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!