Oop Practice Problems

20th February 2026 by Tanishq Chavan
Python C C++ Javascript React JS
Python Programming Language

Python Practice Set: Object-Oriented Programming (OOP)

Test your knowledge of the four pillars of OOP: Inheritance, Polymorphism, Encapsulation, and Abstraction.


1. Conceptual Questions

Q1: What is the difference between a Class and an Object?

Answer: A Class is a blueprint or template (e.g., a "Car" design), while an Object is a specific instance of that class (e.g., your "Red Tesla").

self

Answer: self represents the instance of the class. It allows methods to access and modify the attributes and methods specific to that particular object.

Q3: Match the OOP Pillar to its description: | Pillar | Description | | :--- | :--- | | Inheritance | Creating a new class based on an existing class. | | Polymorphism | Using the same interface/method name for different underlying types. | | Encapsulation | Bundling data and methods together and restricting direct access. | | Abstraction | Hiding complex implementation details and showing only essentials. |


2. Prediction & Analysis

Q4: Predict the output of this code:

class Parent:
    def show(self):
        print("Parent")

class Child(Parent):
    def show(self):
        print("Child")

obj = Child()
obj.show()

Output: Child (Reason: Method overriding in the child class takes precedence.)

Q5: What happens if you try to instantiate an abstract class?

from abc import ABC, abstractmethod

class Base(ABC):
    @abstractmethod
    def run(self):
        pass

# b = Base()

Answer: It raises a TypeError: Can't instantiate abstract class Base with abstract method run.

Q6: What does Name Mangling do to a variable named __price inside a class named Product?

Answer: It renames it internally to _Product__price to prevent accidental access or overrides in subclasses.


3. Coding Challenges

Task 1: Basic Inheritance

Create a Vehicle class with a brand attribute, and a Car subclass that adds a model attribute.

class Vehicle:
    def __init__(self, brand):
        self.brand = brand

class Car(Vehicle):
    def __init__(self, brand, model):
        super().__init__(brand)
        self.model = model

my_car = Car("Toyota", "Corolla")
print(my_car.brand, my_car.model)

Task 2: Encapsulation with Properties

Create a BankAccount class with a private __balance. Use @property to allow reading the balance but prevent direct writing.

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance

    @property
    def balance(self):
        return self.__balance

# account = BankAccount(1000)
# print(account.balance) # Works
# account.balance = 2000 # Fails (AttributeError)

Task 3: Polymorphism in Action

Create a function animal_sound(animal) that calls a .speak() method. Pass both a Dog and a Cat object to it.

class Dog:
    def speak(self): return "Woof!"

class Cat:
    def speak(self): return "Meow!"

def animal_sound(animal):
    print(animal.speak())

animal_sound(Dog())
animal_sound(Cat())

Task 4: Abstract Blueprint

Define an abstract class Device with an abstract method turn_on(). Implement it in a Laptop class.

from abc import ABC, abstractmethod

class Device(ABC):
    @abstractmethod
    def turn_on(self):
        pass

class Laptop(Device):
    def turn_on(self):
        print("Laptop is booting up...")

mac = Laptop()
mac.turn_on()

How did you feel about this post?

๐Ÿ˜ ๐Ÿ™‚ ๐Ÿ˜ ๐Ÿ˜• ๐Ÿ˜ก

Was this helpful?

๐Ÿ‘ ๐Ÿ‘Ž