Ace Java 2025: Rock Your 'Thinking in Java' Challenge!

Question: 1 / 510

What exception is thrown if a downcast fails in Java?

NullPointerException

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.

Get further explanation with Examzify DeepDiveBeta

RuntimeException

IllegalArgumentException

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy