Java System.exit() Method - Quiz
Total: 6 questions
1. What does the System.exit() method do, and what argument does it take?
What does the System.exit() method do, and what argument does it take?
System.exit() terminates the currently running Java Virtual Machine together with all of its threads, immediately ending the program. It takes a single int argument — the exit code (exit status) — that is returned to the operating system. By convention 0 means success and any non-zero value means an error. The call never returns control to the code that follows it.
2. What is an exit code, and what do the values 0 and 1 mean?
What is an exit code, and what do the values 0 and 1 mean?
The exit code is a number that a process returns to the operating system when it ends. By convention 0 means successful termination, 1 is the general code for abnormal termination, and other non-zero values denote application-specific errors. The code matters when a program is launched from shell scripts or CI systems, which use it to decide whether the run succeeded. On Unix-like systems only the lowest byte is kept, so the meaningful range is 0 to 255.
3. How does System.exit() differ from the return statement?
How does System.exit() differ from the return statement?
return exits only the current method and hands control back to the caller — the program keeps running. System.exit() ends the entire program: every thread stops and the JVM shuts down. A special case is return in main(): it finishes main(), but the JVM keeps running while live non-daemon threads remain, whereas System.exit() stops the JVM regardless.
4. Does the finally block run when System.exit() is called inside a try block?
Does the finally block run when System.exit() is called inside a try block?
No. System.exit() stops the JVM immediately without unwinding the call stack, so finally blocks and any code after try-catch never execute. Only registered shutdown hooks are run before the JVM terminates. This is a popular interview question, because resources you expected to close in finally stay open.
5. When should you avoid calling System.exit()?
When should you avoid calling System.exit()?
Avoid it in web applications and server-side code (it would bring down the whole application server with every deployed app), in libraries (library code should not decide when the application terminates — throw an exception instead), and in unit tests (it would kill the test runner process). In regular program logic, use return to leave a method and exceptions to report errors.
6. How is System.exit() different from Runtime.getRuntime().halt()?
How is System.exit() different from Runtime.getRuntime().halt()?
System.exit() (a shortcut for Runtime.getRuntime().exit()) starts the normal shutdown sequence: all registered shutdown hooks run first, then the JVM stops and returns the exit code to the OS. Runtime.getRuntime().halt() stops the JVM instantly, skipping shutdown hooks. Halt is reserved for extreme cases, for example when the shutdown sequence itself hangs.