Python OOPS – Object Oriented Programming System Questions with Solutions

Object-Oriented Programming (OOP) is a programming paradigm that organizes code using classes and objects. It makes programs more reusable, modular, and easier to maintain. In this practice set, you’ll learn the fundamentals of OOP in Python through beginner-friendly, Python OOPS – Object Oriented Programming System Questions with Solutions


1. Python Program to Create a Class and Object

Problem Statement

Write a Python program to create a class named Student and print a student’s name using an object.

Python Solution

class Student:
    name = "John"

student = Student()

print(student.name)

Sample Output

John

Explanation

A class is a blueprint for creating objects. An object is an instance of a class.

Concepts Covered

  • Class
  • Object

2. Python Program to Use the __init__() Constructor

Problem Statement

Write a Python program to initialize a student’s name and age using the constructor.

Python Solution

class Student:

    def __init__(self, name, age):
        self.name = name
        self.age = age

student = Student("John", 20)

print(student.name)
print(student.age)

Sample Output

John
20

Explanation

The __init__() method is automatically called when an object is created.

Concepts Covered

  • Constructor
  • __init__()

3. Python Program to Create an Instance Method

Problem Statement

Write a Python program to create an instance method that displays student information.

Python Solution

class Student:

    def __init__(self, name, course):
        self.name = name
        self.course = course

    def display(self):
        print("Name:", self.name)
        print("Course:", self.course)

student = Student("John", "Python")

student.display()

Sample Output

Name: John
Course: Python

Explanation

Instance methods use self to access object attributes.

Concepts Covered

  • Instance Method
  • self

4. Python Program to Create Multiple Objects

Problem Statement

Write a Python program to create two objects of the same class.

Python Solution

class Student:

    def __init__(self, name):
        self.name = name

student1 = Student("John")
student2 = Student("Rahul")

print(student1.name)
print(student2.name)

Sample Output

John
Rahul

Explanation

A single class can create multiple independent objects.

Concepts Covered

  • Multiple Objects

5. Python Program to Demonstrate Inheritance

Problem Statement

Write a Python program where the Student class inherits from the Person class.

Python Solution

class Person:

    def show(self):
        print("I am a Person.")

class Student(Person):
    pass

student = Student()

student.show()

Sample Output

I am a Person.

Explanation

Inheritance allows one class to inherit properties and methods from another class.

Concepts Covered

  • Inheritance

6. Python Program to Override a Method

Problem Statement

Write a Python program to override a method in the child class.

Python Solution

class Animal:

    def sound(self):
        print("Animal Sound")

class Dog(Animal):

    def sound(self):
        print("Bark")

dog = Dog()

dog.sound()

Sample Output

Bark

Explanation

Method overriding allows a child class to provide its own implementation of a parent class method.

Concepts Covered

  • Method Overriding

7. Python Program to Demonstrate Encapsulation

Problem Statement

Write a Python program to create a private variable inside a class.

Python Solution

class Student:

    def __init__(self):
        self.__marks = 95

    def show_marks(self):
        print(self.__marks)

student = Student()

student.show_marks()

Sample Output

95

Explanation

A double underscore (__) makes an attribute private.

Concepts Covered

  • Encapsulation
  • Private Variables

8. Python Program to Demonstrate Polymorphism

Problem Statement

Write a Python program to demonstrate polymorphism using two classes.

Python Solution

class Dog:

    def sound(self):
        print("Bark")

class Cat:

    def sound(self):
        print("Meow")

for animal in (Dog(), Cat()):
    animal.sound()

Sample Output

Bark
Meow

Explanation

Polymorphism allows different classes to have methods with the same name.

Concepts Covered

  • Polymorphism

9. Python Program to Check the Type of an Object

Problem Statement

Write a Python program to check whether an object belongs to a class.

Python Solution

class Student:
    pass

student = Student()

print(isinstance(student, Student))

Sample Output

True

Explanation

The isinstance() function checks whether an object belongs to a specific class.

Concepts Covered

  • isinstance()

10. Python Program to Access Class Variables

Problem Statement

Write a Python program to create and access a class variable.

Python Solution

class Student:

    school = "CodeMantra"

