SCJP Java OOP and Inheritance 4 — Questions and Answers
Question 1: Which statement correctly describes the behavior of constructors in an inheritance hierarchy?
- Constructors are inherited by subclasses
- The subclass constructor always calls the superclass constructor, explicitly or implicitly
- If no super() call exists, the compiler inserts super() only if the superclass has a no-arg constructor (Correct answer)
- A subclass constructor must always explicitly call super()
Correct answer: If no super() call exists, the compiler inserts super() only if the superclass has a no-arg constructor
If a subclass constructor does not explicitly call super(), the compiler inserts a call to the superclass no-arg constructor; if none exists, a compile error occurs.
Question 2: Given: class A { int x = 5; } class B extends A { int x = 10; } A obj = new B(); System.out.println(obj.x); — What is printed?
- 10
- 5 (Correct answer)
- Compile error
- Runtime exception
Correct answer: 5
Field access is resolved at compile time based on the reference type, so obj.x refers to A's x, printing 5.
Question 3: Which of the following is true about abstract classes in Java?
- An abstract class cannot have constructors
- An abstract class must have at least one abstract method
- An abstract class can have both abstract and concrete methods (Correct answer)
- An abstract class cannot implement interfaces
Correct answer: An abstract class can have both abstract and concrete methods
An abstract class can contain any combination of abstract and concrete methods, and it can also have constructors.
Question 4: What is the result of calling a non-private, non-final instance method on a null reference?
- Returns null
- Compile-time error
- NullPointerException at runtime (Correct answer)
- The method executes using default values
Correct answer: NullPointerException at runtime
Invoking any instance method on a null reference causes a NullPointerException at runtime.
Question 5: class Animal { void speak() { System.out.println("Animal"); } } class Dog extends Animal { void speak() { System.out.println("Dog"); } } Animal a = new Dog(); a.speak(); — What is printed?
- Animal
- Dog (Correct answer)
- AnimalDog
- Compile error
Correct answer: Dog
Method calls are resolved at runtime based on the actual object type (Dog), demonstrating runtime polymorphism.
Question 6: Which modifier combination on a method prevents it from being overridden in a subclass?
- static only
- private only
- final only
- Both private and final work independently, but for different reasons (Correct answer)
Correct answer: Both private and final work independently, but for different reasons
A private method is hidden (not overridden) in subclasses, while a final method explicitly prohibits overriding; both prevent true overriding but via different mechanisms.
Question 7: What does the instanceof operator return when the left operand is null?
- true
- false (Correct answer)
- NullPointerException
- Compile error
Correct answer: false
The instanceof operator always returns false when the left-hand operand is null, regardless of the type on the right.
Which statement correctly describes the behavior of constructors in an inheritance hierarchy?