Python Object-Oriented Programming Basics Questions and Answers — Questions and Answers
Question 1: In object-oriented programming, what is the fundamental relationship between a class and an object?
- A class is an instance of an object.
- A class is a blueprint for creating objects. (Correct answer)
- A class and an object are different terms for the same concept.
- An object is a blueprint for creating classes.
Correct 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.
Question 2: What is the primary role of the `__init__` method in a Python class?
- To destroy an object when it is no longer needed.
- To initialize the state of a new object when it is created. (Correct answer)
- To return a string representation of the object for debugging.
- To define a static method that can be called on the class itself.
Correct 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.
Question 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?
- Encapsulation (Correct answer)
- Inheritance
- Polymorphism
- Abstraction
Correct 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.
Question 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()) ```
- An `AttributeError`
- Some generic sound
- Nothing will be printed.
- Woof! (Correct answer)
Correct 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!".
Question 5: Which of the following scenarios best demonstrates the concept of polymorphism in Python?
- Creating a `Car` class with attributes like `make` and `model`.
- 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. (Correct answer)
- Defining a parent class `Bird` and a child class `Sparrow` that inherits from `Bird`.
- Using a private attribute `_engine_size` within a class to hide its implementation details.
Correct 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.
Question 6: What is the main goal of abstraction in object-oriented programming?
- To allow a class to inherit properties and methods from another class.
- To bundle data and methods that work on the data within one unit.
- To enable an object to be treated as an instance of its parent class.
- To hide complex implementation details and expose only the essential features of an object. (Correct answer)
Correct 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.
In object-oriented programming, what is the fundamental relationship between a class and an object?