student1 = Student()
student2 = Student()

print(student1.school)
print(student2.school)

Sample Output

CodeMantra
CodeMantra

Explanation

Class variables are shared among all objects of a class.

Concepts Covered

  • Class Variables

11. Python Program to Create a Student Class with Constructor and Display Student Details

Problem Statement

Write a Python program to create a Student class with a constructor (__init__) that initializes the student’s name, roll number, and course. Create multiple objects and display their details.

Python Solution

class Student:

    def __init__(self, name, roll_no, course):

        self.name = name
        self.roll_no = roll_no
        self.course = course

    def display(self):

        print("Student Name :", self.name)
        print("Roll Number  :", self.roll_no)
        print("Course       :", self.course)
        print("-" * 30)


student1 = Student("Rahul", 101, "Python")
student2 = Student("Priya", 102, "Data Science")

student1.display()
student2.display()

Sample Output

Student Name : Rahul
Roll Number  : 101
Course       : Python
------------------------------
Student Name : Priya
Roll Number  : 102
Course       : Data Science
------------------------------

Explanation

The constructor automatically initializes object attributes whenever a new object is created. The display() method prints the student information.

Concepts Covered

  • Class
  • Object
  • Constructor
  • Instance Variables
  • Methods

12. Python Program to Calculate Employee Salary Using Class Variables

Problem Statement

Write a Python program to create an Employee class that uses a class variable to represent the company name while storing employee details as instance variables.

Python Solution

class Employee:

    company = "ABC Technologies"

    def __init__(self, name, salary):

        self.name = name
        self.salary = salary

    def display(self):

        print("Company :", Employee.company)
        print("Employee:", self.name)
        print("Salary  : ₹", self.salary)
        print("-" * 30)


employee1 = Employee("Rohit", 45000)
employee2 = Employee("Anjali", 62000)

employee1.display()
employee2.display()

Sample Output

Company : ABC Technologies
Employee: Rohit
Salary  : ₹ 45000
------------------------------
Company : ABC Technologies
Employee: Anjali
Salary  : ₹ 62000
------------------------------

Explanation

The class variable company is shared among all objects, whereas name and salary are unique to each employee.

Concepts Covered

  • Class Variables
  • Instance Variables
  • Objects
  • Constructors

13. Python Program to Demonstrate Single Inheritance

Problem Statement

Write a Python program to demonstrate single inheritance where the Car class inherits properties from the Vehicle class.

Python Solution

class Vehicle:

    def start(self):

        print("Vehicle Started")


class Car(Vehicle):

    def drive(self):

        print("Car is Running")


car = Car()

car.start()

car.drive()

Sample Output

Vehicle Started
Car is Running

Explanation

The Car class inherits the start() method from the Vehicle class, demonstrating code reuse through inheritance.

Concepts Covered

  • Inheritance
  • Parent Class
  • Child Class
  • Code Reusability

14. Python Program to Demonstrate Method Overriding

Problem Statement

Write a Python program to demonstrate method overriding using inheritance.

Python Solution

class Animal:

    def sound(self):

        print("Animals make sounds.")


class Dog(Animal):

    def sound(self):

        print("Dog barks.")


animal = Dog()

animal.sound()

Sample Output

Dog barks.

Explanation

The Dog class overrides the sound() method inherited from the Animal class. When the method is called, the child class implementation is executed instead of the parent class version.

Concepts Covered

  • Method Overriding
  • Inheritance
  • Runtime Polymorphism
  • Classes

15. Python Program to Demonstrate Encapsulation Using Private Variables

Problem Statement

Write a Python program to demonstrate encapsulation by creating private instance variables and accessing them through public methods.

Python Solution

class BankAccount:

    def __init__(self, balance):

        self.__balance = balance

    def deposit(self, amount):

        self.__balance += amount

    def display_balance(self):

        print("Current Balance: ₹", self.__balance)


account = BankAccount(10000)

account.deposit(2500)

account.display_balance()

Sample Output

Current Balance: ₹ 12500

Explanation

The variable __balance is private and cannot be accessed directly from outside the class. Public methods are used to safely modify and retrieve its value, demonstrating encapsulation and data hiding.

