Java OCA OCP Practice Question 42

Question

Which of the following are legal enums?

A.  enum Mammal { LION, TIGER, BEAR } 

B.  enum Mammal { 
      int age; /*from   w w  w .ja  v  a2s  .  c o  m*/
      LION, TIGER, BEAR; 
    } 

C.  enum Mammal { 
      LION, TIGER, BEAR; 
      int weight; 
    } 

D.  enum Mammal { 
      LION(450), TIGER(450), BEAR; 
      int weight; 

      Mammal(int w) { 
        weight = w; 
      } 
    } 

E.  enum Mammal { 
      LION(450), TIGER(450), BEAR; 
      int weight; 

      Mammal() { } 

      Mammal(int w) { 
        weight = w; 
      } 
    } 


C, E.

Note

A is illegal because the list of names must be terminated by a semicolon.

B is illegal because the list of names must be the first element in the enum body.

C is a legal enum that contains, in addition to its name list, a variable.

D is illegal because the declaration of Bear requires the existence of a no-args constructor. E fixes the bug in D by adding a no-args constructor.




PreviousNext

Related