Tamilnadu State Board New Syllabus Samacheer Kalvi 12th Computer Science Guide Pdf Chapter 10 Python Classes and Objects Text Book Back Questions and Answers, Notes.

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

12th Computer Science Guide Python Classes and Objects Text Book Questions and Answers

I. Choose the best answer (1 Marks)

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

Samacheer Kalvi 12th Computer Science Guide Chapter 10 Python 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_ ()

Samacheer Kalvi 12th Computer Science Guide 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 ()

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:

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

Question 8.
Which of the following is the output of the following program?
class Student:
def_init_(self, name):
self.name=name
print(self.name)
S=Student(“Tamil”)
a) Error
b) Tamil
c) name
d) self
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

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

II. Answer the following questions (2 Marks)

Question 1.
What is the class?
Answer:
Classes and Objects are the key features of Object-Oriented Programming. Class is the main building block in Python. 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.

Question 2.
What is instantiation?
Answer:
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:
Error: Sample has no attribute S._num
10

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

RESTART:
C:/ Users/ COMPUTER / AppData / Local/ Programs / Python / Py thon37-32/ tst.py
Traceback (most recent call last):
File “C:/Users/COMPUTER/ AppData/ Local / Programs / Python/ Python37-32/ tst.py”, line
6, in <module>
S.disp()
AttributeError: ‘Sample’ object has no attribute ‘disp’
>>>

Question 4.
How will you create a constructor in Python?
Answer:

  • In Python, there is a special function called “init” which acts as a Constructor.
  • It must begin and end with a double underscore.
  • This function will act as an ordinary function; but the only difference is, it is executed automatically when the object is created.
  • This constructor function can be defined with or without arguments.
  • This method is used to initialize the class variables.

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 the opposite to the constructor. In Python, _del_( ) method is used as the destructor.

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

III. Answer the following questions (3 Marks)

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, a 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 variables 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 with two private class variables and print the sum using a method:
class add:
def_init_(self,m,n):
self. _m=m
self. _n=n # m,n, – private variables
def display (self):
sum=self. m+self. n
print(‘ Enter first number=’,self. m,)
print(‘Enter second number=’,self. n)
print(“The sum is”,sum)
x=add(15,2)
x. display ()
Output:
>>>
RESTART: C:/Users/Computer/
AppData /Local/ Programs / Python /
Py thon3 7 / TW OLOC AL. py
Enter first number= 15
Enter second number= 2
The sum is 17
>>>

Samacheer Kalvi 12th Computer Science Guide 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.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

Samacheer Kalvi 12th Computer Science Guide 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 (‘BinduMadhavan’)
obj. display ()
Output:
Good Morning BinduMadhavan

Question 5.
How do define constructor and destructor in Python?
Answer:
Constructor:

  • The constructor is the special function that is automatically executed when an object of a class is created.
  • 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; but the only difference is, it is executed automatically when the object is created.
  • This constructor function can be defined with or without arguments. This method is used to initialize the class variables.

Syntax:
_init_ method (Constructor function)
def _init_(self, [args …. ]):
< statements >

Example:
class Sample:
def _init_(self, num):
print(” Constructor of class Sample…”)
self.num=num
print(“The value is :”, num)
S=Sample(10)

Destructor:

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

Example:
class Example:
def _init_ (self):
print “Object created”
# destructor
def _del_ (self):
print “Object destroyed”
# creating an object
myObj = Example ()
Output:
Object created
Object destroyed

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

IV. Answer the following questions (5 Marks)

Question 1.
Write a menu-driven program to add or delete stationery items. You should use I 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:

  1. Add item
  2. 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 and 2 to remove items.

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

12th Computer Science Guide Python Classes and Objects Additional Important Questions and Answers

I. Choose the best answer (1 Mark)

Question 1.
………… is not an object-oriented language.
a) C
b) C++
c) Java
d) Python
Answer:
a) C

Question 2.
All integer variables used in the python program is an object of class ……………………….
Answer:
int

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

Question 3.
………….. are called as Functions of the class.
a) Methods
b) Members
c) Variables
d) Loop
Answer:
a) Methods

Question 4.
In Python, every class name followed by ……………..delimiter.
a) ;
b) :
c) .
d) .
Answer:
b) :

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

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

Question 6.
……………… is a valid syntax for crating objects.
a) objectname = classname ()
b) objectname: classname ()
c) objectname = classname
d) classname = Objectname ()
Answer:
a) objectname = classname ()

Question 7.
…………….. is a valid syntax of accessing class members
a) objectname = classmember ()
b) objectname. classmember ()
c) objectname. Classmember
d) objectname.classmember
Answer:
b) objectname. classmember ()

Question 8.
………….. position of the argument named self in python class method.
a) First
b) Second
c) Third
d) Last
Answer:
a) First

Question 9.
The init function should begin and end with
(a) underscore
(b) double underscore
(c) #
(d) S
Answer:
(b) double underscore

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

Question 10.
……………. number of arguments can be taken by Python method even when a method is defined with one argument?
a) 1
b) 3
c) 2
d) 4
Answer:
c) 2

Question 11.
In Python. ……………… function will act as a constructor.
a) int
b) inti
c) class name
d) init
Answer:
d) init

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.
…………… is used to initialize the class variables.
a) Destructor
b) Object
c) Constructor
d) Class member
Answer:
c) Constructor

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

Question 14.
The ……………..of the class should be accessed through the instance of a class.
a) Objects
b) Members
c) Functions
d) Tuples
Answer:
b) Members

Question 15.
Which variables can be accessed only within the class?
(a) private
(b) public
(c) protected
(d) local
Answer:
(a) private

