โ† 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 the purpose of a `@staticmethod` in Python?

    Answer: It cannot access the class or instance

    Static methods belong to the class namespace but receive neither `self` nor `cls`, so they cannot modify class or instance state.

  2. What does `isinstance(obj, MyClass)` return?

    Answer: True if obj is an instance of MyClass or any of its subclasses

    `isinstance()` returns `True` for the specified class and all its subclasses, supporting polymorphic checks.

  3. In Python, what is the Method Resolution Order (MRO)?

    Answer: The order Python searches base classes when resolving a method call

    MRO, computed by the C3 linearization algorithm, determines which class's method is invoked in multiple-inheritance hierarchies.

  4. What does the `@property` decorator do?

    Answer: Allows a method to be accessed like an attribute

    `@property` lets you define getter logic for an attribute so callers use `obj.name` instead of `obj.get_name()`.

  5. Which of the following demonstrates composition over inheritance?

    Answer: class Car: def __init__(self): self.engine = Engine()

    Composition means a class contains an instance of another class as an attribute, favoring 'has-a' over 'is-a' relationships.

  6. What happens when you call `del obj.attr` where `attr` has a `@property` setter but no deleter?

    Answer: Python raises an AttributeError

    Without a `@attr.deleter` defined, attempting to delete the property raises `AttributeError: can't delete attribute`.

  7. Which dunder method allows instances to be used as callables (e.g., `obj()`)?

    Answer: __call__

    Defining `__call__` on a class makes its instances callable, executing the `__call__` body when `obj()` is used.