ProgrammingVery hardHCL · Cognizant GenC

What does this Java method do when called?

What does this Java method do when called?
static int f() {
    try { throw new RuntimeException("a"); }
    finally { return 5; }
}
  1. A. Throws RuntimeException
  2. B. Compile error: missing return
  3. C. Returns 0
  4. D. Returns 5, silently discarding the exception

Correct answer: D. Returns 5, silently discarding the exception

A return inside finally overrides any pending exception, so the method completes normally with 5.

More OOP with C++ and Java questions

Exams that ask this topic