Java OCA OCP Practice Question 3229

Question

What will be the result of compiling and running the following code?

public enum Main {
  PLATINUM(20), GOLD(10), SILVER(5), BASIC(0);
  private double extra;

  Main(double extra) {
    this.extra = extra;
  }//from w  ww .j av a 2 s  .  co m

  public static void main (String[] args) {
    System.out.println(GOLD.ordinal() > SILVER.ordinal());
    System.out.println(max(GOLD,SILVER));
    System.out.println(max2(GOLD,SILVER));
  }

  public static Main max(Main c1, Main c2) {
    Main maxFlyer = c1;
    if (c1.compareTo(c2) < 0)
      maxFlyer = c2;
    return maxFlyer;
  }

  public static Main max2(Main c1, Main c2) {
    Main maxFlyer = c1;
    if (c1.extra < c2.extra)
      maxFlyer = c2;
    return maxFlyer;
  }
}

Select the one correct answer.

(a)  The program will compile and print:

    false//ww w .  jav a2s. c  o m
     SILVER
    GOLD

(b)  The program will compile and print:

    true
    GOLD
     SILVER

(c)  The program will compile and print:

    true
    GOLD
    GOLD

(d)  The  program  will  not  compile,  since  the  enum  type  Main  does  not
    implement the Comparable interface.


(a)

Note

All enum types implement the Comparable interface.

Comparison is based on the natural order, which in this case is the order in which the constants are specified, with the first one being the smallest.

The ordinal value of the first enum constant is 0, the next one has the ordinal value 1, and so on.




PreviousNext

Related