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());- A. M LOW1 HIGH3 S 1
- B. M S LOW1 HIGH3 1
- C. LOW1 HIGH3 S M 1
- 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
- What is printed? java enum Dir { N, E, S, W } // in main: System.out.print(Dir.values()[2] + " " + Dir.valueOf("W").ordinal() + " ")…
- What is printed? java class Box implements Cloneable { int[] data = {1, 2}; int n = 5; public Box clone() throws…
- What is printed? java // static method of the main class: static void swap(int[] a, int[] b) { int[] t = a; a = b; b = t; a[0] = 100; } //…
- What is printed? java Object o = "hi"; String s = (String) o; Integer n = null; System.out.print((n instanceof Integer) + " " + (o…
- What is printed? java class A { static int v = 1; static int get() { return v; } } class B extends A { static int v = 2; } // in main…
- What happens when this code is compiled? java try { new java.io.FileReader("x.txt"); } catch (Exception e) { System.out.print("E"); }…