Question 17
Back to Exercise
What is the output of the following Java program?
class Animal { }
class Dog extends Animal { }
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
System.out.println((a.getClass() == Dog.class) + " " + (a.getClass() == Animal.class) + " " + (a instanceof Animal) + " " + a.getClass().getSimpleName() + " " + int[].class.getSimpleName() + " " + new int[0].getClass().getSuperclass().getSimpleName());
}
}
- A true false true Dog int[] Object Correct
- B false true true Animal int[] Object
- C true true true Dog int[] Object
- D true false true Dog int Array
Correct Answer: Option A
getClass reports the exact run-time class; instanceof also accepts supertypes. Arrays are objects whose superclass is Object.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!