Multiple choice technology programming languages

Given: 11. public class Ball { 12. public enum Color { RED, GREEN, BLUE }; 13. public void foo() { 14. // insert code here 15. { System.out.println(c); } 16. } 17. } Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?

  1. for( Color c : Color.values())

  2. for( Color c = RED; c <= BLUE; c++)

  3. for( Color c; c.hasNext() ; c.next())

  4. for( Color c = Color[0]; c <= Color[2]; c++)

  5. for( Color c = Color.RED; c <= Color.BLUE; c++)

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

To iterate over enum constants RED, GREEN, BLUE, use the enhanced for-loop with Color.values() which returns all enum constants. Option A is correct. Options B, C, D, E use incorrect syntax - enums are not array-indexable, don't support hasNext()/next() without iterator, and don't support <= comparison.