ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
enum Dir { N, E, S, W }
// in main:
System.out.print(Dir.values()[2] + " " + Dir.valueOf("W").ordinal() + " ");
System.out.print(Dir.valueOf("w"));- A. "S 3 " is printed, then IllegalArgumentException is thrown
- B. S 3 W
- C. "E 4 " is printed, then IllegalArgumentException is thrown
- D. S 3 null
Correct answer: A. "S 3 " is printed, then IllegalArgumentException is thrown
values() is in declaration order (index 2 = S) and ordinals start at 0 (W = 3). valueOf is case-sensitive and throws IllegalArgumentException for a name that is not a constant; it never returns null.
More OOP with C++ and Java questions
- 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"); }…
- What is printed? java // static method of the main class: static int f() { try { throw new RuntimeException("t"); } finally { return 7; }…