Students can Download Computer Science Chapter 10 Python Classes and Objects Questions and Answers, Notes Pdf, Samacheer Kalvi 12th Computer Science Book Solutions Guide Pdf helps you to revise the complete Tamilnadu State Board New Syllabus and score more marks in your examinations.
Tamilnadu Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects
Samacheer Kalvi 12th Computer Science Python Classes and Objects Text Book Back Questions and Answers
PART – 1
I. Choose The Best Answer
Question 1.
Which of the following are the key features of an Object Oriented Programming language?
(a) Constructor and Classes
(b) Constructor and Object
(c) Classes and Objects
(d) Constructor and Destructor
Answer:
(c) Classes and Objects
Question 2.
Functions defined inside a class:
(a) Functions
(b) Module
(c) Methods
(d) section
Answer:
(c) Methods
Question 3.
Class members are accessed through which operator?
(a) &
(b) .
(c) #
(d) %
Answer:
(b) .
Question 4.
Which of the following method is automatically executed when an object is created?
(a) _object_( )
(b) _del( )_( )
(c) _func_( )
(d) _init_( )
Answer:
(d) _init_( )
Question 5.
A private class variable is prefixed with
(a) _
(b) &&
(c) ##
(d) **
Answer:
(a) _
Question 6.
Which of the following method is used as destructor?
(a) _init_( )
(b) _dest_ ( )
(c) _rem_( )
(d) _del_( )
Answer:
(d) _del_( )
Question 7.
Which of the following class declaration is correct?
(a) class class_name
(b) class class_name< >
(c) class class_name:
(d) class class_name[ ]
Answer:
(c) class class_name:
Question 8.
Which of the following is the output of the following program?
– class Student:
def_init_(self, name):
self.name=name
S=Student(“Tamil”)
(a) Error
(b) Tamil
(c) name
Answer:
(b) Tamil
Question 9.
Which of the following is the private class variable?
(a) _num
(b) ##num
(c) $$num
(d) &&num
Answer:
(a) _num
Question 10.
The process of creating an object is called as:
(a) Constructor
(b) Destructor
(c) Initialize
(d) Instantiation
Answer:
(d) Instantiation
PART – II
II. Answer The Following Questions
Question 1.
What is class?
Answer:
Classes and Objects are the key features of Object Oriented Programming. Class is the main building block in Python. Object is a collection of data and function that act on those data. Class is a template for the object. According to the concept of Object Oriented Programming, objects are also called as instances of a class or class variable.
Question 2.
What is instantiation?
Answer:
Once a class is created, next you should create an object or instance of that class. The process of creating object is called as “Class Instantiation”.
Syntax:
object_name = class_name( )
Question 3.
What is the output of the following program?
Answer:
class Sample:
_num=10
def disp(self):
print(self._num)
S=Sample( )
S.disp( )
print(S._num)
Output:
Error: Sample has no attribute S._num
10
Question 4.
How will you create constructor in Python?
Answer:
In Python, there is a special function called “init” which act as a Constructor. It must begin and end with double underscore. This function will act as an ordinary function;
General format of init method (Constructor function)
def_init_(self.[args …………….]):
<statements>
Question 5.
What is the purpose of Destructor?
Answer:
Destructor is also a special method gets executed automatically when an object exit from the scope. It is just opposite to constructor. In Python, _del_( ) method is used as destructor.
PART – III
III. Answer The Following Questions
Question 1.
What are class members? How do you define it?
Answer:
In Python, a class is defined by using the keyword class. Every class has a unique name followed by a colon ( : ).
Syntax:
class class_name:
statement_1
statement_2
…………………
…………………
statement_n
Where, statement in a class definition may be a variable declaration, decision control, loop or even a function definition. Variables defined inside a class are called as “Class Variable” and functions are called as “Methods”. Class variable and methods are together known as members of the class. The class members should be accessed through objects or instance of class. A class can be defined anywhere in a Python program.
Example:
Program to define a class
class Sample:
x, y = 10, 20 # class variables
In the above code, name of the class is Sample and it has two variables x and y having the initial value 10 and 20 respectively. To access the values defined inside the class, you need an object or instance of the class.
Question 2.
Write a class with two private class variables and print the sum using a method?
Answer:
class Sample:
def_init_(self,n1,n2):
self._n1=n1
self._n2=n2
def display(self):
print(“class variable 1:”, self._n1)
print(“class variable 2:”, self._n2)
print(“sum self._n1 + self._n2)
s = sample(10, 20)
s.display( )
Output:
class variable 1 : 10
class variable 2 : 20
sum : 30
Question 3.
Find the error in the following program to get the given output?
class Fruits:
def_init_(self, f1, f2):
self.f1=f1
self.f2=f2
def display (self):
print(“Fruit 1 = %s, Fruit 2 = %s” %(self.fl, self.f2))
F = Fruits (‘Apple’, ‘Mango’)
del F.display
F.display( )
Output
Fruit 1 = Apple, Fruit 2 = Mango
Answer:
In line No. 8, del F.display will not come
Question 4.
What is the output of the following program?
Answer:
class Greeting:
def_init_(self, name):
self._name = name
def display(self):
print(“Good Morning “, self._name)
obj=Greeting(‘Bindu Madhavan’)
obj.display( )
Output:
Bindu Madhavan Good Morning
Question 5.
How do define constructor and destructor in Python?
Answer:
General format of init method (Constructor function)
def_init_(self.[args ……………….]):
<statements>
To define destructor:
_del_ ( ) method is used.
PART – IV
IV. Answer The Following Questions
Question 1.
Write a menu driven program to add or delete stationary items. You should use dictionary to store items and the brand?
Answer:
stationary = { }
while((ch == 1) or (ch == 2))
print(” 1. Add Item \n 2. Delete Item”)
ch = int(input(“Enter your choice “))
if(ch==1):
n = int(input(“Enter the number of items to be added in the stationary shop”))
for i in range(n):
item = input(“Enter an item “)
brand = input(“Enter the brand Name”)
stationary[item] = brand
print(stationary)
elif(ch == 2):
remitem = input(“Enter the item to be deleted from the shop”)
dict.pop(remitem)
print( stationary)
else:
print(“Invalid options. Type 1 to add items and 2 to remove items “)
ch = int(input(“Enter your choice :”)
Output:
- Add item
- Delete Item Enter your choice : 1
Enter the number of items to be added in the stationary shop : 2
Enter an item : Pen
Enter the brand Name : Trimax
Enter an item : Eraser
Enter the brand Name : Camlin
Pen : Trimax
Eraser : Camlin
Enter your choice : 2
Enter the item to be deleted from the shop : Eraser
Pen : Trimax
Enter your choice : 3
Invalid options. Type 1 to add items an 2 to remove items.
Practice Programs
Question 1.
Write a program using class to store name and marks of students in list and print total marks?
Answer:
class stud:
def_init_(self):
self.name=” ”
self.m1=0
self.m2=0
self.tot=0
def gdata(self):
self.name = input(“Enter your name”)
self.m1 = int(input(“Enter marks 1”))
self.m2 = int(input(“Enter marks2”))
self, tot = self.m1+self.m2
def disp(self):
print(self.name)
print(self.m1)
print(self.m2)
print(self.tot)
mlist = [ ]
st = stud( )
st.gdata( )
mlist. append(st)
for x in mlist:
x.disp( )
Output:
Enter your name Ram
Enter marks 1 100
Enter marks2 100
Ram 100 100 200
Question 2.
Write a program using class to accept three sides of a triangle and print its area?
Answer:
class Tr:
def_init_(self, a, b, c):
self.a = float(a)
self.b = float(b)
self.c = float(c)
def area(self):
s = (self.a + self.b + self.c)/2
return((s*(s-self.a) * (s-self.b) * (s-self.c) ** 0.5)
a = input(“Enter side 1:”)
b = input(“Enter side2:”)
c = input(“Enter side3:”)
ans=Tr(a,b,c)
print(ans.area( ))
Output:
Enter side 1 : 3
Enter side 2 : 4
Enter side 3 : 5
6.0
Question 3.
Write a menu driven program to read, display, add and subtract two distances?
Answer:
class Dist:
def_init_(self):
self, dist 1=0
self.dist 2=0
def read(self):
self.dist 1=int(input(“Enter distance 1”))
self.dist 2=int(input(“Enter distance 2”))
def disp(self):
print(“distance 1”, self.dist 1)
print(“distance 2”, self.dist 2)
def add(self):
print(“Total distances”, self.dist 1+self.dist 2)
def sub(self):
print(“Subtracted distance”, self.dist 1-self.dist 2)
d=Dist( )
choi = “y”
while(choi == “y”):
print(” 1. accept \n 2. Display \n 3. Total \n 4. Subtract”)
ch = int(input(“Enter your choice”))
if(ch==l):
d.read( )
elif(ch==2):
d.disp( )
elif(ch==3):
d.add( )
elif(ch==4):
d.sub( )
else:
print(“Invalid Input…”)
choi = input(“Do you want to continue”)
Output:
- Accept
- Display
- Add
- Subtract
Enter your choice : 3
Enter distance 1 : 100
Enter distance 2 : 75
Do you want to continue .. y
- Accept
- Display
- Add
- Subtract
Enter your choice : 3
Total distances : 175
Do you want to continue .. y
- Accept
- Display
- Add
- Subtract
Enter your choice : 2
Enter distance 1 : 100
Enter distance 2 : 75
Do you want to continue .. y
- Accept
- Display
- Add
- Sub
Enter your choice : 4
Subtracted distance : 25
Do you want to continue .. N
Samacheer kalvi 12th Computer Science Python Classes and Objects Additional Questions and Answers
PART – 1
1. Choose The Correct Answer
Question 1.
……………………. are also called as instances of a class or class variable.
Answer:
objects
Question 2.
All integer variables used in python program is an object of class ……………………….
Answer:
int
Question 3.
All the string variables are of object of class ……………………..
Answer:
strings
Question 4.
class is defined by the keyword ………………………
Answer:
class
Question 5.
A statement in a class definition may be a ………………………..
(a) variable declaration
(b) decision control
(c) loop
(d) all of these
Answer:
(d) all of these
Question 6.
………………….. and …………………. are called as members of the class
Answer:
class variables and methods
Question 7.
The first argument of the class method is ……………………….
(a) class
(b) func
(c) def
(d) self
Answer:
(d) self
Question 8.
The statements defined inside the class must be properly indented.
True / false
Answer:
True
Question 9.
The init function should begin and end with
(a) underscore
(b) double underscore
(c) #
(d) S
Answer:
(b) double underscore
Question 10.
…………………….. is used to initialize the class variables.
(a) constructor
(b) destructor
(c) class
(d) objects
Answer:
(a) constructor
Question 11.
Find the correct statement from the following.
(a) constructor function can be defined with arguments
(b) constructor function can be defined without arguments
(c) constructor function can be defined with or without argument
(d) constructor function cannot be defined
Answer:
(c) constructor function can be defined with or without argument
Question 12.
…………………….. is a special function to gets executed automatically when an object exit from the scope.
(a) constructor
(b) init
(c) destructor
(d) object
Answer:
(c) destructor
Question 13.
The variables which are defined inside the class is by default.
(a) private
(b) public
(c) protected
(d) local
Answer:
(b) public
Question 14.
Which variables can be accessed anywhere in the program using dot operator?
(a) private
(b) public
(c) protected
(d) auto
Answer:
(b) public
Question 15.
Which variables can be accessed only within the class?
(a) private
(b) public
(c) protected
(d) local
Answer:
(a) private
Question 16.
Match the following
1. constructor – (i) def process(self)
2. Destructor – (ii) S.x
3. method – (iii) _del_(self)
4. object – (iv) _init_(self, num)
(a) 1-(iv) 2-(iii) 3-(i) 4-(ii)
(b) 1-(i) 2-(ii) 3-(iii) 4-(iv)
(c) 1-(iv) 2-(ii) 3-(i) 4-(iii)
(d) 1-(i) 2-(iii) 3-(iv) 4-(ii)
Answer:
(a) 1-(iv) 2-(iii) 3-(i) 4-(ii)
PART – II
II. Answer The Following Questions
Question 1.
Write note on self?
Answer:
The class method must have the first argument named as self. No need to pass a value for this argument when we call the method. Python provides its value automatically. Even if – a method takes no arguments, it should be defined with the first argument called self. If a method is defined to accept only one argument it will take it as two arguments ie. self and the defined argument.
PART – III
III. Answer The Following Questions
Question 1.
Write the output for the program?
Answer:
class Sample:
def_init_(self, num):
print(“Constructor of class Sample…”)
self.num=num
print(“The value is num)
S=Sample(10)
Constructor of class sample…
The value is: 10
PART – IV
IV. Answer The Following Questions
Question 1.
Write a program to calculate area and circumference of a circle?
Answer:
class Circle:
pi=3.14
def_init_(self,radius):
self.radius=radius
def area(self):
return Circle.pi*(self.radius**2)
def circumference(self):
return 2*Circle.pi*self.radius
r = int(input(“Enter Radius:”))
C=Circle(r)
print(“The Area =”,C.area( ))
print(“The Circumference =”, C.circumference( ))
Output:
Enter Radius: 5
The Area = 78.5
The Circumference = 31.400000000000002
Question 2.
Write a menu driven program that keeps record of books available in your school library?
Answer:
class Library:
def_init_(self):
self.bookname=””
self.author=””
def getdata(self):
self.bookname = input(“Enter Name of the Book: “)
self.author = input(“Enter Author of the Book: “)
def display(self):
print(“Name of the Book: “,self.bookname)
print(” Author of the Book: “,self.author)
print(“\n”)
book=[ ] #empty list
ch = ‘y’
while(ch= =’y’):
print(“1. Add New Book \n 2.Display Books”)
resp = int(input(“Enter your choice :”))
if(resp= =1):
L=Library( )
L.getdata( )
book.append(L)
elif(resp= =2):
for x in book:
x.display( )
else:
print(“Invalid input….”)
ch = input(“Do you want continue….”)
Output:
- Add New Book
- Display Books
Enter your choice : 1
Enter Name of the Book: Programming in C++
Enter Author of the Book: K. Kannan
Do you want continue….y
- Add New Book
- Display Books
Enter your choice : 1
Enter Name of the Book: Learn Python
Enter Author of the Book: V.G.Ramakrishnan
Do you want continue….y
- Add New Book
- Display Books
Enter your choice : 1
Enter Name of the Book: Advanced Python
Enter Author of the Book: Dr. Vidhya
Do you want continue….y
- Add New Book
- Display Books Enter your choice : 1
Enter Name of the Book: Working with OpenOffice
Enter Author of the Book: N.V.Gowrisankar
Do you want continue….y
- Add New Book
- Display Books Enter your choice : 1
Enter Name of the Book: Data Structure
Enter Author of the Book: K.Lenin
Do you want continue….y
- Add New Book
- Display Books
Enter your choice : 1
Enter Name of the Book: An Introduction to Database System
Enter Author of the Book: R.Sreenivasan
Do you want continue….y
- Add New Book
- Display Books Enter your choice : 2
Enter Name of the Book: Programming in C++
Enter Author of the Book: K. Kannan
Name of the Book: Learn Python
Author of the Book: V.G.Ramakrishnan
Name of the Book: Advanced Python
Author of the Book: Dr. Vidhya
Name of the Book: Working with OpenOffice
Author of the Book: N.V.Gowrisankar
Name of the Book: Data Structure
Author of the Book: K.Lenin
Name of the Book: An Introduction to Database System
Author of the Book: R.Sreenivasan
Do you want continue….n