← All AP CSA Flashcard Decks

Interfaces and Abstract Classes Flashcards

7 cards from real AP CSA practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.

Read the first 7 Interfaces and Abstract Classes flashcards as text
  1. Which of the following correctly demonstrates polymorphism using an interface reference?

    Answer: Runnable r = new Thread();

    Because Thread implements Runnable, a Runnable reference variable can hold a Thread object, demonstrating interface-based polymorphism.

  2. The Comparable interface in Java requires implementing which method?

    Answer: compareTo(Object o)

    The Comparable interface requires implementing `compareTo(Object o)`, which returns a negative, zero, or positive integer based on ordering.

  3. Given `interface Printable { void print(); }` and `abstract class Document implements Printable { }`, which statement is TRUE about Document?

    Answer: Document can leave print() unimplemented since it is abstract

    An abstract class is not required to implement inherited abstract interface methods; it can defer that responsibility to its concrete subclasses.

  4. Which of the following is a valid reason to choose an interface over an abstract class?

    Answer: You want a class to inherit behavior from multiple unrelated types

    Interfaces support a form of multiple inheritance of type, allowing a class to fulfill contracts from multiple unrelated types — something abstract classes cannot do.

  5. What does the following code print? abstract class Animal { String name; Animal(String n) { name = n; } abstract String sound(); public String toString() { return name + " says " + sound(); } } class Cat extends Animal { Cat(String n) { super(n); } String sound() { return "meow"; } } // In main: Animal a = new Cat("Whiskers"); System.out.println(a);

    Answer: Whiskers says meow

    The Cat constructor passes "Whiskers" to the Animal constructor via super(n), and the overridden sound() returns "meow", so toString() produces "Whiskers says meow".

  6. In AP CSA, when a method parameter is declared with an interface type, what objects can be passed to it?

    Answer: Any object whose class implements that interface

    Because of polymorphism, any object whose class implements the specified interface can be passed to a method that accepts an interface-type parameter.

  7. Consider two interfaces: `interface Flyable { void fly(); }` and `interface Swimmable { void swim(); }`. A class Duck implements both. Which statement about Duck is correct?

    Answer: Duck can be assigned to variables of type Flyable, Swimmable, or Duck

    Duck objects can be assigned to Flyable references, Swimmable references, or Duck references, since Duck implements both interfaces and is a concrete class.