Question 16.
In Python, the class method must name the first argument named as………….
a) this
b) new
c) self
d) var
Answer:
c) self

Question 17.
…………… and…………… are the key features of object-oriented programming.
a) List and tuples
b) Set and dictionary
c) Classes and objects
d) Variables and methods
Answer:
c) Classes and objects

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

Question 18.
By default, the class variables are ……………..
a) Private
b) Public
c) Protected
d) Method
Answer:
b) Public

II. Answer the following questions (2 and 3 Marks)

Question 1.
Write a note on public and private data members of the python class.
Answer:

  • The variables which are defined inside the class is public by default.
  • These variables can be accessed anywhere in the program using dot operator.
  • A variable prefixed with double underscore becomes private in nature.
  • These variables can be accessed only within the class.

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)
S=Sample(12,14)
S.display()
print(“Value 1 = “, S.n1)
print(“Value 2 = “, S._n2)

  • In the above program, there are two class variables n1 and n2 are declared.
  • The variable n1 is a public variable and n2 is a private variable.
  • The display( ) member method is defined to show the values passed to these two variables.
  • The print statements defined within the class will successfully display the values of n1 and n2, even though the class variable n2 is private.
  • Because, in this case, n2 is called by a method defined inside the class.
  • But, when we try to access the value of n2 from outside the class Python throws an error.
  • Because private variables cannot be accessed from outside the class.

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

Question 1.
Write a 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 i.e. self and the defined argument.

Question 3.
How Python class function differs from ordinary function.
Answer:

  • Python class function or Method is very similar to ordinary function with, a small difference that, 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.

Question 4.
Write the output of the following (March 2020)
Answer:
class Hosting:
def _init_ (self.name)
self. _name=name
def display (self):
print(“Welcome to”,self._name)
obj=Hosting(” Python Programming”)
obj.display()
Output:
Welcome to Python Programming

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

Question 5.
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

HANDS ON PRACTICE

Question 1.
Rewrite the following Python program to get the given output:
OUTPUT:
Enter Radius: 5
The area = 78.5
The circumference = 34.10
CODE:
Class circle ()
pi=3.14
def _init_(self, radius):
self=radius
DEF area(SELF):
Return
Circle.pi + (self.radius * 2)
Def circumference(self):
Return 2*circle.pi * self.radius
r = input(“Enter radius=”)
c = circle(r)
print “The Area:”, c.area()
printf (“The circumference=”, c)

Correct Program:
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())

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

Question 2.
Write a menu driven program to read, display, add and subtract two distances.
Coding:
class Distances:
def init (self):
self.distl =”
self.dist2 = ”
self .result = ”
def inputdata(self):
self.dist1=float(input(/Enter the first point=’))
self.dist2=float(input(“Enter the second point= “))
def adddist(self):
self ,result=self. dist1+self. dist2
def subdist(self):
if self.distl >self.dist2:
self.result=self.distl-self.dist2
else:
self.result=self.dist2-self.distl
def DisplayDist(self):
return self, result
Dt=Distances() .
ch=,y’
while(ch==’y,):
print(“\nl. Add two points\n2.Subtract two points “)
choice=int(input(“\nEnter your choice:”))
if(choice==l):
Dt.inputdataQ
Dt.adddist()
print(“Sum of two points is:”,round
(Dt.DisplayDist(),3))
elif(choice==2):
Dt.inputdata()
Dt.subdist()
print(“Difference in between two points is:”,round(Dt.DisplayDist(),3))
else:
print(“Invalid input”)
ch=input(“do you want to continue y/n: “)
Output:
>>>

RESTART:
C:/Users / COMPUTER/ AppData/ Local / Programs / Python / Py thon37-32/ menudist-05.02.2020. py
1. Add two points
2. Subtract two points Enter your choice: 1
Enter the first point = 58.6
Enter the second point = 12.8
The Sum of two points is: 71.4
1. Add two points
2. Subtract two points
Enter your choice: 2 ,
Enter the first point= 47.5
Enter the second point= 23.6
The difference between the two points is: 23.9
1. Add two points
2.Subtract two points
Enter your choice: 4
Invalid input
do you want to continue y/n: n
>>>

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

Question 3.
What will be the output of the following Python code
class String:
def _init_(self):
self.uppercase=0
self.lowercase=0
self.vowels=0
self.consonants=0
self.spaces=0
self.string=””
def getstr (self):
self. string=” Welcome Puducherry”
def count_upper(self):
for ch in self.string:
if (ch.isupper())
self.uppercase+=1
def count_lo wer (self):
for ch in self.string:
if (ch.islower())
self.lowercase+=1
def count_vowers(self)
for ch in self.string:
if (ch in (‘A’, ‘a’, V, ‘E!, T, T, ‘o’, ‘O’, ‘u’,’U’)):
self.vowers+=1
def count_consonants(self):
for ch in self.string:
if (ch not in ((‘A’, ‘a’, V, ‘E’, Y, T, ‘o’, ‘O’, ‘u’, ‘U’,”)):
self.consonants+=1
def count_space (self):
for ch in self.string:
if (ch==” “):
self spaces+=1
def execute (self):
self.count_upper()
self.count_lower() ¦
self.count_vowels()
self.count_consonants()
self.count_space()
def display (self):
print (” The given string contains…”)
print(“%d Uppercase letters”% self .uppercase)
pring(“&d Lowercase letters” % self .lowrcase)
print(” % d V owels” % self .vowels)
print(” % d Consonants” % self, consonants)
pring(“%d Spaces” % self.spaces)
S = String()
S.getstr()
S.execute()
S.display()
Output:
The given string contains…
2 Uppercase letters
17 Lowercase letters
7 Vowels
12 Consonants
2 Spaces

Leave a Reply