OCP Java Class Design and OOP Questions and Answers 1 — Questions and Answers
Question 1: Given the following class definitions, which statement is true? public class Animal { public Number getAge() throws Exception { return 10; } } public class Dog extends Animal { // INSERT METHOD HERE }
- Overriding the method with `public Integer getAge() throws Exception` is a valid covariant return. (Correct answer)
- Overriding the method with `public Number getAge()` is invalid because a checked exception is removed.
- Overriding the method with `public Object getAge() throws Exception` is a valid override.
- Overriding the method with `public Integer getAge() throws RuntimeException` is invalid because the return type is changed.
Correct answer: Overriding the method with `public Integer getAge() throws Exception` is a valid covariant return.
In Java, when overriding a method, the return type can be a subtype of the return type in the superclass. This is known as a covariant return type. Since `Integer` is a subtype of `Number`, this is a valid override. [4, 7] The overriding method can also declare no exceptions, or a subclass of the exception declared in the superclass method, but it cannot declare a broader or new checked exception. Removing a checked exception or replacing it with a RuntimeException (which is unchecked) is perfectly valid.
Question 2: Which of the following statements about Java Records is correct?
- A record can extend another class.
- All fields in a record are implicitly final. (Correct answer)
- A record can be declared as abstract.
- A record's constructor must explicitly assign all fields.
Correct answer: All fields in a record are implicitly final.
A key feature of records in Java is their immutability. All instance fields declared in the record's header are implicitly `final`, meaning they cannot be modified after the object is created. Records cannot extend any other class because they implicitly extend `java.lang.Record`. They also cannot be declared abstract. [15] While records have a canonical constructor, compact constructors can be used for validation without explicitly assigning fields.
Question 3: What is the primary purpose of a `sealed` class in Java 17?
- To prevent a class from being instantiated directly.
- To ensure that all methods of the class are final.
- To create a class that can only be accessed from within the same package.
- To restrict which other classes or interfaces may extend or implement it. (Correct answer)
Correct answer: To restrict which other classes or interfaces may extend or implement it.
Sealed classes, introduced in Java 17, provide a mechanism to control inheritance. By using the `sealed` modifier and a `permits` clause, a developer can explicitly list which classes are allowed to extend the sealed class, thereby closing the class hierarchy to any other extensions. [20, 25, 26] This is different from `abstract` (prevents instantiation), `final` (prevents extension altogether), or package-private access.
Question 4: A software developer is designing a system for a library. They have created a `Book` class with private fields for `title` and `author`, and public `getter` and `setter` methods to access these fields. Which object-oriented programming principle is best demonstrated by this design?
- Polymorphism
- Inheritance
- Encapsulation (Correct answer)
- Abstraction
Correct answer: Encapsulation
Encapsulation is the practice of bundling the data (attributes) and the methods that operate on the data into a single unit, or class, and restricting direct access to some of the object's components. By making the `title` and `author` fields private and providing public getter and setter methods, the internal state of the `Book` object is protected from outside interference and misuse. [8, 13]
Question 5: Which of the following is a valid declaration for a class that extends a sealed class named `Vehicle`?
- public class Car extends Vehicle { } // Assumes Car is in the permits list
- public non-sealed class Car extends Vehicle { } // Assumes Car is in the permits list (Correct answer)
- public open class Car extends Vehicle { } // Assumes Car is in the permits list
- public class Car extends Vehicle permits Truck { }
Correct answer: public non-sealed class Car extends Vehicle { } // Assumes Car is in the permits list
A class that extends a sealed class must be declared with one of three modifiers: `final`, `sealed`, or `non-sealed`. [22, 26] The `non-sealed` modifier allows the subclass (`Car` in this case) to be extended by any other class, effectively re-opening the inheritance hierarchy at this point. Declaring it without one of these three modifiers is a compile-time error. `open` is not a valid Java keyword in this context.
Question 6: Examine the following code: public class Calculator { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } public int add(int a, int b, int c) { return a + b + c; } } This code is an example of which OOP concept?
- Method Overriding
- Abstraction
- Inheritance
- Method Overloading (Correct answer)
Correct answer: Method Overloading
Method overloading allows multiple methods with the same name to exist within the same class, as long as their parameter lists are different (either by the number of parameters or the type of parameters). In this example, the `add` method is overloaded with different parameter types and numbers, which is a form of polymorphism. [23] Method overriding, in contrast, involves a subclass providing a specific implementation of a method that is already provided by its superclass.
Given the following class definitions, which statement is true?
public class Animal {
public Number getAge() throws Exception {
return 10;
}
}
public class Dog extends Animal {
// INSERT METHOD HERE
}