Home | | Computer Science 12th Std | Python Classes and Objects: Book Back Questions and Answers

Python Classes and Objects - Python Classes and Objects: Book Back Questions and Answers | 12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects

Chapter: 12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects

Python Classes and Objects: Book Back Questions and Answers

Choose the best answer, Answer the following questions

Computer Science : Python Modularity and OOPS : Python Classes and Objects

Evaluation


Part – I

Choose the best answer (1 Marks)


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


2.Functions defined inside a class:

(a) Functions

(b) Module

(c) Methods

(d) section


3.Class members are accessed through which operator?

(a) &

(b) .

(c) #

(d) %


4.Which of the following method is automatically executed when an object is created?

(a) __object__( )

(b) __del__( )

(c) __func__( )

(d) __init__( )


5.A private class variable is prefixed with

(a) __

(b) &&

(c) ##

(d) **


6.Which of the following method is used as destructor?

(a) __init__( )

(b) __dest__( )

(c) __rem__( )

(d) __del__( )


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[ ]


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

(d) self


9.Which of the following is the private class variable?

(a) __num

(b) ##num

(c) $$num

(d) &&num


10. The process of creating an object is called as:

(a) Constructor

(b) Destructor

(c) Initialize

(d) Instantiation


Part -II      

Answer the following questions       (2 Marks)


1. What is class?

Ans. A class is a way of binding data members and member function together. Class is a template for the object.


2. What is instantiation?

Ans. Once a class is created, next to create an object or instance of that class. This process of creating object is called as “Class Instantiation”.

 

3. What is the output of the following program? class Sample:

__num=10

def disp(self):

print(self.__num)

S=Sample()

S.disp() print(S.__num)

Ans. 10

         10


4. How will you create constructor in Python?

Ans. General format of__init__method (Constructor function)

def__init__ (self, [args …….]):

<statements>

 

5. What is the purpose of Destructor?

Ans. Destructor is also a special method gets execution automatically when an object ,exits from the scope.


Part –III

Answer the following questions       (3 Marks)


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

Ans. 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.


2. Write a class with two private class variables and print the sum using a method.

Ans. Class sum:

-a = 10

- b = 20

def disp (self):

print (“sum =”, (self. - a + self. - b))

 s = sum ()

s. disp ()


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

Fruit 1 = Apple,  Fruit 2 = Mango

Ans. The statement del F.display should not be used to display the required output.


4. What is the output of the following program?

class Greeting:

def __init__(self, name): self.__name = name

def display(self):

print("Good Morning ", self.__name)

obj=Greeting('Bindu Madhavan')

obj.display()

Ans. Good Morning Bindu Madhavan


5. How do define constructor and destructor in Python?

Ans. (i) 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.

(ii) It must begin and end with double underscore. This function will act as an ordinary function; but only difference is, it is executed automatically when the object is created.

(iii) This constructor function can be defined with or without arguments. This method is used to initialize the class variables.

General format of init method (Construe tor function)

def__init__ (self, [args………..]):

<statements>


Part –IV

Answer the following questions       (5 Marks)


1. Write a menu driven program to add or delete stationary items. You should use dictionary to store items and the brand.

Ans. Class stationary:

def getdata (self):

self. Itemname = input (“enter name of the item”)

self. Brand = input (“enter brand name”)

item = {}

ch = y

while (ch = = ‘y’)

print (“1. Add items \n 2. Delete items”)

 n = int (input (“enter your choice”))

if(n = = 1):

s = stationary ()

s. get data ()

 item. Append {S}

dif(n = = 2):

del item

else: print (“Invalid Input”)

ch = input (“Do you want to continue   (Y/N)”)


HANDS OF EXPERIENCE

 

1. Write a program using class to store name and marks of students in list and print total marks.

Ans. Class total:

name = []

mark = []

def get (self):

num = input (“How many students? :”)

for i in self, num;

self, n = input (“Enter student name”)

self name, append (self, n)

self. M = int inp (“enter mark of the student”)

mark = append (self, m)

 def display (self):

for i in self. Num

print (name [i] “:” mark [i])

t = t + mark [i]

print (“Total marks”, t)

t = total ()

t. get ()

t. display ()

 

2. Write a program using class to accept three sides of a triangle and print its area.

Ans. Class area:

def area (self, b, h);

print (“Area of Triangle” , (b * h)/2)

A = area ()

b = int (input (“enter base”))

h = int (input (“enter height”))

(a) area (b, h)

 

3. Write a menu driven program to read, display, add and subtract two distances.

Ans. def add (dl, d2)

return dl + d2

def subtract (dl, d2)

return dl - d2

“/. Read \n”, “2. Display \n”,

print (“Menu \n, 3. Add \n”, “4. Subtract \n”)

ch = int (input (“enter your choice”))

 if(ch = = l): .

x = int (input (“enter distance 1”))

y = int (input (“enter distance 2”))

dif(ch = = 2):

print (“Distance 1 ”, x)

 print (“Distance 2”, y)

dif(ch = = 3):

(add(x, y)

dif (ch = = 4):

print (substract (x, y))

else:

print(“Invalid Input”)

 

Tags : Python Classes and Objects , 12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects : Python Classes and Objects: Book Back Questions and Answers | Python Classes and Objects


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.