Exercise Solutions Available
Step-by-step answers for all textbook questions in this chapter
Introduction to Object-Oriented Programming Techniques — Exercise Solutions
1. How to Use This Page
Welcome to the TN Board Class 11 Computer Science exercise solutions for the chapter Introduction to Object-Oriented Programming Techniques. Here is how to get the most out of this page:
- Read the Key Concepts section first — it gives you a rapid recap of every major topic in the chapter with Python code examples you can try in your IDLE.
- Attempt every question on your own before reading the solution. Cover the answer with your hand, think, and then compare.
- Pay special attention to the explanations after each MCQ — the reasoning matters more than the final letter.
- Use the Common Mistakes and Exam Checklist sections the night before your exam for a quick, focused review.
2. Key Concepts (Quick Recap)
Classes and Objects
| Concept | Description |
|---|---|
| Class | A blueprint or template that defines the properties (attributes) and behaviours (methods) common to all objects of that type. |
| Object | An instance of a class. Each object has its own copy of instance variables but shares the class methods. |
| Instance Variable | A variable defined inside __init__ (or any method) using self. — unique to each object. |
| Class Variable | A variable defined inside a class but outside any method — shared across all objects of the class. |
class Student:
school_name = "Govt HSS" # class variable
def __init__(self, name, roll_no):
self.name = name # instance variable
self.roll_no = roll_no # instance variable
def display(self):
print(f"{self.roll_no} - {self.name}")
s1 = Student("Arun", 101)
s2 = Student("Bala", 102)
s1.display() # 101 - Arun
s2.display() # 102 - Bala
The Four Pillars of OOP
| Pillar | What It Means | Python Example |
|---|---|---|
| Encapsulation | Bundling data and methods together; restricting direct access to internal state. | Using self.__balance (name-mangled) to hide an attribute. |
| Abstraction | Showing only essential features and hiding background details. | A start_engine() method hides ignition complexity. |
| Inheritance | A child class derives properties and methods from a parent class. | class Dog(Animal): — Dog inherits from Animal. |
| Polymorphism | Same interface, different behaviour depending on context. | len() works on strings, lists, and tuples differently. |
Encapsulation — Detailed
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.__balance = balance # private attribute (name-mangled)
def deposit(self, amount):
if amount > 0:
self.__balance += amount
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient balance")
def get_balance(self):
return self.__balance # controlled access
acc = BankAccount("Kumar", 5000)
acc.deposit(2000)
print(acc.get_balance()) # 7000
# print(acc.__balance) # AttributeError — cannot access directly
Inheritance — Types and Examples
| Type | Definition | Syntax |
|---|---|---|
| Single | One child inherits from one parent. | class Child(Parent): |
| Multilevel | Child inherits from a derived class, forming a chain. | class Grandchild(Child): |
| Multiple | Child inherits from more than one parent simultaneously. | class Child(Parent1, Parent2): |
| Hierarchical | Multiple children inherit from one parent. | Multiple class X(Parent): |
# Single Inheritance
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
d.speak() # Inherited from Animal
d.bark() # Own method
# Multilevel Inheritance
class GrandParent:
def house(self):
print("GrandParent's house")
class Parent(GrandParent):
def car(self):
print("Parent's car")
class Child(Parent):
def bike(self):
print("Child's bike")
c = Child()
c.house() # Inherited through chain
c.car() # Inherited from Parent
c.bike() # Own method
Polymorphism — Detailed
Compile-time (Static) Polymorphism: Method overloading — same method name, different parameters. Python does not support true method overloading; the last definition wins.
Run-time (Dynamic) Polymorphism: Method overriding — child class redefines a parent method.
class Shape:
def area(self):
return 0
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self): # overriding
return 3.14 * self.radius ** 2
class Rectangle(Shape):
def __init__(self, length, breadth):
self.length = length
self.breadth = breadth
def area(self): # overriding
return self.length * self.breadth
shapes = [Circle(5), Rectangle(4, 6)]
for s in shapes:
print(s.area()) # 78.5, 24 — same method name, different behaviour
Constructors and the self Parameter
| Concept | Details |
|---|---|
| Constructor | __init__() — called automatically when an object is created. Used to initialise instance variables. |
self | Refers to the current object. Must be the first parameter of every instance method. Python passes it automatically. |
| Default Constructor | A constructor with no parameters besides self. |
| Parameterised Constructor | A constructor that accepts arguments besides self. |
class Book:
# Default constructor
def __init__(self):
self.title = "Unknown"
self.author = "Unknown"
# Note: Python does not support constructor overloading.
# Use default arguments instead:
# def __init__(self, title="Unknown", author="Unknown"):
b = Book()
print(b.title) # Unknown
Access Specifiers in Python
| Specifier | Syntax | Access Level |
|---|---|---|
| Public | self.name | Accessible from anywhere. |
| Protected | self._name | Accessible within the class and subclasses (convention only, not enforced). |
| Private | self.__name | Name-mangled to self._ClassName__name; not directly accessible outside the class. |
3. I. Choose the Best Answer (MCQs)
Q1. Which of the following is a blueprint that defines the structure and behaviour of objects?
(a) Object (b) Class (c) Method (d) Constructor
Q2. The concept of wrapping data and methods together into a single unit is called:
(a) Inheritance (b) Polymorphism (c) Encapsulation (d) Abstraction
Q3. A child class inherits from two or more parent classes. Which type of inheritance is this?
(a) Single inheritance (b) Multilevel inheritance (c) Multiple inheritance (d) Hierarchical inheritance
Q4. Which method is automatically called when an object is created in Python?
(a) __new__() (b) __init__() (c) __create__() (d) __start__()
Q5. What does the self parameter in a Python class method refer to?
(a) The class itself (b) The current object calling the method (c) The parent class (d) The global namespace
Q6. Which access specifier in Python makes an attribute private using name mangling?
(a) Single underscore prefix: _var (b) Double underscore prefix: __var (c) No prefix: var (d) Triple underscore prefix: ___var
Q7. Method overriding is an example of which type of polymorphism?
(a) Compile-time polymorphism (b) Run-time polymorphism (c) Operator overloading (d) Method overloading
Q8. In multilevel inheritance, if class C inherits from class B, and class B inherits from class A, which class does C NOT directly inherit from?
(a) Class A (b) Class B (c) Both A and B (d) C inherits attributes from both A and B
Q9. Which of the following is NOT a feature of Object-Oriented Programming?
(a) Data hiding (b) Function overloading (c) Inheritance (d) Procedural paradigm
Q10. What is the output of the following code?
class Test:
def __init__(self, a=10):
self.a = a
t1 = Test()
t2 = Test(20)
print(t1.a + t2.a)
(a) 10 (b) 20 (c) 30 (d) Error
4. II. Fill in the Blanks
1. A \_\_\_\_\_\_ is a blueprint that defines the structure and behaviour of objects. Answer: class
2. An \_\_\_\_\_\_ is an instance of a class. Answer: object
3. The \_\_\_\_\_\_ method is called automatically when an object is created in Python. Answer: __init__
4. In Python, \_\_\_\_\_\_ refers to the current object and must be the first parameter of every instance method. Answer: self
5. \_\_\_\_\_\_ is the process of bundling data and methods that operate on the data into a single unit. Answer: Encapsulation
6. The \_\_\_\_\_\_ inheritance type involves one child class inheriting from more than one parent class. Answer: Multiple
7. When a child class provides its own implementation of a method defined in the parent class, it is called \_\_\_\_\_\_. Answer: method overriding
8. In Python, prefixing an attribute with \_\_\_\_\_\_ makes it private through name mangling. Answer: double underscore (__)
9. \_\_\_\_\_\_ is the OOP principle that allows hiding unnecessary details and showing only essential features. Answer: Abstraction
10. A \_\_\_\_\_\_ constructor does not accept any arguments besides self. Answer: default
11. The process of acquiring the properties of one class by another class is called \_\_\_\_\_\_. Answer: inheritance
12. \_\_\_\_\_\_ polymorphism is also known as static polymorphism. Answer: Compile-time
5. III. True or False
1. A class can exist without creating any objects. Answer: True Explanation: A class is simply a definition or blueprint. It can be defined without ever creating an object from it, just as an architectural plan can exist without building a house.
2. In Python, a private attribute can never be accessed from outside the class. Answer: False Correction: Python uses name mangling for private attributes (e.g., __var becomes _ClassName__var), so they can still be accessed using the mangled name. True encapsulation with strict access control does not exist in Python; it relies on convention.
3. Method overloading is fully supported in Python. Answer: False Correction: Python does not support true method overloading. If you define multiple methods with the same name, only the last definition is retained. Default arguments can simulate some overloading behaviour, but it is not the same as compile-time method overloading found in languages like C++ or Java.
4. A child class can override methods of the parent class. Answer: True Explanation: Method overriding is a key feature of inheritance. The child class can provide its own implementation of a method that already exists in the parent class. This is an example of run-time polymorphism.
5. In multilevel inheritance, a grandchild class can access methods of the grandparent class. Answer: True Explanation: In multilevel inheritance, properties and methods are inherited through the chain. If class A is inherited by class B, and class B is inherited by class C, then class C can access methods of both class B and class A.
6. The self keyword is mandatory in Python class definitions, but you can name it something else. Answer: True Explanation: While self is the conventional name, Python does not enforce it. You could technically use this, obj, or any other name as the first parameter. However, using anything other than self is considered very poor practice and makes code hard to read.
6. IV. Match the Following
| No. | Column A | No. | Column B |
|---|---|---|---|
| 1 | Class | a | Instance of a class |
| 2 | Object | b | Blueprint or template |
| 3 | Encapsulation | c | Hiding unnecessary details |
| 4 | Abstraction | d | Bundling data and methods |
| 5 | Inheritance | e | Acquiring properties from parent |
| 6 | Polymorphism | f | Same interface, different behaviour |
| 7 | __init__ | g | Constructor method |
| 8 | self | h | Reference to current object |
| Column A | Column B |
|---|---|
| 1 — Class | b (Blueprint or template) |
| 2 — Object | a (Instance of a class) |
| 3 — Encapsulation | d (Bundling data and methods) |
| 4 — Abstraction | c (Hiding unnecessary details) |
| 5 — Inheritance | e (Acquiring properties from parent) |
| 6 — Polymorphism | f (Same interface, different behaviour) |
7 — __init__ | g (Constructor method) |
8 — self | h (Reference to current object) |
7. V. Answer Briefly (2-Mark Questions)
Q1. What is a class? Give an example.
A class is a blueprint or template that defines the properties (attributes) and behaviours (methods) that its objects will have. It does not occupy memory for data; it only describes the structure.
class Car:
def __init__(self, brand, colour):
self.brand = brand
self.colour = colour
def display(self):
print(f"{self.colour} {self.brand}")
Here, Car is a class that specifies that every car object will have a brand and a colour, and can display itself.
Q2. Differentiate between a class and an object.
| Class | Object |
|---|---|
| It is a blueprint or template. | It is an instance of a class. |
| It defines the structure and behaviour. | It has actual values for the attributes. |
| It does not occupy memory for data. | It occupies memory in the computer. |
Example: class Student: | Example: s1 = Student("Arun", 101) |
| A class is created only once. | Many objects can be created from one class. |
Q3. What is encapsulation? Why is it important?
Encapsulation is the technique of bundling data (attributes) and the methods that operate on that data within a single unit (class), while restricting direct access to some of the object's components. This is typically achieved using access specifiers (public, protected, private).
Importance:
- It prevents accidental modification of data by external code.
- It provides controlled access through getter and setter methods.
- It improves code maintainability and security.
Example: A BankAccount class keeps the balance as a private attribute and allows access only through deposit() and withdraw() methods.
Q4. What is the role of the self parameter in Python classes?
The self parameter is a reference to the current object calling the method. It must be the first parameter of every instance method in a Python class. When you call obj.method(arg), Python automatically converts it to Class.method(obj, arg), passing the object as self.
selfdistinguishes between instance variables (self.var) and local variables (var).- Without
self, Python would not know which object's data to operate on. - It allows each object to maintain its own separate state.
Q5. Differentiate between single inheritance and multiple inheritance.
| Single Inheritance | Multiple Inheritance |
|---|---|
| One child class inherits from one parent class. | One child class inherits from two or more parent classes. |
| Simpler and easier to understand. | Can lead to ambiguity (diamond problem). |
class Child(Parent): | class Child(Parent1, Parent2): |
Example: class Dog(Animal): | Example: class FlyingCar(Vehicle, Aircraft): |
8. VI. Answer in Detail (5-Mark Questions)
Q1. Explain the four pillars of Object-Oriented Programming with suitable Python examples.
Q2. Explain the different types of inheritance in Python with examples.
Q3. Explain constructors in Python with types and examples. How are they different from regular methods?
A constructor is a special method that is automatically called when an object of a class is created. In Python, the constructor method is __init__().
Types of Constructors:
| Type | Description | Example |
|---|---|---|
| Default Constructor | Takes no parameters besides self. | def __init__(self): |
| Parameterised Constructor | Takes parameters besides self to initialise attributes. | def __init__(self, name, age): |
Default Constructor:
class Student:
def __init__(self):
self.name = "Unknown"
self.roll_no = 0
def display(self):
print(f"Name: {self.name}, Roll No: {self.roll_no}")
s = Student() # Default constructor called
s.display() # Name: Unknown, Roll No: 0
Parameterised Constructor:
class Student:
def __init__(self, name, roll_no):
self.name = name
self.roll_no = roll_no
def display(self):
print(f"Name: {self.name}, Roll No: {self.roll_no}")
s = Student("Priya", 105) # Parameterised constructor called
s.display() # Name: Priya, Roll No: 105
Constructor with Default Arguments (simulating overloading):
class Student:
def __init__(self, name="Unknown", roll_no=0):
self.name = name
self.roll_no = roll_no
def display(self):
print(f"Name: {self.name}, Roll No: {self.roll_no}")
s1 = Student() # Uses both defaults
s2 = Student("Karthik") # Overrides name, uses default roll_no
s3 = Student("Karthik", 202) # Overrides both
s1.display() # Name: Unknown, Roll No: 0
s2.display() # Name: Karthik, Roll No: 0
s3.display() # Name: Karthik, Roll No: 202
Difference Between Constructors and Regular Methods:
| Feature | Constructor (__init__) | Regular Method |
|---|---|---|
| Called automatically | Yes, when object is created | No, called explicitly |
| Purpose | To initialise object attributes | To perform operations on the object |
| Return value | Should not return anything | Can return values |
| Name | Must be __init__ | Can be any valid identifier |
| Number of calls per object | Called exactly once at creation | Can be called any number of times |
9. Common Mistakes to Avoid
1. Forgetting self in method definitions: Every instance method in a Python class must have self as the first parameter. Omitting it causes a TypeError.
# WRONG
def display():
print(self.name) # NameError: self not defined
# CORRECT
def display(self):
print(self.name)
2. Confusing class and object: A class is the blueprint; an object is the instance. Writing Student.display() without creating an object first is wrong. You must first create s = Student() and then call s.display().
3. Trying to access private attributes directly: Private attributes (with __ prefix) cannot be accessed directly as obj.__attr. Use the mangled name obj._ClassName__attr or, preferably, a getter method. Do not rely on direct access.
4. Confusing method overriding with method overloading: Method overriding is when a child class redefines a parent method (same name, same parameters). Method overloading is having multiple methods with the same name but different parameters — Python does not support this. The last method definition wins.
5. Misunderstanding the constructor return value: The __init__ method should never have a return statement with a value. It implicitly returns None. Adding return value inside __init__ causes a TypeError.
6. Using class name instead of self for instance variables: Writing Class.var = value modifies the class variable (shared by all objects), not the instance variable. Use self.var = value to set an instance variable.
class Counter:
count = 0 # class variable
def __init__(self):
self.count = 0 # instance variable (shadows class var)
Counter.count += 1 # modifies class variable
7. Calling the parent constructor incorrectly in inheritance: When a child class has its own __init__, the parent's __init__ is not automatically called. You must explicitly call it using super().__init__() or Parent.__init__(self, ...).
# WRONG — parent attributes not initialised
class Child(Parent):
def __init__(self, child_attr):
self.child_attr = child_attr
# CORRECT
class Child(Parent):
def __init__(self, parent_attr, child_attr):
super().__init__(parent_attr)
self.child_attr = child_attr
10. Exam Checklist
Before your exam, make sure you can confidently answer the following:
- [ ] Define class and object, and explain the difference between them.
- [ ] Name and explain the four pillars of OOP: Encapsulation, Abstraction, Inheritance, Polymorphism.
- [ ] Write a simple Python class with
__init__, instance variables, and at least one method. - [ ] Explain the role of the
selfparameter and what happens if you omit it. - [ ] Differentiate between public, protected, and private access specifiers in Python (
var,_var,__var). - [ ] List and explain the five types of inheritance: single, multilevel, multiple, hierarchical, hybrid.
- [ ] Explain method overriding with a Python example.
- [ ] Explain why Python does not support true method overloading.
- [ ] Write the syntax for calling a parent class constructor from a child class using
super(). - [ ] Define polymorphism and give one example each of compile-time and run-time polymorphism.
- [ ] Explain name mangling in Python and how
__varbecomes_ClassName__var. - [ ] Differentiate between default and parameterised constructors.
- [ ] Explain the Method Resolution Order (MRO) and its relevance in hybrid/multiple inheritance.
- [ ] Draw a simple inheritance diagram for multilevel and hierarchical inheritance.
- [ ] Write a complete Python program that demonstrates encapsulation using getter and setter methods.
All the best for your exam! Remember: understanding concepts deeply matters far more than memorising answers. Practise writing Python code in IDLE for every concept you learn.
