ProgrammingVery hardWipro Elite · TCS NQT Prime

What is printed?

What is printed?
class T {
    static int v = f();
    static int f() { throw new RuntimeException(); }
}
// main:
for (int k = 0; k < 2; k++) {
    try { new T(); }
    catch (Throwable e) { System.out.print(e.getClass().getSimpleName() + ","); }
}
  1. A. RuntimeException,RuntimeException,
  2. B. ExceptionInInitializerError,ExceptionInInitializerError,
  3. C. RuntimeException,NoClassDefFoundError,
  4. D. ExceptionInInitializerError,NoClassDefFoundError,

Correct answer: D. ExceptionInInitializerError,NoClassDefFoundError,

A failed static initializer throws ExceptionInInitializerError once and leaves the class erroneous; later uses throw NoClassDefFoundError.

More OOP with C++ and Java questions

Exams that ask this topic