St. Britto Hr. Sec. School - Madurai
12th Computer Science Weekly Test - 1 ( Python Classes and objects )-Aug 2020
-
-
-
-
-
-
-
-
-
-
-
-
-
Write a program in python that illustrate the use of constructor.
-
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. -
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) -
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) -
Write a note on self argument used in python class function.
-
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 the output of the (allowing 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 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 program to accept a string and print the number of uppercase, lowercase, vowels, consonants and spaces in the given string.
-
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
-