Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Easy
Question 54
//InstanceofTest.java
public class InstanceofTest {
public static void main(String[] args) {
Error obj = new Error();
boolean flag1 = obj instanceof RuntimeException; //Line n1
boolean flag2 = obj instanceof Exception; //Line n2
boolean flag3 = obj instanceof Error; //Line n3
boolean flag4 = obj instanceof Throwable; //Line n4
System.out.println(flag1 + ":" + flag2 + ":" + flag3 + ":" + flag4);
}
}
What will be the result of compiling and executing InstanceofTest class?
-
A
false:false:true:false
-
B
false:false:true:true
-
C
Compilation error
-
D
false:true:true:true
-
E
true:true:true:true
Reveal correct answer
Correct answer: C
Explanation
The Error class extends Throwable, so obj instanceof Error and obj instanceof Throwable return true. However, the Error class is not related to Exception and RuntimeException classes in multilevel inheritance, causing Line n1 and Line n2 to produce compilation errors.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
