nerdexam
Oracle

1Z0-851 · Question #156

Given: 11. public class Rainbow { 12. public enum MyColor { 13. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff); 14. private final int rgb; 15. MyColor(int rgb) { this.rgb = rgb; } 16. public int…

The correct answer is B. MyColor treeColor = MyColor.GREEN. See the full explanation below for the reasoning.

Question

Given: 11. public class Rainbow { 12. public enum MyColor { 13. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff); 14. private final int rgb; 15. MyColor(int rgb) { this.rgb = rgb; } 16. public int getRGB() { return rgb; } 17. }; 18. public static void main(String[] args) { 19. // insert code here 20. } 21. } Which code fragment, inserted at line 19, allows the Rainbow class to compile?

Options

  • AMyColor skyColor = BLUE;
  • BMyColor treeColor = MyColor.GREEN;
  • Cif(RED.getRGB() < BLUE.getRGB()) { }
  • DCompilation fails due to other error(s) in the code.
  • EMyColor purple = new MyColor(0xff00ff);
  • FMyColor purple = MyColor.BLUE + MyColor.RED;

How the community answered

(41 responses)
  • A
    2% (1)
  • B
    76% (31)
  • C
    7% (3)
  • E
    2% (1)
  • F
    12% (5)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice