Java OCA OCP Practice Question 2673

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/*  w  w  w .j a  v a 2s.  c  o  m*/
  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