1Z0-811 Inheritance and Polymorphism 3 — Questions and Answers
Question 1: Which statement is true about abstract classes in Java?
- Abstract classes can be instantiated directly with new
- Abstract classes cannot contain any concrete methods
- Abstract classes can have both abstract and concrete methods (Correct answer)
- Abstract classes must implement all methods of any interface they implement
Correct answer: Abstract classes can have both abstract and concrete methods
Abstract classes can contain both abstract methods (with no implementation) and concrete methods (with full implementation).
Question 2: What does the 'instanceof' operator return in Java?
- The class name of the object as a String
- A boolean indicating if an object is an instance of a given type (Correct answer)
- A new instance of the specified class
- The memory address of the object
Correct answer: A boolean indicating if an object is an instance of a given type
The 'instanceof' operator returns true if the object on the left is an instance of the class or interface on the right, enabling safe type checking before casting.
Question 3: What exception is thrown when an invalid cast is attempted at runtime?
- IllegalArgumentException
- NullPointerException
- ClassNotFoundException
- ClassCastException (Correct answer)
Correct answer: ClassCastException
A ClassCastException is thrown at runtime when an object cannot be cast to the specified type because it is not an instance of that type.
Question 4: What is a covariant return type in method overriding?
- The overriding method must return the exact same type as the parent method
- The overriding method may return a subtype of the parent method's return type (Correct answer)
- The overriding method may return a supertype of the parent method's return type
- The overriding method must declare void as its return type
Correct answer: The overriding method may return a subtype of the parent method's return type
A covariant return type allows an overriding method to return a more specific (subtype) type than the parent method's declared return type.
Question 5: Which of the following is a valid polymorphic variable assignment, where Dog extends Animal?
- Dog d = new Animal();
- Animal a = new Dog(); (Correct answer)
- Object o = new int();
- Dog d = (Dog) new Object();
Correct answer: Animal a = new Dog();
A reference variable of a parent type can hold an object of any subtype — Animal a = new Dog() is valid because Dog is-a Animal.
Question 6: What happens if a subclass constructor does not explicitly call super()?
- A compilation error always occurs
- The compiler automatically inserts a call to the no-arg super() constructor as the first statement (Correct answer)
- The parent class constructor is never invoked
- A RuntimeException is thrown when the object is created
Correct answer: The compiler automatically inserts a call to the no-arg super() constructor as the first statement
If super() is not explicitly called, the compiler automatically inserts a call to the parent's no-arg constructor; if no such constructor exists, a compile error results.
Question 7: A concrete class that extends an abstract class must:
- Override all methods inherited from java.lang.Object
- Implement all abstract methods or itself be declared abstract (Correct answer)
- Be declared final to prevent further subclassing
- Declare at least one public constructor
Correct answer: Implement all abstract methods or itself be declared abstract
A concrete subclass of an abstract class must provide implementations for all abstract methods; otherwise, the subclass must also be declared abstract.
Which statement is true about abstract classes in Java?