Java OCA OCP Practice Question 1636

Question

What will be the result of attempting to compile and run the following code?.

public enum Main {
  C1("c1"), C2("c2"),
  C3("c3"), C4("c4");

  private String c;

  Main(String c) {// w  w  w.j a  v  a  2s.  c  o m
    this.c = c;
  }

  public static void main(String[] args) {
    System.out.println(C1);  // (1)
    System.out.println(C4);  // (2)
  }
}

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 errors in (1) and (2), as the constants must be qualified by the enum type name Main.
  • (c) The compiler reports errors in (1) and (2), as the constants cannot be accessed in a static context.
  • (d) The code compiles and prints: C1 C4
  • (e) The code compiles and prints: c1 c4
  • (f) None of the above.


(d)

Note

An enum type can be run as a standalone application.

The constants need not be qualified when referenced inside the enum type declaration.

The constants are static members.

The toString() method always returns the name of the constant, unless it is overridden.




PreviousNext

Related