ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
enum Level {
    LOW(1), HIGH(3);
    Level(int v) { System.out.print(name() + v + " "); }
    static { System.out.print("S "); }
}
// in main:
System.out.print("M ");
Level l = Level.HIGH;
System.out.print(l.ordinal());
  1. A. M LOW1 HIGH3 S 1
  2. B. M S LOW1 HIGH3 1
  3. C. LOW1 HIGH3 S M 1
  4. D. M HIGH3 S 1

Correct answer: A. M LOW1 HIGH3 S 1

The enum class is initialised on first use (after M). Its static initialisation runs in textual order: the constants first (each constant's constructor), then the static block. HIGH is the second constant, ordinal 1.

More OOP with C++ and Java questions

Exams that ask this topic