Multiple choice technology web technology

If I write System.exit (0); at the end of the try block, will the finally block still execute?

  1. Yes

  2. No

  3. Cannot be determined

  4. None of the above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

When System.exit(0) is called, the JVM terminates immediately. This bypasses all normal flow control including the finally block - the finally block will NOT execute. System.exit() halts the JVM completely, so no further code runs. Option A is incorrect because finally is only guaranteed for normal control flow and exceptions, not JVM termination. Option C is incorrect - the behavior is deterministic and well-defined.

AI explanation

System.exit(0) immediately terminates the JVM by calling native shutdown code — it does not return control to the calling method, so any pending finally block in that thread's call stack is skipped and never executes (the only exception is JVM shutdown hooks, which are a separate mechanism, not the finally block itself). So if System.exit(0) is the last statement executed in the try block, the finally block will NOT run — 'No' is correct. 'Yes' is wrong because it contradicts the fundamental behavior of System.exit, which is designed to halt the JVM abruptly rather than unwind the stack normally.