Given static int f() { try { return 1; } finally { return 2; } }, what does System.out.println(f()); print?
Correct answer C. 2
Explanation
When a finally block completes abruptly with its own return, that outcome replaces whatever the try block was doing, so the pending return of 1 is discarded and 2 is printed. A return in finally would also swallow a pending exception. It compiles, but it is regarded as poor practice and linters commonly flag it.
Answer checked How we write and check questions
Discussion