ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
// static method of the main class:
static void down() { down(); }
// in main:
try { down(); }
catch (Exception e) { System.out.print("E"); }
finally { System.out.print("F"); }
System.out.print("X");
  1. A. EF X
  2. B. EFX
  3. C. F is printed, then the program ends with a StackOverflowError
  4. D. FX

Correct answer: C. F is printed, then the program ends with a StackOverflowError

StackOverflowError is an Error, not an Exception, so catch (Exception) does not catch it. finally still runs, then the error propagates out of main and the last print never runs.

More OOP with C++ and Java questions

Exams that ask this topic