โ† All OCP Flashcard Decks

Java Class Design and OOP Flashcards

6 cards from real OCP practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Java Class Design and OOP flashcards as text
  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 }

    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.

  2. Which of the following statements about Java Records is 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.

  3. What is the primary purpose of a `sealed` class in Java 17?

    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.

  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?

    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]

  5. Which of the following is a valid declaration for a class that extends a sealed class named `Vehicle`?

    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.

  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?

    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.