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
Difficulty: Foundation
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
Difficulty: Foundation
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
Difficulty: Foundation
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
Difficulty: Foundation
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
Difficulty: Practice
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
Difficulty: Practice
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
Difficulty: Practice
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
Difficulty: Challenge
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
Difficulty: Challenge
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
Difficulty: Challenge
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
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.
