Wednesday, February 3, 2010

SCJP exercise : enum

Exercise :



Given the following :

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 :


The solution is A.There is no errors.You can use enum with == or with the method equals.

No comments:

Post a Comment