Exercice :
public class Test {
enum Animal { "CATS", "DOGS", "ELEPHANT"};
public static void main(String [] args) {
Animal myAnimal = Animal.CATS;
if ( myAnimal == Animal.CATS) {
System.out.println("It's a CATS");
} else if (myAnimal.equals(Animal.DOGS)) {
System.out.println("It's a DOGS");
} else {
System.out.println("It's an elephant");
}
}
}
What is the result ? (choose one)
A It's a CATS
B It's a DOGS
C It's an elephant
D Compilation fails
E An exception is thrown at runtime
Solution :
La solution est D.La compilation échoue car la déclaration de l'enum est incorrecte.
La déclaration aurait du être la suivante : enum Animal { CATS, DOGS, ELEPHANT};
Aucun commentaire:
Enregistrer un commentaire