ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
class K {
static int v = 10 / Integer.parseInt("0");
}
// in main:
try { System.out.print(K.v); }
catch (ArithmeticException e) { System.out.print("AE"); }
catch (ExceptionInInitializerError e) {
System.out.print("EIIE " + e.getCause().getClass().getSimpleName());
}- A. EIIE ArithmeticException
- B. AE
- C. EIIE NumberFormatException
- D. 0
Correct answer: A. EIIE ArithmeticException
An exception escaping a static initializer is wrapped in ExceptionInInitializerError, whose cause is the original ArithmeticException (division by zero). parseInt("0") itself succeeds.
More OOP with C++ and Java questions
- What is printed? java // static method of the main class: static void down() { down(); } // in main: try { down(); } catch (Exception e) {…
- What happens when this class is compiled? java class A { A() { this(1); } A(int x) { this(); } }
- What is printed? java class A { void f() { System.out.print("Af "); g(); } void g() { System.out.print("Ag "); } } class B extends A {…
- What is printed? java class Pt { int x; Pt(int x) { this.x = x; } public boolean equals(Object o) { return o instanceof Pt && ((Pt) o).x…
- What is printed? java interface Greeter { String name(); default String greet() { return "Hi " + name(); } } class G implements Greeter {…
- Which line fails to compile? java abstract class Shape { abstract double area(); } // in main: Shape a = new Shape(); // L1 Shape b = new…