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 exception must be handled when calling the sleep() method on a thread?

  1. InterruptedException

  2. IOException

  3. TimeOutException

  4. ExecutionException

The correct answer is: InterruptedException

The "InterruptedException" exception must be handled when calling the sleep() method on a thread. This exception is thrown when another thread interrupts the sleeping thread, causing it to be woken up and unable to complete its sleep. The other options are incorrect because - IOException is a checked exception related to input/output operations and is not related to thread sleep. - TimeOutException may seem like a valid option, but in reality it is not a standard exception in Java. It is a custom exception that may be defined by the developer. - ExecutionException is also not a valid option because it is a checked exception related to errors and exceptions that occur while running a task on a different thread using the "Executor" interface. It is not directly related to thread sleep.