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

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

Dive into the depths of Java with our quiz based on "Thinking in Java, Fourth Edition" by Bruce Eckel. Test your knowledge, solidify concepts, and prepare for certification with challenging questions and insightful feedback.

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


What exception is thrown if a downcast fails in Java?

  1. NullPointerException

  2. ClassCastException

  3. RuntimeException

  4. IllegalArgumentException

The correct answer is: ClassCastException

A downcast in Java is when an object is converted from a superclass type to a subclass type. The ClassCastException is thrown when an attempt to perform this conversion fails, indicating that the object is not an instance of the specified subclass. This is different from a NullPointerException, which is thrown when a null object is accessed, and from a RuntimeException, which is a general exception that is not specifically related to downcasting. Similarly, an IllegalArgumentException is thrown when an invalid argument is passed to a method, so it is not directly related to downcasting. Thus, the ClassCastException is the most appropriate exception to be thrown when a downcast fails in Java.