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? (Choose two.)

  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() ) {}


Correct Option: B,F

AI Explanation

To answer this question, let's review each of the given options:

Option A) Color skyColor = BLUE; This option assigns the enum constant "BLUE" to the variable "skyColor". This is incorrect because "BLUE" is a member of the "Color" enum and should be accessed as "Color.BLUE".

Option B) Color treeColor = Color.GREEN; This option correctly assigns the enum constant "GREEN" from the "Color" enum to the variable "treeColor". This is a valid way to access enum constants.

Option C) Color purple = new Color(0xff00ff); This option attempts to create a new enum constant using the constructor of the "Color" enum. However, enum constants are predefined and cannot be created using the constructor. This is incorrect.

Option D) if (RED.getRGB() < BLUE.getRGB()) {} This option correctly accesses the "getRGB()" method of the "RED" and "BLUE" enum constants and performs a comparison. This is a valid way to access enum constants and their methods.

Option E) Color purple = Color.BLUE + Color.RED; This option attempts to add the enum constants "BLUE" and "RED". However, enum constants cannot be added together using the "+" operator. This is incorrect.

Option F) if (Color.RED.ordinal() < Color.BLUE.ordinal()) {} This option correctly accesses the "ordinal()" method of the "RED" and "BLUE" enum constants and performs a comparison. This is a valid way to access enum constants and their methods.

Therefore, the correct options are B) Color treeColor = Color.GREEN; and F) if (Color.RED.ordinal() < Color.BLUE.ordinal()).

Find more quizzes: