Java OCA OCP Practice Question 2333

Question

Select the correct statement(s) based on the following code:

enum Command { /* w  w  w  .  jav a 2s. c om*/
    ASSERT(1.4),                             // line1 
    DO, IF, WHILE;                           // line2 
    double version = 1.0;                    // line3 

    Command() {                             // constructor 1 
        this.version = 1.0;                  // constructor 1 
    }                                        // constructor 1 

    Command(double version) {               // constructor 2 
        this.version = version;              // constructor 2 
    }                                        // constructor 2 

    public static void main(String args[]) { 
        Command[] commands = Command.values(); 
        for (Command val:commands) 
           System.out.println(val); 
    } 
} 
  • a The enum commands won't compile due to code at (#1).
  • b The enum commands won't compile due to code at either (#2) or (#3).
  • c If you swap the complete code at (#1) and (#2) with code at (#3), enum commands will compile successfully.
  • d The enum commands will fail to compile due to the declaration of multiple constructors.
  • e None of the above


e

Note

The code compiles successfully.

An enum can define and use multiple constructors.

The declaration of enum constants must follow the opening brace of the enum declaration.

It can't follow the definition of variables or methods.




PreviousNext

Related