OCP Java Class Design and OOP 2 — Questions and Answers
Question 1: Which keyword prevents a method from being overridden in a subclass?
- static
- final (Correct answer)
- private
- abstract
Correct answer: final
The `final` keyword on a method prevents subclasses from overriding it.
Question 2: What happens when a class is declared `abstract` and contains no abstract methods?
- Compilation error
- The class cannot be instantiated but can be subclassed (Correct answer)
- It behaves exactly like a concrete class
- Only static methods are allowed
Correct answer: The class cannot be instantiated but can be subclassed
An abstract class with no abstract methods is valid — it simply cannot be instantiated directly.
Question 3: Given: `class A { int x = 5; }` and `class B extends A { int x = 10; }`, what does `((A) new B()).x` evaluate to?
- 10
- 5 (Correct answer)
- Compilation error
- Runtime exception
Correct answer: 5
Field access is resolved at compile time based on the reference type, so `((A) b).x` gives A's field value 5.
Question 4: Which of the following correctly describes a covariant return type in Java?
- An overriding method can return a supertype of the original return type
- An overriding method can return a subtype of the original return type (Correct answer)
- An overriding method must return the same type as the original
- Covariant return types are not supported in Java
Correct answer: An overriding method can return a subtype of the original return type
Java allows covariant return types, meaning an overriding method may return a subtype of the declared return type.
Question 5: What is the result of calling a `default` interface method when a class implements two interfaces that define the same default method?
- The first interface's method is used
- Compilation error unless the class overrides the method (Correct answer)
- The JVM picks one at runtime
- Both methods are called in sequence
Correct answer: Compilation error unless the class overrides the method
If two interfaces provide the same default method, the implementing class must override it or a compilation error occurs.
Question 6: Which access modifier makes a member visible only within the same package and to subclasses in other packages?
- private
- public
- protected (Correct answer)
- package-private (no modifier)
Correct answer: protected
`protected` grants access within the same package and to subclasses regardless of package.
Question 7: What is the effect of marking a constructor `private` in a class?
- The class cannot have any subclasses and cannot be instantiated outside the class (Correct answer)
- Only the class cannot be subclassed
- Only instances cannot be created from outside the class
- The class is treated as abstract
Correct answer: The class cannot have any subclasses and cannot be instantiated outside the class
A private constructor prevents both external instantiation and subclassing, since subclass constructors must call a superclass constructor.
Which keyword prevents a method from being overridden in a subclass?