Java OCA OCP Practice Question 1650

Question

Given the following code:.

package mypkg;/*from   w w  w  .java  2s.c om*/

enum Command {Compile, Test}            // (1)

public class Main {
 enum Command {Compile, Test}           // (2)
 static enum Military {
   INFANTRY, AIRFORCE;
   enum Command {Compile, Test}         // (3)
 }
 class Secret {
   enum Command {Compile, Test}         // (4)
 }
 static class Open {
   enum Command {Compile, Test}         // (5)
 }
 public static void declareWar() {
   enum Command {Compile, Test}         // (6)
 }
 public void declarePeace() {
   enum Command {Compile, Test}         // (7)
 }

Which enum declarations are not legal?.

Select the three correct answers.

  • (a) The enum declaration at (1) is not legal.
  • (b) The enum declaration at (2) is not legal.
  • (c) The enum declaration at (3) is not legal.
  • (d) The enum declaration at (4) is not legal.
  • (e) The enum declaration at (5) is not legal.
  • (f) The enum declaration at (6) is not legal.
  • (g) The enum declaration at (7) is not legal.


(d), (f), and (g)

Note

A nested enum type must be declared inside a static member type, like (2), (3) and (5).

Note that a nested enum type is implicitly static, and the keyword static is not mandatory in this case.

An enum type cannot be local, as static member types cannot be declared locally.




PreviousNext

Related