ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
enum Lvl { LOW, MID, HIGH }
// in main:
Lvl a = Lvl.valueOf("MID");
System.out.print(a.ordinal() + " " + (a.compareTo(Lvl.HIGH) < 0) + " " + Lvl.values().length);
  1. A. 2 true 3
  2. B. 1 false 3
  3. C. 1 true 3
  4. D. 1 true 2

Correct answer: C. 1 true 3

MID has ordinal 1; enums compare by ordinal, and MID comes before HIGH, so compareTo is negative (true); values() returns all 3 constants.

More OOP with C++ and Java questions

Exams that ask this topic