St. Britto Hr. Sec. School - Madurai
12th Computer Science Monthly Test - 2 ( Python Classes and objects)-Aug 2020
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Program to illustrate Constructor
class Sample:
def __init__(self, num):
print("Constructor of class Sample...")
self.num=num
print("The value is :", num)
S=Sample(10) -
How will you create constructor in Python?
-
Write the syntax for the following
(i) Creating objects
(ii) Accessing class members -
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) -
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() -
What is the output of the following program?
class Greeting:
def __ini
t__(self, name):
self.__name = name
def di
splay(self):
print("Good Morning ", self.__name)
obj=Greeting ('Bindu Madhavan')
obj.display() -
Fill up the blanks in the following program to get the output :
Valueofx= 10
Value of y = 20
Sum of x andy= 30
Class sample:
x, _______ = 10, 20 -------------------(1)
s = _______ --------------------(2)
print ("value ofx = '',____ )-------------(3)
print ("value ofy ='',__ ) -------------(4)
print ("sum of x and y =", __ ) --------(5) -
Read the following program. Answer the following question.
Class sample:
x, y = 10,20
s= sample()
print (s. x + s. y)
1. What does sample denotes?
2. What does x, y denotes?
3. What does s denotes? -
Write a note on self argument used in python class function.
-
-
Write a python program that illustrate the use of destructor.
-
Explain the working of the following program.
class Sample:
def _ init_(self, num):
print("Constructor of class Sample ... ")
self.num=num
print("The value is:", num)
S=Sample(10)
-
-
Program to illustrate class variable to keep count of number of objects created.
class Sample:
num=0
def __init__(self, var):
Sample.num+=1
self.var=var
print("The object value is = ", var)
print("The count of object created = ", Sample.num)
S1=Sample(15)
S2=Sample(35)
S3=Sample(45)
In the above program, class variable num is shared by all three objects of the class Sample. It is initialized to zero and each time an object is created, the num is incremented by 1. Since, the variable shared by all objects, change made to num by one object is reflected in other objects as well. Thus the above program produces the output given below. -
-
Write a program to accept a string and print the number of uppercase, lowercase, vowels, consonants and spaces in the given string.
-
Write a menu driven program that keeps record of books available in you school library.
-
-
Write a menu driven program to add or delete stationary items. You should use dictionary to store items and the brand.
-
Write a program to calculate area and circumference of a circle.
-
Write a python program to check and print if the given number is odd or even using class.
-
Program to find total and average marks using class
class Student:
mark1, mark2, mark3 = 45, 91, 71 #class variable
def process(self): #class method
sum = Student.mark1 + Student.mark2 + Student.mark3
avg = sum/3
print("Total Marks = ", sum)
print("Average Marks = ", avg)
return
S=Student()
S.process()
In the above program, after defining the class, an object S is created. The statement S.process( ), calls the function to get the required output.
Note that, we have declared three variables mark1, mark2 and mark3 with the values 45, 91, 71 respectively. We have defined a method named process with self argument, which means, we are not going to pass any value to that method. First, the process method adds the values of the class variables, stores the result in the variable sum, finds the average and displays the result.
Thus the above code will show the following output