LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Predict the Output · Question 17

getClass() comparisons with subclasses Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 17 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links