Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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_( )

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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_( )

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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 the class?
Answer:

  • Classes and Objects are the key features of Object-Oriented Programming.
  • A class is a way of binding data members and member function together.
  • Class is the main building block in Python.
  • Class is a template for the object.

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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 an object is called “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
>>>

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

Question 4.
How will you create a constructor in Python?
Answer:
In Python, there is a special function called “init” which act as a Constructor. It must begin and end with a double underscore. This function will act as an ordinary function;
The general format of init method (Constructor function)
def_init_(self.[args …………….]):
<statements>

Question 5.
What is the purpose of Destructor?
Answer:

  • Destructor is a special method that gets executed automatically when an object exit from the scope.
  • It is just the opposite of the constructor.
  • In Python, _del _() method is used as the destructor.

PART – III
III. Answer The Following Questions

Question 1.
What are class members? How do you define it?
Answer:

  • Variables defined inside a class are called as “Class Variable” and
  • Functions defined inside 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.

Accessing class members:
Any class member (class variable or method (function)) can be defined and accessed by using object with a dot (.) operator.

Syntax:
Object_name ,class_member
Example: Program to define a class and access its member variables
class Sample:
#class variables
x, y = 10, 20
S=Sample() # class instantiation
print(“Value of x = “, S.x)
print(“Value of y = “, S.y)
print(“Value of x and y = “, S.x+S.y)
Output:
Value of x = 10
Value of y = 20
Value of x and y = 30

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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. f1, self.f2))
F = Fruits (‘Apple’, ‘Mango’)
del F. display
F.display()
Output:
Traceback (most recent call last):
File “C:/Users/Computer/AppData/
Local / Programs / Python / Py thon37 /
mango.py”, line 8, in <module>
delF. display

AttributeError: display
>>>
If we omit del F. display we will get the following result.
RESTART:
C:/Users/Computer/AppData/ Local / Programs / Python / Py thon37 / mango.py =
Fruit 1 = Apple, Fruit 2 = Mango
>>>

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

Question 5.
How do define constructor and destructor in Python?
Answer:
The general format of the 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:
Coding:
stationary={}
print(“\nl. Add Item \n2.Delete item \n3.Exit”)
ch=int(input(“\nEnter your choice: “))
while(ch==1)or(ch==2):
if(ch==1):
n=int(input(“\nEnter the Number of ‘ Items to be added in the Dictionary: “))
for i in range (n):
item=input(“\nEnter an Item Name: “)
brand=input(“\nEnter the Brand Name: “)
stationary [item] =brand
print(stationary)
elif(ch==2):
ritem=input(“\nEnter the item to be
removed from the Dictionary: “)
stationary.pop(ritem)
print(stationary)
ch=int(input(“\nEnter your choice: “))
Output:
>>>
RESTART: C:/Users/COMPUTER/ AppData / Local / Programs / Python / Python37-32/menu-dictionary .py
1. Add Item
2. Delete item
3. Exit
Enter your choice: 1
Enter the Number of Items to be added
in the Dictionary: 2
Enter an Item Name: Pen
Enter the Brand Name: Natraj
{‘Pen : ‘Natraj’}
Enter an Item Name: Eraser
Enter the Brand Name: camlin
{‘Pen’: ‘Natraj’, ‘Eraser’: ‘camlin’}
{‘Pen’: ‘Natraj’, ‘Eraser’: ‘camlin’}
Enter your choice: 2
Enter the item to be removed from the
Dictionary: Eraser
{‘Pen’: ‘Natraj’}
Enter your choice: 3
>>>

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

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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:

  1. Accept
  2. Display
  3. Add
  4. Subtract

Enter your choice : 3
Enter distance 1 : 100
Enter distance 2 : 75
Do you want to continue .. y

  1. Accept
  2. Display
  3. Add
  4. Subtract

Enter your choice : 3
Total distances: 175
Do you want to continue .. y

  1. Accept
  2. Display
  3. Add
  4. Subtract

Enter your choice: 2
Enter distance 1 : 100
Enter distance 2 : 75
Do you want to continue .. y

  1. Accept
  2. Display
  3. Add
  4. 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 instances of a class or class variable.
Answer:
objects

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

Question 2.
………….. is called as instances of a class or class variable?
a) Methods
b) Objects
c) Functions
d) Datatypes
Answer:
b) Objects

Question 3.
All the string variables are of the object of class ……………………..
Answer:
strings

Question 4.
class is defined by the keyword ………………………
Answer:
class

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

Question 5.
…………….. a valid class definition.
a) Class classname () statement_1
b) Class classname :: statement_1
c) Class classname:statement_1
d) Class classname statement_1
Answer:
c) Class classname:statement_1

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

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

Question 8.
The statements defined inside the class must be properly indented. (True/false)
Answer:
True

Question 9.
Which argument does not need a value when we call the method?
a) this
b) self
c) var
d) first
Answer:
b) self

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

Question 12.
………………….. is automatically executed when an object of a class is created.
a) constructor
b) destructor
c) class
d) members
Answer:
d) init

Question 13.
The variables which are defined inside the class is by default.
(a) private
(b) public
(c) protected
(d) local
Answer:
(b) public

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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.
In Python, …………….. method is used as the destructor.
a) – – init – – ()
b) _des_ ()
c) _del_ ()
d) – – destructor – – ()
Answer:
c) _del_ ()

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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.
Define object.
Answer:

  • The object is a collection of data and functions that act on those data. Class is a template for the object.
  • According to the concept of Object-Oriented Programming, objects are also called instances of a class or class variable.
  • In Python, everything is an object.

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

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)
The constructor of class sample…
The value is: 10

PART – IV
IV. Answer The Following Questions

Question 1.
Find the output of the following Python code.
Answer:
class Sample:
num=0
def _init_(self, var):
Sample.num+=1
print(“The object value is =”, var)
print(“The count of object created =”, Sample.num)
S1=Sample(15)
S2=Sample(35)
S3=Sample(45)
Output:
The object value is = 15
The count of object created = 1
The object value is = 35
The count of object created = 2
The object value is = 45
The count of object created = 3

Samacheer Kalvi 12th Computer Science Solutions Chapter 10 Python Classes and Objects

Question 2.
Write a menu-driven program that keeps a 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:

  1. Add New Book
  2. 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

  1. Add New Book
  2. 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

  1. Add New Book
  2. 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

  1. Add New Book
  2. 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

  1. Add New Book
  2. 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

  1. Add New Book
  2. 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

  1. Add New Book
  2. 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