← 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. 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()); ```

    Answer: Child

    Method overriding uses dynamic dispatch — at runtime the JVM calls Child's `greet()` because the actual object is a Child.

  2. Which statement about `static` methods and inheritance is 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.

  3. What does the `instanceof` operator return when tested against `null`?

    Answer: false

    `null instanceof AnyType` always returns `false` — no exception is thrown.

  4. Which of the following is true about interface variables in Java?

    Answer: They are implicitly public, static, and final

    All fields declared in an interface are implicitly `public static final` — they are constants.

  5. In Java, what is the purpose of using `super()` as the first statement in a constructor?

    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.

  6. Which scenario would cause a `ClassCastException` at runtime?

    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.

  7. What distinguishes method overloading from method overriding in Java?

    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.