โ† All Python Flashcard Decks

Object-Oriented Programming Basics Flashcards

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

Read the first 7 Object-Oriented Programming Basics flashcards as text
  1. What is a mixin in Python OOP?

    Answer: A small class designed to add specific behavior via multiple inheritance

    Mixins are narrowly focused classes added to a class hierarchy via multiple inheritance to inject reusable behavior without deep coupling.

  2. What does `__slots__` do when defined in a class?

    Answer: Replaces `__dict__` to restrict instance attributes to a fixed set

    Declaring `__slots__` removes the per-instance `__dict__`, reducing memory and preventing dynamic attribute creation.

  3. Which of the following is an example of polymorphism in Python?

    Answer: Two classes both define a `speak()` method with different behaviors

    Polymorphism allows different classes to implement the same interface (e.g., `speak()`), enabling generic code to work with multiple types.

  4. What is the difference between `@classmethod` and `@staticmethod`?

    Answer: Class methods receive `cls`; static methods receive neither `self` nor `cls`

    `@classmethod` passes the class as its first argument (`cls`), while `@staticmethod` is a plain function scoped to the class.

  5. In Python, what does `__del__` define?

    Answer: A destructor called when the object is about to be garbage collected

    `__del__` is the finalizer, invoked just before the interpreter destroys the object, though its exact timing is not guaranteed.

  6. Which of the following correctly uses `__eq__` to compare two objects?

    Answer: def __eq__(self, other): return self.value == other.value

    `__eq__` should compare the relevant attribute of `self` with the same attribute of `other`, returning a boolean.

  7. What is encapsulation in OOP?

    Answer: Bundling data and methods that operate on that data within a class, restricting direct access

    Encapsulation hides internal state and exposes only what's necessary through a public interface, protecting the object's integrity.