Java OCA OCP Practice Question 1642

Question

What will be the result of compiling and running the following code?.

public enum Main {
  RED("Stop"), YELLOW("Caution"), GREEN("Go");

  private String action;

  Main(String action) {/*from   w ww  .  ja  v  a 2s.co  m*/
    this.action = action;
  }

  public static void main(String[] args) {
    Main green = new Main("Go");
    System.out.println(GREEN.equals(green));
  }
}

Select the one correct answer.

  • (a) The code will compile and print: true.
  • (b) The code will compile and print: false.
  • (c) The code will not compile, as an enum type cannot be instantiated.
  • (d) An enum type does not have the equals() method.


(c)

Note

All enum types override the equals() method from the Object class.

The equals() method of an enum type compares its constants for equality according to reference equality (same as with the == operator).

This equals() method is final.




PreviousNext

Related