Multiple choice technology programming languages

Given: 10. public class Fabric 11. public enum Color { 12. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff); 13. private final int rgb; 14. Color( int rgb) { this.rgb = rgb; } 15. public int getRGB() { return rgb; } 16. }; 17. public static void main( String[] argv) { 18. // insert code here 19. } 20. } Which two code fragments, inserted independently at line 18, allow the Fabric class to compile?

  1. Color skyColor = BLUE;

  2. Color treeColor = Color.GREEN;

  3. Color purple = new Color( 0xff00ff);

  4. if( RED.getRGB() < BLUE.getRGB() ) {}

  5. Color purple = Color.BLUE + Color.RED;

  6. if( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}

Reveal answer Fill a bubble to check yourself
B,F Correct answer
Explanation

Option B compiles because enum constants must be qualified with the enum type name (Color.GREEN). Option F compiles because enum constants have an ordinal() method returning their zero-based position. Options A and C fail - enum constants cannot be referenced without the enum name, and enums cannot be instantiated with new. Option D fails because enum constants must be qualified (RED.getRGB). Option E fails because enums cannot be added with + operator.

AI explanation

Color treeColor = Color.GREEN; compiles because enum constants must be accessed through the enum type name when referenced outside the enum body (here, in main), and Color.GREEN is exactly such a qualified reference. if( Color.RED.ordinal() &lt; Color.BLUE.ordinal() ) also compiles since ordinal() is a built-in method every enum constant has, returning its declaration position. The unqualified BLUE and RED.getRGB() fail to compile outside the enum body, and new Color(...) fails because enum constructors are implicitly private and can't be invoked with new.