Practice mode Java

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.