This page contains the CBSE Computer Science with Python class 12 Unit-1 chapter 2 Concept Of Object Oriented Programming. You can find the solutions for chapter 2 of CBSE class 12 Computer Science with Python Exercise. So is the case if you are looking for CBSE class 12 Computer Science with Python related topic Concept Of Object Oriented Programming questions and answers for the Exercise.
Question 1
Fill in the blanks:
(a)
Act of representing essential features without background detail is called . (✔ Data Abstraction)
(b)
Wrapping up of data and associated functions in to a single unit is called . (✔ Encapsulation)
(c)
is called the instance of a class. (✔ Object)
Answer 1
(a)
Data Abstraction
(b)
Encapsulation
(c)
Object
Question 2
What is Object Oriented Programming? List some of its advantages.
Answer 2
Object Oriented Programming (OOP) is a programming approach where a program is organised around objects (data + functions together) instead of only steps/procedures.
Advantages (any few):
•
Modifiability: changes inside a class usually do not affect other parts.
•
Extensibility & Maintainability: easy to add new features by adding new classes/objects.
•
Re-usability: classes can be reused; inheritance helps reuse common features.
•
Security (Data Hiding): reduces accidental data access/modification.
Question 3
Differentiate between an object and a class.
Answer 3
Basis
Class
Object
Meaning
A blueprint / template used to create objects.
An instance of a class (real entity created using the class).
Nature
Logical (design / idea).
Physical / real (exists in memory when created).
Creation
Defined using the
class keyword.Created using the class name (constructor call).
Contains
Defines data members (attributes) and member functions (methods).
Has actual values for attributes and can use methods.
Example
Mobile_phone is a class.Android phone, Windows phone, iPhone are objects (instances).
Question 4
What is inheritance? Explain with an example.
Answer 4
Definition:
Inheritance is the process of forming a new class (derived class) from an existing class (base class). In the derived class, the data members and methods of the base class are accessible, so it supports reusability of code.
Example:
If
Mobile_phone is the base class, then i_phone can be a derived class (because an iPhone is a kind of mobile phone). The derived class i_phone will have all features of Mobile_phone, and it can also add its own extra attributes/features.Question 5
List its three features that make it an important characteristic of OOP.
Answer 5
(Here, “its”(in the question) refers to inheritance.)
Here are the same three features with a short explanation for each:
1.
Re-usability: Inheritance allows the same base class to be used repeatedly to create new derived classes. This reduces development time and improves accuracy.
2.
Extensibility: New features can be added by creating a new derived class. The original base class usually does not need modification.
3.
Maintainability: Classes can be maintained separately, so debugging and updates are easier. Changes in one derived class generally do not require changes in the whole hierarchy.
Question 6
Consider the figure given below and answer the questions that follow:
(a)
Name the base class and the derived class.
(b)
Which concept of OOP is implemented in the figure given above?
Answer 6
(a)
Base class:
Student;Derived classes:
Graduate, PostGraduate.(b)
OOP concept used is Inheritance.
Question 7
How do abstraction and encapsulation complement each other?
Answer 7
Abstraction and Encapsulation are complementary concepts.
•
Through encapsulation, we enclose components of an object into a single unit (class) and separate private and public members.
•
Through abstraction, only essential behaviour/information is visible to the outside world, while internal details remain hidden.
Therefore: Encapsulation is the way to implement data abstraction.
Examples:
•
In Mobile_phone, the case/body acts like the public interface (abstraction), while internals such as memory, processor, RAM remain hidden (private).
•
In Student, visible details may include roll number, name, date of birth, and course, while internal processing like grade calculation and examiner allotment remains hidden.
Question 8
Explain polymorphism with an example.
Answer 8
Polymorphism means many forms. In OOP, it means the same action/message (same method name) can behave differently for different objects.
Real-world example (Mobile Phone idea):
Think of a common action like
call() in a Mobile_phone.•
For an Android phone,
call() may open the dialer app and place a call.•
For an iPhone,
call() may open the iOS Phone app and place a call.•
For a Windows phone (older),
call() may perform the same task in its own way.The same action name
call() is used, but implementation differs for each phone type. One action with many forms is polymorphism.Question 9
Explain Data Hiding with respect to OOP.
Answer 9
Data Hiding means keeping class data hidden from direct outside access and allowing access through methods.
It improves security and prevents accidental modification of internal data.
Question 10
Explain Function overloading with an example.
Answer 10
Function overloading means having more than one function with the same name, but with different parameters (different number of arguments or different data types). The correct function is chosen based on the arguments passed.
Example (conceptual):
Suppose we have a function named
add().•
add(10, 20) adds two numbers and returns 30.•
add("Hello", "World") concatenates two strings and returns "HelloWorld".Here, the function name is same (
add) but argument types are different, so behaviour is different.Note (Python): In C++/Java this is directly supported. In Python, if we write two
add() functions, the later one overwrites the earlier one, so we use conditions or *args to achieve similar behaviour.Question 11
Is function overloading supported by Python? Give reasons.
Answer 11
Python does not support true function overloading like C++/Java.
Reason: if two functions have the same name, the latest definition overwrites the previous one.
Alternative ways in Python:
•
Default arguments
•
*args (variable number of arguments)•
Type checking inside one function
Question 12
Write the overloaded function definitions of
add() – one adds two numbers and the other concatenates two strings.Answer 12
Python Program (Python 2):
# add() behaves like overloading by checking argument types
def add(a, b):
# if both are numbers, perform addition
if (type(a) == int or type(a) == float) and (type(b) == int or type(b) == float):
return a + b
# otherwise convert both to strings and concatenate
else:
return str(a) + str(b)
# sample calls
print add(10, 20)
print add("Hello", "World")
Sample Output:
30
HelloWorld
Question 13
Predict the output of the following program. Also state which concept of OOP is being implemented.
def sum(x, y, z):print "sum= ", x + y + zdef sum(a, b):print "sum= ", a + bsum(10, 20)sum(10, 20, 30)Answer 13
The second function definition
sum(a, b) overwrites the first one in Python.Output:
sum= 30
TypeError: sum() takes exactly 2 arguments (3 given)
Why this output occurs:
1.
Python keeps only the latest function with the same name, so after redefinition,
sum refers to sum(a, b) only.2.
The call
sum(10, 20) matches two parameters, so it prints sum= 30.3.
The call
sum(10, 20, 30) passes three arguments to a function that accepts only two, so Python raises TypeError.4.
This shows that Python does not support true function overloading by multiple definitions with the same name.
Concept attempted: Function overloading / polymorphism idea.
Question 14
State whether the following are function overloading or not. Explain why or why not.
(a)
def sum(a, b):def sum(x, y):(b)
def sum(x, y, z):def sum(e, f):Answer 14
(a)
Valid syntax, but not effective overloading in Python.
Both definitions have the same name and same number of parameters (2). The second
sum(x, y) simply overwrites the first sum(a, b).(b)
Conceptually overloading (same name, different parameter count), but Python still keeps only the latest definition.
After the second definition, only
sum(e, f) exists. A call like sum(10, 20, 30) raises TypeError.Conclusion: Python does not support true function overloading by multiple definitions with the same name; the later definition overwrites the earlier one.
Question 15
Define binding.
Answer 15
Binding means the process of linking (associating) a function call with the actual function definition (code) that should run.
In other words, when we write a method/function call, binding decides which block of code will execute for that call.
•
If linking is done before execution (at compile time), it is called static binding.
•
If linking is done during execution (at run time), it is called dynamic binding.
Question 16
Differentiate between static and dynamic binding.
Answer 16
Basis
Static Binding
Dynamic Binding
When binding happens
Done during compilation (before execution).
Done during execution (run-time).
Also called
Early binding
Late binding
Decision depends on
Fixed at compile time (method to run is decided early).
Decided at run-time based on the actual object/class.
Flexibility
Less flexible.
More flexible (supports run-time behaviour changes).
Commonly seen in
Languages/places where method calls are resolved early.
OOP situations like method overriding (run-time selection).
Question 17
Write a program to find area of the following using function overloading.
•
Area of circle (function with one parameter)
•
Area of rectangle (function with two parameters)
•
Area of triangle (function with three parameters)
Answer 17
Program 1: Area of Circle (one parameter)
# Area of circle
pi = 22.0 / 7.0
r = float(raw_input("Enter radius of circle: "))
area = pi * r * r
print "Area of circle:", area
Sample Output:
Enter radius of circle: 7
Area of circle: 154.0
Program 2: Area of Rectangle (two parameters)
# Area of rectangle
l = float(raw_input("Enter length of rectangle: "))
b = float(raw_input("Enter breadth of rectangle: "))
area = l * b
print "Area of rectangle:", area
Sample Output:
Enter length of rectangle: 10
Enter breadth of rectangle: 5
Area of rectangle: 50.0
Program 3: Area of Triangle (three parameters)
# Area of triangle using Heron's formula
a = float(raw_input("Enter side a of triangle: "))
b = float(raw_input("Enter side b of triangle: "))
c = float(raw_input("Enter side c of triangle: "))
# s is the semi-perimeter of the triangle
s = (a + b + c) / 2.0
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
print "Area of triangle:", area
Sample Output:
Enter side a of triangle: 3
Enter side b of triangle: 4
Enter side c of triangle: 5
Area of triangle: 6.0
Question 18
Write a program to find out volume of the following using function overloading.
•
Volume of cube
•
Volume of cuboid
•
Volume of cylinder
Answer 18
Program 1: Volume of Cube (one parameter)
# Volume of cube
a = float(raw_input("Enter side of cube: "))
volume = a * a * a
print "Volume of cube:", volume
Sample Output:
Enter side of cube: 5
Volume of cube: 125.0
Program 2: Volume of Cuboid (three parameters)
# Volume of cuboid
l = float(raw_input("Enter length of cuboid: "))
b = float(raw_input("Enter breadth of cuboid: "))
h = float(raw_input("Enter height of cuboid: "))
volume = l * b * h
print "Volume of cuboid:", volume
Sample Output:
Enter length of cuboid: 10
Enter breadth of cuboid: 5
Enter height of cuboid: 2
Volume of cuboid: 100.0
Program 3: Volume of Cylinder (two parameters)
# Volume of cylinder
pi = 22.0 / 7.0
r = float(raw_input("Enter radius of cylinder: "))
h = float(raw_input("Enter height of cylinder: "))
volume = pi * r * r * h
print "Volume of cylinder:", volume
Sample Output:
Enter radius of cylinder: 7
Enter height of cylinder: 10
Volume of cylinder: 1540.0