TN State Board 12th Computer Science Important Questions Chapter 10 Python Classes and Objects

Question 1.
What is a public variable in python?
Answer:

  1. The variables which are defined inside the class is public by default.
  2. These variables can be accessed anywhere is the program using dot operator.

Question 2.
What is private variable in python?
Answer:
A variable prefixed with double underscore becomes in nature. These variables can be accessed only within the class.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 3.
Explain class methods using python.
Answer:

  1. Python class function or method is very similar to ordinary function with a small difference that, the class method must have the first argument names as self.
  2. No need to pass a value for this argument when we call the method.
  3. Python provides its value automatically. Even if a method takes no arguments.

Question 4.
Write a program to check if the given number j is odd or even using class.
Answer:
class Odd Even:
even = 0 #class varaiable
def check(self,.num):
if num%2==0:
print(num,“ is Even number”)
else.
print(num,“ is Odd number”)
n=Odd_Even()
x = int(input(“Enter a value: ”))
n.check(x)

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions 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( ))

Question 6.
What is class?
Answer:

  • In python, a class is defined by using the keyword class.
  • Every class has unique name followed by a colon (:).
    Eg: class student:

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 7.
What is instantiation?
Answer:
When a class is created, next we should create an object or instance of that class. The process of creating object is called as class instantiation.

Question 8.
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:
10
10

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

  1. In python, there is special function called ‘init’ which act as a constructor.
  2. It must begin and end with double underscore.
  3. It is executed automatically when the object is created.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

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

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

Question 11.
What are class members? How do you define it?
Answer:
(i) Any class member that is class variable or method (function) can be accessed by using object with a dot (.) operator.
(ii) Class variable and methods are together known as member of the class. The class members should be accessed. Though objects or instance of class.
The syntax is
object_name.class_member
Eg: stud name.Nam
In the above example, the name of the class is stud_name. Nam is called variable as class variable or member variable of the class.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 12.
Write a class with two private class variables and print the sum using a method.
Answer:
A variable prefixed with double underscore becomes private in nature. These variables can be accessed only within the class.
Eg:
Class student:
def_init_(self m, m1, m2):
self, m1 = m1
self._m2 = m2
In this example, there are two private class variables ml and m2 are declared, ml and m2 are added and display the sum values.
defdisplsy (self):
sum = student.ml+student.m2
print (“sum=”, self.sum)

