← All OCP Flashcard Decks

Java Class Design and OOP Flashcards

7 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 7 Java Class Design and OOP flashcards as text
  1. In a class hierarchy, when is the parent class's static initializer block executed relative to the child's?

    Answer: Parent's static initializer runs first

    Static initializers run in top-down order in the hierarchy — the parent class is initialized before the child class.

  2. What is the effect of declaring a nested class as `static`?

    Answer: It does not hold an implicit reference to the enclosing class instance

    A static nested class has no implicit reference to its enclosing class instance and can be instantiated independently.

  3. Which of the following best describes polymorphism via interfaces in Java?

    Answer: An object can be referenced through any interface it implements, enabling polymorphic behavior

    Interface-based polymorphism allows an object to be referenced as any interface type it implements, enabling flexible method dispatch.

  4. What is the output of the following code? ```java class Base { Base() { display(); } void display() { System.out.println("Base"); } } class Derived extends Base { int value = 42; void display() { System.out.println(value); } } new Derived(); ```

    Answer: 0

    During Base's constructor, the overridden `display()` is called on the Derived object, but `value` hasn't been initialized yet, so it prints the default int value 0.

  5. Which modifier combination is illegal for a class member in Java?

    Answer: private abstract

    A method cannot be both `private` and `abstract` — private methods are not inherited and therefore cannot be overridden.

  6. What is the primary purpose of the `final` keyword when applied to a class?

    Answer: To prevent the class from being subclassed

    Declaring a class `final` prevents any other class from extending it.

  7. Which of the following is true about constructor chaining using `this()` in Java?

    Answer: this() must be the first statement in the constructor and cannot coexist with super() in the same constructor

    `this()` must be the first statement; since `super()` is also required to be first, both cannot appear in the same constructor.