Java OCA OCP Practice Question 1638

Question

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

import java.util.Arrays;

public enum Main {
  ONE(1) { public String toString() { return "LOW"; } },        // (1)
  TWO(2),/* w w w .jav  a2s  . com*/
  THREE(3) { public String toString() { return "NORMAL"; } },   // (2)
  FOUR(4),
  FIVE(5) { public String toString() { return "HIGH"; } };      // (3)

  private int v;

  Main(int v) {
    this.v = v;
  }

  public static void main(String[] args) {
    System.out.println(Arrays.toString(Main.values()));
  }
}

Select the one correct answer.

  • (a) The code compiles, but reports a ClassNotFoundException when run, since an enum type cannot be run as a standalone application.
  • (b) The compiler reports syntax errors in (1), (2), and (3).
  • (c) The code compiles and prints:[LOW, TWO, NORMAL, FOUR, HIGH]
  • (d) The code compiles and prints:[ONE, TWO, THREE, FOUR, HIGH]
  • (e) None of the above.


(c)

Note

An enum type can be run as a standalone application.

(1), (2), and (3) define constant-specific class bodies that override the toString() method.

For constants that do not override the toString() method, the name of the constant is returned.




PreviousNext

Related