Concepts Covered

  • Encapsulation
  • Private Variables
  • Data Hiding
  • Public Methods
  • Object-Oriented Programming

16. Python Program to Demonstrate Multiple Inheritance

Problem Statement

Write a Python program to demonstrate multiple inheritance where a child class inherits features from two parent classes.

Python Solution

class Father:

    def skills(self):

        print("Father: Driving")


class Mother:

    def talent(self):

        print("Mother: Cooking")


class Child(Father, Mother):

    def hobby(self):

        print("Child: Playing Cricket")


child = Child()

child.skills()

child.talent()

child.hobby()

Sample Output

Father: Driving
Mother: Cooking
Child: Playing Cricket

Explanation

The Child class inherits from both Father and Mother, allowing it to access methods from both parent classes. This is known as Multiple Inheritance.

Concepts Covered

  • Multiple Inheritance
  • Parent Classes
  • Child Class
  • Code Reusability

17. Python Program to Demonstrate Multilevel Inheritance

Problem Statement

Write a Python program to demonstrate multilevel inheritance where a class inherits from another derived class.

Python Solution

class GrandFather:

    def house(self):

        print("Grandfather owns a house.")


class Father(GrandFather):

    def car(self):

        print("Father owns a car.")


class Son(Father):

    def bike(self):

        print("Son owns a bike.")


person = Son()

person.house()

person.car()

person.bike()

Sample Output

Grandfather owns a house.
Father owns a car.
Son owns a bike.

Explanation

The inheritance chain is:

GrandFather → Father → Son

The Son class automatically inherits methods from both parent classes.

Concepts Covered

  • Multilevel Inheritance
  • Class Hierarchy
  • Objects
  • OOP

18. Python Program to Demonstrate Class Method and Static Method

Problem Statement

Write a Python program that demonstrates the difference between a class method and a static method.

Python Solution

class College:

    college_name = "VSIT Computer Institute"

    @classmethod
    def show_college(cls):

        print("College:", cls.college_name)

    @staticmethod
    def greeting():

        print("Welcome to Python OOP Practice!")


College.show_college()

College.greeting()

Sample Output

College: VSIT Computer Institute
Welcome to Python OOP Practice!

Explanation

  • @classmethod works with class variables using the cls parameter.
  • @staticmethod does not depend on class or object data and behaves like a normal function inside the class.

Concepts Covered

  • Class Method
  • Static Method
  • Decorators
  • Class Variables

19. Python Program to Demonstrate the super() Function

Problem Statement

Write a Python program that uses the super() function to call the constructor of the parent class.

Python Solution

class Person:

    def __init__(self, name):

        self.name = name


class Student(Person):

    def __init__(self, name, course):

        super().__init__(name)

        self.course = course

    def display(self):

        print("Name  :", self.name)

        print("Course:", self.course)


student = Student("Riya", "Python")

student.display()

Sample Output

Name  : Riya
Course: Python

Explanation

The super() function allows the child class to access the constructor and methods of its parent class without explicitly mentioning the parent class name.

Concepts Covered

  • super()
  • Constructor
  • Inheritance
  • Parent Constructor

20. Python Program to Demonstrate Hierarchical Inheritance

Problem Statement

Write a Python program to demonstrate hierarchical inheritance where multiple child classes inherit from a single parent class.

Python Solution

class Animal:

    def eat(self):

        print("Animal is eating.")


class Dog(Animal):

    def bark(self):

        print("Dog barks.")


class Cat(Animal):

    def meow(self):

        print("Cat meows.")


dog = Dog()

cat = Cat()

dog.eat()

dog.bark()

cat.eat()

cat.meow()

Sample Output

Animal is eating.
Dog barks.
Animal is eating.
Cat meows.

Explanation

In Hierarchical Inheritance, multiple child classes (Dog and Cat) inherit common properties and methods from a single parent class (Animal).

Concepts Covered

  • Hierarchical Inheritance
  • Parent Class
  • Multiple Child Classes
  • Object-Oriented Programming

21. Python Program to Create an Abstract Class Using the abc Module

Problem Statement

Write a Python program to create an abstract class Shape with an abstract method area(). Then create a child class Rectangle that implements the abstract method.

