Basic Exercise with Solution on Class Object Method
« Tutorial on Class Object & Method
- Basic Exercise on Object Oriented Programming
- Create one class student_kit
-
Within the student_kit class create one class attribute principal name ( Mr ABC )
-
Create one attendance method and take input as number of days.
-
While creating student take input their name .
-
Create one certificate for each student by taking input of number of days present in class.
Exercise Solution
class student_kit():
principal_name='Mr ABC'
def __init__(self,name):
self.name=name
def attendance(self,days):
self.present=days+10
print("Name : ",self.name)
print("Present :",self.present)
print("Principal : ",self.principal_name)
kalu=student_kit("Kalu")
kalu.attendance(20)
Output
Name : Kalu
Present : 30
Answer these Questions
-
What is the name of the method used in student_kit class ?
-
What is the class attribute used in above code ?
-
Which is the object created in above code ?
-
How the method got access to class attributes ?
-
What is the instance attribute in above code ?
-
What is the data type of the created object ( kalu) ?
Answers
- attendance
-
principal_name
-
kalu
-
self.principal_name
-
self.present
-
print(type(kalu)) # <class '__main__.student_kit'>
How the object can access the instance attribute ?
print(kalu.present) # 30
« Tutorial on Class Object & Method
Exercise constructor ( basic )»
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com