โ† All HACKERRANK Flashcard Decks

Object-Oriented Programming Basics Flashcards

6 cards from real HACKERRANK 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. Which of the following best describes the relationship between a class and an object in Python?

    Answer: An object is a specific, concrete realization of a class.

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

  2. In a Python class, what is the primary role of the special method `__init__`?

    Answer: To initialize the state of a newly created object with its starting attributes.

    The `__init__` method is a special method, often called a constructor or initializer, that Python calls automatically when 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. Analyze the following code. What principle of object-oriented programming does the `Penguin` class's `fly` method demonstrate?

    Answer: Method Overriding

    Method overriding occurs when a subclass (child class) provides a specific implementation for a method that is already defined in its superclass (parent class). Here, the `Penguin` class overrides the `fly` method inherited from the `Bird` class to provide its own specialized behavior.

  4. A developer wants to prevent accidental modification of a `_balance` attribute from outside the `Account` class. According to Python's conventions for encapsulation, which naming prefix should be used for the attribute?

    Answer: __balance

    Using a double underscore prefix (e.g., `__balance`) invokes Python's name mangling feature. This renames the attribute internally to `_ClassName__attributeName`, making it difficult to access directly from outside the class and thus preventing accidental modification in subclasses. This is Python's mechanism for creating 'private' attributes.

  5. What will be the output of the following Python code snippet?

    Answer: Canis familiaris, Canis familiaris

    The line `dog1.species = "Canis lupus"` creates a new *instance attribute* for `dog1` that shadows the class attribute. It does not change the `species` attribute of the `Dog` class itself. Therefore, `Dog.species` remains "Canis familiaris", and `dog2.species` also refers to the original class attribute.

  6. Which object-oriented principle allows different objects to respond to the same method call in their own unique ways, enabling flexible and reusable code?

    Answer: Polymorphism

    Polymorphism, which means 'many forms', is the principle where objects of different classes can be treated as objects of a common superclass. It allows a single interface (like a method name) to be used for different types, with each type providing its own implementation.