Question 13.
Find the error in the following program to get the given output?
Answer:
class Fruits:
def_init_(self, fl, 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.dispiay
F.display( )
Output:
Fruit 1 = Apple, Fruit 2 = Mango
Errors:
(i) Space between def and _init not allowed. (Correct statement is def_init_(self,f1,f2)
(ii) Space not allowed between Fruit and 1.
(iii) Space not allowed between Fruit and 2.
(iv) Space not allowed between Fruits and open bracket.
(v) del statement should not be allowed here.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 14.
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:
Good morning Bindumadhavan.

Question 15.
How do define constructor and destructor in Python?
Answer:
(i) In python, there is a special function called ‘init’ which act as a constructor.
(ii) It must begin and end with double under j score.
(iii) This constructor function can be defined with or without arguments. This method is used initialize the class variables.
Eg:
class sample:
def _init_(self, num)
This example class sample has only a constructor with one argument named as num.
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.
Eg:
class sample:
def_del_(self):

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 16.
Write a menu driven program to add or delete stationary items. You should use dictionary to store items and the brand.
Answer:
Class Dictionary:
def_init_(self):
self.dictionaryname =“ ”
self, brand =“ ”
def get data (self):
self.dictionaryname = input (“Type Dictionary name”)
self.brand = input (“Type brand name”)
def display (self):
Print(“Name of Dictionary”, self.dictionary)
printed (“Name of the company”, self.brand)
print (“\n”)
dictionary = [ ]
choice – ‘Y’
while (choice =‘y’):
print (“ 1. Add New Dictionary”)
resp = int (input (“Type your choice”))
if (resp =1):
D = Dictionary
D.getdata ( )
dictionary, append (D)
elif (resp==2):
for x in dictionary
x.display ( )
else:
print (“Invalid Input….”)
choice = input (“Do you continue”)

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 17.
Write a program using class to store name | and marks of students in list and print total marks.
Answer:
Class student:
_stud_name = [ ]
_stud_markl = [ ]
_stud_mark2 = [ ]
_stud_mark3 = [ ]
defgetdata(self):
self.s = int(input(“Type the student Name and marks to store”))
for i in range (self.s):
self._stud_name.append(str(input(“Type Name”)))
self._stud_mark1.append(int(input (“TypeMarkl”)))
self._stud_mark2.append(int(input (“TypeMark2”)))
self._stud mark3.append(int(input (“TypeMark3”)))
total_marks = 0
totafmarks-total marks + self. __stud_mark1+ self. studjnark2 + self._stud_mark3
for i in range (self. S)
print (“student name\t markl \t mark2 \t mark3”)
print (self._stud_name [i], “\t”,
self _stud_markl [i], “\t”,
self._stud_mark2 [i], “\t”,
self._stud_mark3 [i], “\t”,
print (“Total marks =”, total marks)

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 18.
Write a program using class to accept three I sides of a triangle and print its area.
Answer:
Class Triangle
def_int_(self, a, b, c):
self. a = a
self. b = b
self. c = c
def area (self):
S = (self. a + self. b+ self. c)/2
return (s*((s_selfa)*(s_selfb)* (s_selfc))) **0.5
a = input (“Type the value a”)
b = input (“Type the value b”)
c = input (“Type the value c”)
print (“Area =”, s. area ( ))

Question 19.
Write a menu driven program to read, display, add and subtract two distances.
Answer:
Class dist ( ):
def _init_(self, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def add(self):
return
((abs (x 1 +x2) * *2)+(abs(y 1 +y2)* *2 * * .5)
def sub (self):
return ((abs (x1-x2)**2) + (abs(y1-y2) **2**.5))
x1 = int (input(“Type xl value”))
x2 = int (input(“Type x2 value”))
y1 = int (input(“Type yl value”))
y2 = int (input(“Type y2 value”))
obj = cal (x1, x2, y 1, y2)
choice = 1
while choice ! = 0:
print (“0. Exit”)
print (“1. Add”)
print (“2. Subtract”)
Choice = int (input(‘Type choice”))
if choice == 1:
print (“Result”, obj. add( ))
elif choice == 2:
print (“Result”, obj.sub( ))
elif choice == 0:
print (“Exit’)
else:
print (“Invalid choice”)
print ( )

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Choose the correct answer:

Question 1.
In python every class has a unique name followed by a:
(a) . (dot)
(b) : (colon)
(c) – (hyphen)
(d) ; (semi colon)
Answer:
(b) : (colon)

Question 2.
Class variable and methods are together known as:
(a) member of class
(b) methods
(c) class variable
(d) functions
Answer:
(a) member of class

Question 3.
Variables defined inside a class are called as
(a) member of class
(b) methods
(c) class variable
(d) function
Answer:
(c) class variable

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 4.
What is the first arguments value defined the class method?
(a) self
(b) init
(c) proc
(d) obj
Answer:
(a) self

Question 5.
What is a special function called in python, i which act as constructor?
(a) self
(b) init
(c) proc
(d) obj
Answer:
(b) init

Question 6.
The public variables can be accessed anywhere in the program using:
(a) (.) dot operator
(b) & (ampersand)
(c) # (hash)
(d) % (percentage)
Answer:
(a) (.) dot operator

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 7.
Which variable can be accessed only within the class?
(a) Pubilc
(b) Private
(c) Class variable
(d) Method
Answer:
(b) Private

Question 8.
Match the following:

(i) Class Variable A. class method
(ii) Class Instantiation B. constructor
(iii) init C. object
(iv) self D. inside a class

(a) (i) – D, (ii) – C, (iii) – B, (iv) – A
(b) (i) – D, (ii) – B, (iii) – A, (iv) – C
(c) (i) – B, (ii) – A, (iii) – C, (iv) – D
(d) (i) – B, (ii) – C, (iii) – D, (iv) – A
Answer:
(a) (i) – D, (ii) – C, (iii) – B, (iv) – A

Question 9.
Choose the incorrect pair:
(a) class – keyword
(b) Function – method
(c) constructor – object
(d) Destructor – exits
Answer:
(c) constructor – object

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 10.
Choose the correct pair:
(a) . (dot) – class member
(b) _del_() – constructor
(c) _num – public
(d) : (colon) – object
Answer:
(a) . (dot) – class member

Question 11.
Choose the incorrect statement:
(a) Classes and object are the key features of OOP.
(b) Class is the main building block in python.
(c) Class is a template for the object.
(d) Class has unique name followed by a # (hash).
Answer:
(d) Class has unique name followed by a # (hash).

Question 12.
Choose the correct statement:
(a) Variable defined inside a class are called as class variable.
(b) Variable defined inside a functions are called constructor.
(c) Class variable and methods are together known as function.
(d) A Class can be defined only in the top of the python program.
Answer:
(a) Variable defined inside a class are called as class variable.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 13.
Assertion (A):
The process of creating object is called as class instantiation.
Reason (R):
Once a class is created, next you should create an object or instance of that class is known as process of object.
(a) Both A and R are true and R is the correct explanation for A.
(b) Both A and R are true and R is not the correct explanation for A.
(c) A is True but R is false.
(d) A is false but R is True.
Answer:
(a) Both A and R are true and R is the correct explanation for A.

Question 14.
Assertion (A):
In python, _del_ ( ) method is used as constructor.
Reason (R):
Constructor is special method gets executed automatically when an object exit from the scope.
(a) Both A and R are true and R is the correct explanation for A.
(b) A is true but R is false
(c) A is false but R is true
(d) Both A and R are false.
Answer:
(d) Both A and R are false.

Question 15.
Pick the odd one out:
(a) classes
(b) object
(c) destructor
(d) methods
Answer:
(c) destructor

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 16.
By default, the class variable is:
(a) Public
(b) Private
(b) Protected
(d) local
Answer:
(a) Public

Question 17.
Which of the following called as instance of a class or class variable?
(a) methods
(b) objects
(c) class
(d) Function
Answer:
(b) objects

Question 18.
Which of the following are the key features of 1 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 TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 19.
Functions defined inside a class:
(a) Functions
(b) Module
(c) Methods
(d) section
Answer:
(c) Methods

Question 20.
Class members are accessed through which operator?
(a) &
(b) .
(c) #
(d) %
Answer:
(b) .

Question 21.
Which of the following method is automatically executed when an object is created?
(a) _object_( )
(b) _del_( )
(c) _fonc_( )
(d) _init_( )
Answer:
(d) _init_( )

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 22.
A private class variable is prefixed with:
(a) _
(b) &&
(c) ##
(d)**
Answer:
(a) _

Question 23.
Which of the following method is used as destructor?
(a) _init_( )
(b) _dest ( )
(c) _rem_( )
(d) _del_( )
Answer:
(d) _del_( )

Question 24.
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 TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 25.
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
(d) Self
Answer:
(b) Tamil

Question 26.
Which of the following is the private class variable?
(a) num
(b) ##num
(c) $$num
(d) &&num
Answer:
(a) num

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 10 Python Classes and Objects

Question 27.
The process of creating an object is called as:
(a) Constructor
(b) Destructor
(c) Initialize
(d) Instantiation
Answer:
(d) Instantiation

 

TN Board 12th Computer Science Important Questions