โ† All Python Flashcard Decks

Object-Oriented Programming Basics Flashcards

6 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 6 Object-Oriented Programming Basics flashcards as text
  1. In object-oriented programming, what is the fundamental relationship between a class and an object?

    Answer: A class is a blueprint for creating objects.

    A class acts as a template or blueprint that defines the attributes and methods for a certain type of entity. An object is a specific, concrete instance created from that class, with its own state.

  2. What is the primary role of the `__init__` method in a Python class?

    Answer: To initialize the state of a new object when it is created.

    The `__init__` method is a special method, often called a constructor, that Python calls automatically whenever a new instance of a class is created. Its primary purpose is to set up the initial state of the object by assigning values to its instance attributes.

  3. A developer is designing a `BankAccount` class and wants to prevent the `_balance` attribute from being directly modified from outside the class. They provide `deposit()` and `withdraw()` methods to control changes to the balance. Which fundamental OOP principle is being applied here?

    Answer: Encapsulation

    Encapsulation is the bundling of data (attributes) and the methods that operate on that data into a single unit (a class). It often involves restricting direct access to some of an object's components, which is a key concept in data hiding. Using methods to interact with the `_balance` attribute is a classic example of encapsulation.

  4. Consider the following Python code. What will be the output when it is executed? ```python class Animal: def speak(self): return "Some generic sound" class Dog(Animal): def speak(self): return "Woof!" my_dog = Dog() print(my_dog.speak()) ```

    Answer: Woof!

    The `Dog` class inherits from the `Animal` class but provides its own implementation of the `speak()` method. This is called method overriding. When `my_dog.speak()` is called on an instance of `Dog`, Python uses the more specific version of the method defined in the `Dog` class, printing "Woof!".

  5. Which of the following scenarios best demonstrates the concept of polymorphism in Python?

    Answer: A function `render_shape(shape)` that can correctly draw a `shape` object whether it is a `Circle` or a `Square`, because both classes have a `.draw()` method.

    Polymorphism (from Greek, meaning "many forms") is the ability of an object to take on many forms. In programming, it means that a single interface (like the `render_shape` function) can be used for objects of different types. The function can operate on different shape objects without needing to know their specific type, as long as they adhere to the common interface of having a `.draw()` method.

  6. What is the main goal of abstraction in object-oriented programming?

    Answer: To hide complex implementation details and expose only the essential features of an object.

    Abstraction focuses on hiding the internal complexity of a system and exposing only the necessary parts. In OOP, this means creating classes that have a simple interface for interacting with them, without the user needing to know the intricate details of how their methods are implemented.