Python Solution

from abc import ABC, abstractmethod


class Shape(ABC):

    @abstractmethod
    def area(self):
        pass


class Rectangle(Shape):

    def __init__(self, length, width):

        self.length = length
        self.width = width

    def area(self):

        return self.length * self.width


rectangle = Rectangle(10, 5)

print("Area =", rectangle.area())

Sample Output

Area = 50

Explanation

The Shape class is an abstract class because it contains an abstract method. Any child class inheriting from Shape must implement the area() method, otherwise Python will raise an error.

Concepts Covered

  • Abstract Class
  • Abstract Method
  • abc Module
  • Inheritance
  • Polymorphism

22. Python Program to Demonstrate Operator Overloading

Problem Statement

Write a Python program to overload the + operator so that two objects of a class can be added together.

Python Solution

class Box:

    def __init__(self, value):

        self.value = value

    def __add__(self, other):

        return self.value + other.value


box1 = Box(35)

box2 = Box(45)

print("Total Value =", box1 + box2)

Sample Output

Total Value = 80

Explanation

The __add__() magic method overloads the + operator, allowing two custom objects to be added just like integers.

Concepts Covered

  • Operator Overloading
  • Magic Methods
  • Dunder Methods
  • OOP

23. Python Program to Demonstrate Composition

Problem Statement

Write a Python program to demonstrate composition by creating an Engine class that is used inside a Car class.

Python Solution

class Engine:

    def start(self):

        print("Engine Started")


class Car:

    def __init__(self):

        self.engine = Engine()

    def drive(self):

        self.engine.start()

        print("Car is Running")


car = Car()

car.drive()

Sample Output

Engine Started
Car is Running

Explanation

Composition represents a “has-a” relationship. A Car has an Engine, and the Car object uses the functionality of the Engine object instead of inheriting from it.

Concepts Covered

  • Composition
  • Object Relationships
  • Has-A Relationship
  • Classes

24. Python Program to Demonstrate Aggregation

Problem Statement

Write a Python program to demonstrate aggregation where a Department class uses an existing Teacher object.

Python Solution

class Teacher:

    def __init__(self, name):

        self.name = name


class Department:

    def __init__(self, teacher):

        self.teacher = teacher

    def display(self):

        print("Department Teacher:", self.teacher.name)


teacher = Teacher("Annu Ma'am")

department = Department(teacher)

department.display()

Sample Output

Department Teacher: Annu Ma'am

Explanation

Aggregation represents a “uses-a” relationship. The Department class uses a Teacher object, but both objects can exist independently.

Concepts Covered

  • Aggregation
  • Object Association
  • Uses-A Relationship
  • OOP Design

25. Python Program to Build a Library Management System Using OOP

Problem Statement

Write a Python program to create a simple Library Management System using classes and objects. The program should allow adding books and displaying the available books.

Python Solution

class Library:

    def __init__(self):

        self.books = []

    def add_book(self, book_name):

        self.books.append(book_name)

    def display_books(self):

        print("Available Books:\n")

        for book in self.books:

            print("-", book)


library = Library()

library.add_book("Python Programming")

library.add_book("Data Structures")

library.add_book("Machine Learning")

library.display_books()

Sample Output

Available Books:

- Python Programming
- Data Structures
- Machine Learning

Explanation

This program demonstrates a real-world OOP application. The Library class encapsulates the book collection and provides methods to add and display books. This approach improves modularity, maintainability, and code reusability.

Concepts Covered

  • Classes
  • Objects
  • Encapsulation
  • Lists
  • Methods
  • Real-World OOP Project

Frequently Asked Questions (FAQs)


1. What is Object-Oriented Programming (OOP) in Python?

Object-Oriented Programming (OOP) is a programming paradigm that organizes code using classes and objects. It helps developers build reusable, modular, and maintainable applications by modeling real-world entities.

Example:

class Student:

    def __init__(self, name):

        self.name = name

student = Student("Rahul")

print(student.name)

Output

Rahul

Concepts Covered

  • Object-Oriented Programming
  • Class
  • Object
  • Constructor

2. What is the difference between a Class and an Object?

