Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


What can prevent a finally block from executing?

  1. An uncaught exception in the try block

  2. A return statement in the try block

  3. A call to System.exit()

  4. Nothing can prevent the finally block from executing

The correct answer is: A call to System.exit()

A call to System.exit() is an explicit way of terminating the entire Java program and therefore prevents any code, including the finally block, from executing after it. Options A and B may lead to the catch block being executed, but the finally block will still run. Option D is incorrect because the finally block will execute unless the program is terminated, which can be done through a call to System.exit().