OOPs Concepts with Python - Assessment Preparation
1. Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects.
It helps structure a program into simple, reusable pieces of code blueprints (usually called classes),
which are then used to create individual instances of objects.
2. Class and Object
A class is a user-defined blueprint or prototype from which objects are created. Objects are
instances of classes.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says woof!"
d = Dog("Buddy")
print(d.bark()) # Output: Buddy says woof!
3. Encapsulation
Encapsulation is the mechanism of hiding the internal data of an object and restricting access to it. It
is done using private variables (with double underscores).
class Account:
def __init__(self):
self.__balance = 0
def deposit(self, amount):
OOPs Concepts with Python - Assessment Preparation
self.__balance += amount
def get_balance(self):
return self.__balance
4. Inheritance
Inheritance is a mechanism where one class derives properties and behavior (methods) from
another class.
class Animal:
def speak(self):
return "Animal speaks"
class Dog(Animal):
def speak(self):
return "Dog barks"
d = Dog()
print(d.speak()) # Output: Dog barks
5. Polymorphism
Polymorphism allows us to use a unified interface for multiple forms (data types).
class Bird:
def fly(self):
print("Some birds can fly")
class Penguin(Bird):
def fly(self):
OOPs Concepts with Python - Assessment Preparation
print("Penguins can't fly")
def flying_test(bird):
bird.fly()
flying_test(Bird()) # Some birds can fly
flying_test(Penguin()) # Penguins can't fly
6. Abstraction
Abstraction means hiding the complexity and only showing the essential features. In Python, it can
be achieved using abstract classes and methods (ABC module).
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
class Car(Vehicle):
def start(self):
print("Car started")
c = Car()
c.start() # Output: Car started
7. Constructor and Destructor
Constructors are used for initializing objects. Destructors are called when an object is deleted or
goes out of scope.
OOPs Concepts with Python - Assessment Preparation
class Demo:
def __init__(self):
print("Constructor called")
def __del__(self):
print("Destructor called")
obj = Demo()
del obj # Destructor called immediately
8. Class Method and Static Method
Class methods are bound to the class, not the instance. Static methods do not depend on class or
instance state.
class MyClass:
count = 0
@classmethod
def increment(cls):
cls.count += 1
@staticmethod
def greet():
print("Hello!")
MyClass.increment()
MyClass.greet() # Output: Hello!
9. Operator Overloading
Operator overloading lets us redefine the behavior of operators for user-defined classes.
OOPs Concepts with Python - Assessment Preparation
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
p1 = Point(1, 2)
p2 = Point(3, 4)
p3 = p1 + p2 # Uses __add__
print(p3.x, p3.y) # Output: 4 6
10. MCQs and Output-Based Questions (100+)
In this section, you'll find over 100 carefully designed multiple-choice and output-based questions.
These test your conceptual understanding and Python application in OOP. [Continued on next
pages]