Pip 5
Pip 5
Que.N Bloom’s
Marks Assignment Questions
o. Level
1 1, 2 4 What is Object-Oriented Programming (OOP) in Python? Explain its advantages.
Marking Scheme 2 marks deftn, 2 marks advantages
Object-Oriented Programming (OOP) is a programming paradigm in Python that is
based on the concept of objects and classes. It allows the organization of code into
reusable and modular components by grouping data and behavior together.
A class is a blueprint for creating objects, and an object is an instance of a class that
holds data (attributes) and functions (methods).
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
return f"Car: {self.brand} {self.model}"
# Creating an object
student1 = Student("John", 20)
print(student1.display()) # Output: Student Name: John, Age: 20
Here, Student is a class, and student1 is an object created from that class.
def get_balance(self):
return self.__balance # Accessing private variable via method
# Creating an account
account = BankAccount(5000)
print(account.get_balance()) # Allowed: Output - 5000
print(account.__balance) # Error: Cannot access private attribute directly
🔹 Here, the balance is a private attribute, and it cannot be accessed directly,
ensuring data security.
class Shape(ABC):
@abstractmethod
def area(self):
pass # Abstract method
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius # Must implement area()
circle = Circle(5)
print(circle.area()) # Output: 78.5
🔹 The Shape class cannot be instantiated, and all subclasses must implement the
area() method.
dog = Dog()
print(dog.speak()) # Output: Dog barks
🔹 The Dog class inherits the speak() method from the Animal class and modifies it.
class Car(Vehicle):
def move(self):
return "Car drives on the road"
class Boat(Vehicle):
def move(self):
return "Boat sails on water"
car = Car()
boat = Boat()
def show_details(self):
return f"Laptop: {self.brand}, Price: {self.price}"
Applies Only to the calling Affects the whole class and all
To instance instances
Inheritance is a key concept in OOP that allows a child class to inherit properties and methods
from a parent class. This promotes code reusability and reduces redundancy.
Types of Inheritance in Python:
1. Single Inheritance – One child class inherits from a single parent class.
Answer
2. Multiple Inheritance – A child class inherits from multiple parent classes.
3. Multilevel Inheritance – A child class inherits from another child class, forming a chain.
4. Hierarchical Inheritance – Multiple child classes inherit from a single parent class.
5. Hybrid Inheritance – A combination of different types of inheritance.
@abstractmethod
def make_sound(self): # Abstract method
pass
# Creating objects
dog = Dog()
cat = Cat()
def get_balance(self):
return f"Balance: ₹{self.__balance}"
class Calculator:
def add(self, a, b=0, c=0): # Default parameters allow overloading behavior
return a + b + c
calc = Calculator()
print(calc.add(5)) # Output: 5
print(calc.add(5, 10)) # Output: 15
print(calc.add(5, 10, 15)) # Output: 30
Encapsulation is a mechanism in OOP that binds data and methods that manipulate
the data into a single unit. It restricts direct access to some of an object’s components,
making the data secure.
In Python, encapsulation is achieved using private variables (prefixing variable names
with __).
class BankAccount:
def __init__(self, account_holder, balance):
self.account_holder = account_holder # Public attribute
self.__balance = balance # Private attribute
def get_balance(self):
return self.__balance # Accessing private variable through a method
# Creating an object
account = BankAccount("John", 5000)
print(account.get_balance()) # Accessing balance via method
print(account.__balance) # This will raise an AttributeError