A Class is a blueprint used to create objects, while an Object is an actual instance of that class.

ClassObject
Blueprint or templateReal instance of a class
Does not occupy memory until instantiatedOccupies memory when created
Defines attributes and methodsUses attributes and methods
Example: CarExample: BMW, Audi

Understanding this distinction is fundamental to learning OOP.


3. What is a Constructor (__init__) in Python?

A constructor is a special method that is automatically called when an object is created. It initializes the object’s attributes.

Example

class Employee:

    def __init__(self, name):

        self.name = name


employee = Employee("Aman")

print(employee.name)

Output

Aman

Constructors simplify object initialization by assigning values during object creation.


4. What are the four pillars of Object-Oriented Programming?

The four fundamental principles of OOP are:

  1. Encapsulation – Protects data by restricting direct access.
  2. Inheritance – Allows one class to inherit features from another.
  3. Polymorphism – Enables the same interface to perform different actions.
  4. Abstraction – Hides implementation details and exposes only essential functionality.

These principles make applications more secure, reusable, and easier to maintain.


5. What is Inheritance in Python?

Inheritance allows a child class to reuse the properties and methods of a parent class, reducing code duplication.

Example

class Animal:

    def sound(self):

        print("Animal Sound")


class Dog(Animal):

    pass


dog = Dog()

dog.sound()

Output

Animal Sound

Inheritance is widely used to create relationships between classes.


6. What is Polymorphism in Python?

Polymorphism allows the same method name to behave differently depending on the object that calls it.

Example

class Bird:

    def sound(self):

        print("Bird Chirps")


class Cat:

    def sound(self):

        print("Cat Meows")


animals = [Bird(), Cat()]

for animal in animals:

    animal.sound()

Output

Bird Chirps
Cat Meows

Polymorphism improves code flexibility and extensibility.


7. What is Encapsulation in Python?

Encapsulation is the practice of hiding an object’s internal data and providing controlled access through public methods.

Example

class Bank:

    def __init__(self):

        self.__balance = 5000

    def get_balance(self):

        return self.__balance


account = Bank()

print(account.get_balance())

Output

5000

Encapsulation helps protect data from accidental modification and enforces controlled access.


8. What is Abstraction in Python?

Abstraction hides implementation details and exposes only the necessary functionality. It is commonly implemented using abstract classes from the abc module.

Example

from abc import ABC, abstractmethod


class Vehicle(ABC):

    @abstractmethod
    def start(self):

        pass


class Car(Vehicle):

    def start(self):

        print("Car Started")


car = Car()

car.start()

Output

Car Started

Abstraction allows developers to define a common interface while leaving implementation details to child classes.


9. What is the difference between Method Overloading and Method Overriding in Python?

Method OverloadingMethod Overriding
Multiple methods with the same name but different parameters (limited support in Python)Child class redefines a method of the parent class
Occurs within the same classOccurs between parent and child classes
Achieved using default arguments or *args in PythonAchieved through inheritance

Method overriding is one of the most frequently used OOP concepts in Python applications.


10. Why is OOP important for Python interviews and real-world projects?

Object-Oriented Programming is one of the most important Python concepts because it helps developers build scalable, maintainable, and reusable software.

Common interview topics include:

  • Classes and Objects
  • Constructors (__init__)
  • Instance and Class Variables
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction
  • Method Overriding
  • super() Function
  • Class Methods
  • Static Methods
  • Composition
  • Aggregation
  • Magic (Dunder) Methods
  • Operator Overloading
  • Abstract Classes

OOP is extensively used in Web Development (Django, Flask), Desktop Applications, Game Development, Automation, Artificial Intelligence, Machine Learning, Data Science, Banking Systems, E-commerce Platforms, ERP Software, Cloud Applications, and Enterprise Software Development. Mastering OOP is essential for writing clean, reusable, and professional Python code, making it one of the highest-priority topics for technical interviews and real-world software engineering.

Chapter Summary

After completing this chapter, you have learned:

  • Classes
  • Objects
  • Constructors
  • Instance Variables
  • Instance Methods
  • Inheritance
  • Method Overriding
  • Encapsulation
  • Polymorphism
  • Class Variables
  • isinstance()

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top