St. Britto Hr. Sec. School - Madurai
12th Computer Science Monthly Test - 1 ( Python Classes and objects )-Aug 2020
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Write the general form of declaring class in Python.
-
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) -
Write a menu driven program to read, display, add and subtract two distances.
-
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) -
What is the purpose of Destructor?
-
Explain the working of the following program
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) -
What is class?
-
Name the function which acts as a constructor and destructor.
-
Write a program using class to store name and marks of students in list and print total marks.
-
Write the syntax for the following
(i) Creating objects
(ii) Accessing class members -
program to check and print if the given number is odd or even using class
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)
When you execute this program, Python accepts the value entered by the user and passes it to the method check through object. -
Write a program using class to accept three sides of a triangle and print its area.
-
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() -
Write a class with two private class variables and print the sum using a method.
-
Write a python program to find total and average marks using class.
-
Program to illustrate about the __del__( ) method
class Sample:
num=0
def __init__(self, var):
Sample.num+=1
self.var=var
print("The object value is = ", var)
print("The value of class variable is= ", Sample.num)
def __del__(self):
Sample.num-=1
print("Object with value %d is exit from the scope"%self.var)
S1=Sample(15)
S2=Sample(35)
S3=Sample(45) -
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() -
-
Write a note on object.
-
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.
-
-
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 python program that illustrate the use of destructor.
-
Write a note on self argument used in python class function.
-
What are class members? How do you define it?
-
Write a program to check and print if the given number is negative or positive using class.
-
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 to add or delete stationary items. You should use dictionary to store items and the brand.
-
-
Write a program to store product and its cost price. Display all the available products and prompt to enter quantity of all the products. Finally generate a bill which displays the total amount to be paid.
-
Write a program to calculate area and circumference of a circle.
-
-
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