OCP Java Class Design and OOP 3 — Questions and Answers
Question 1: What is the output of the following code? ```java class Parent { String greet() { return "Parent"; } } class Child extends Parent { String greet() { return "Child"; } } Parent p = new Child(); System.out.println(p.greet()); ```
- Parent
- Child (Correct answer)
- Compilation error
- null
Correct answer: Child
Method overriding uses dynamic dispatch — at runtime the JVM calls Child's `greet()` because the actual object is a Child.
Question 2: Which statement about `static` methods and inheritance is correct?
- Static methods can be overridden and use dynamic dispatch
- Static methods are hidden, not overridden, and use static dispatch (Correct answer)
- Static methods cannot be inherited
- Static methods must be redeclared in every subclass
Correct answer: Static methods are hidden, not overridden, and use static dispatch
Static methods are hidden (not overridden); the method called depends on the reference type, not the runtime type.
Question 3: What does the `instanceof` operator return when tested against `null`?
- true
- false (Correct answer)
- NullPointerException
- Compilation error
Correct answer: false
`null instanceof AnyType` always returns `false` — no exception is thrown.
Question 4: Which of the following is true about interface variables in Java?
- They are implicitly public, protected, and final
- They are implicitly public, static, and final (Correct answer)
- They can be any access modifier but are always static
- They must be explicitly declared static and final
Correct answer: They are implicitly public, static, and final
All fields declared in an interface are implicitly `public static final` — they are constants.
Question 5: In Java, what is the purpose of using `super()` as the first statement in a constructor?
- To call a static initializer in the parent class
- To explicitly invoke a parent class constructor (Correct answer)
- To call a sibling constructor in the same class
- To initialize the current object's fields before the parent's
Correct answer: To explicitly invoke a parent class constructor
`super()` calls a parent class constructor and must appear as the first statement in a subclass constructor.
Question 6: Which scenario would cause a `ClassCastException` at runtime?
- Casting an object to its own class
- Casting a parent reference to a child type when the object is not actually an instance of that child (Correct answer)
- Casting an int to a long
- Assigning a child object to a parent reference
Correct answer: Casting a parent reference to a child type when the object is not actually an instance of that child
Casting a reference to a subtype when the object is not actually of that subtype throws `ClassCastException` at runtime.
Question 7: What distinguishes method overloading from method overriding in Java?
- Overloading occurs in the same class with different parameter lists; overriding redefines a parent method in a subclass (Correct answer)
- Overloading requires the same parameter list; overriding allows different parameter lists
- Both occur only between parent and child classes
- Overloading is resolved at runtime; overriding is resolved at compile time
Correct answer: Overloading occurs in the same class with different parameter lists; overriding redefines a parent method in a subclass
Overloading means multiple methods with the same name but different signatures in the same class; overriding provides a new implementation of an inherited method.
What is the output of the following code?
```java
class Parent {
String greet() { return "Parent"; }
}
class Child extends Parent {
String greet() { return "Child"; }
}
Parent p = new Child();
System.out.println(p.greet());
```