Java OCA OCP Practice Question 3069

Question

What is the output of the following program?

class Main {
   enum Directions { North, East, West, South };
   enum Cards { Spade, Hearts, Club, Diamond };
   public static void main(String []args) {
       System.out.println("equals: " + Directions.East.equals(Cards.Hearts));
       System.out.println("ordinals: " +
                       (Directions.East.ordinal() == Cards.Hearts.ordinal()));
   }
}
a)  equals: false//from www.  j  av  a2  s  . com
    ordinals: false

b) equals: true
   ordinals: false

c) equals: false
   ordinals: true

d) equals: true
   ordinals: true


c)

Note

the equals() method returns true only if the enumeration constants are the same.

In this case, the enumeration constants belong to different enumerations, so the equals() method returns false.

however, the ordinal values of the enumeration constants are equal since both are second elements in their respective enumerations.




PreviousNext

Related