Hackerrank - HackerRank Python Object-Oriented Programming Basics Questions and Answers 1 — Questions and Answers
Question 1: Which of the following best describes the relationship between a class and an object in Python?
- An object is a specific, concrete realization of a class. (Correct answer)
- A class is an instance of an object.
- A class and an object are interchangeable terms for the same concept.
- An object is a template used to create multiple classes.
Correct 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.
Question 2: In a Python class, what is the primary role of the special method `__init__`?
- To define the string representation of an object for end-users.
- To initialize the state of a newly created object with its starting attributes. (Correct answer)
- To destroy an object and free up memory when it is no longer in use.
- To create a new instance of the class before it is initialized.
Correct 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.
Question 3: Analyze the following code. What principle of object-oriented programming does the `Penguin` class's `fly` method demonstrate?
- Encapsulation
- Abstraction
- Inheritance
- Method Overriding (Correct answer)
Correct 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.
Question 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?
- balance_
- _balance
- __balance (Correct answer)
- BALANCE
Correct 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.
Question 5: What will be the output of the following Python code snippet?
- Canis lupus, Canis lupus
- Canis familiaris, Canis familiaris (Correct answer)
- Canis lupus, Canis familiaris
- Canis familiaris, Canis lupus
Correct 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.
Question 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?
- Inheritance
- Polymorphism (Correct answer)
- Encapsulation
- Composition
Correct 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.
Which of the following best describes the relationship between a class and an object in Python?