Given:
1. public class Rainbow {
2. public enum MyColor {
3. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);
4. private final int rgb;
5. MyColor(int rgb) { this.rgb = rgb; }
6. public int getRGB() { return rgb; }
7. };
8. public static void main(String[] args) {
9. //insert code here
10. }
11. }
Which code fragment, inserted at line 19, allows the Rainbow class to compile?
A.
MyColor skyColor = BLUE;
B.
MyColor treeColor = MyColor.GREEN;
C.
if(RED.getRGB() < BLUE.getRGB()) { }
D.
Compilation fails due to other error(s) in the code.
E.
MyColor purple = new MyColor(0xff00ff);
F.
MyColor purple = MyColor.BLUE + MyColor.RED;
Explanation:
A:
Main.java:9: cannot find symbol
symbol : variable BLUE
location: class Rainbow
MyColor skyColor = BLUE;
^
1 errorB:
Compiled successfullyC:
Main.java:9: cannot find symbol
symbol : variable RED
location: class Rainbow
if(RED.getRGB() < BLUE.getRGB()) { }
^
Main.java:9: cannot find symbol
symbol : variable BLUE
location: class Rainbow
if(RED.getRGB() < BLUE.getRGB()) { }
^
2 errorsD:
Other code compiled successfully. This option is not correct.E:
Main.java:9: enum types may not be instantiated
MyColor purple = new MyColor(0xff00ff);
^
1 errorF:
Main.java:9: operator + cannot be applied to Rainbow.MyColor,Rainbow.MyColor
MyColor purple = MyColor.BLUE + MyColor.RED;
^
1 error
A is the correct answer
Correct answer is B
That’s right. Option B is the correct one.