what will be the output of the following code?class Myclass: x=0 def incre(self): self.x+=1 def disp(self): print(self.x)ob1=Myclass()ob1.incre()ob1.disp()ob2=Myclass()ob2.incre()ob2.disp()
Question
what will be the output of the following code?class Myclass: x=0 def incre(self): self.x+=1 def disp(self): print(self.x)ob1=Myclass()ob1.incre()ob1.disp()ob2=Myclass()ob2.incre()ob2.disp()
Solution
The output of the code will be:
1 1
Here's the step by step explanation:
-
A class named
Myclassis defined with a class variablexinitialized to 0. -
Two methods are defined within the class:
increanddisp. Theincremethod increments the value ofxby 1 and thedispmethod prints the value ofx. -
An object
ob1ofMyclassis created. Theincremethod is called onob1, which incrementsob1'sxvalue by 1 (from 0 to 1). Then, thedispmethod is called onob1, which printsob1'sxvalue, which is 1. -
Another object
ob2ofMyclassis created. Theincremethod is called onob2, which incrementsob2'sxvalue by 1 (from 0 to 1). Then, thedispmethod is called onob2, which printsob2'sxvalue, which is 1.
So, the output of the code is 1 and 1.
Similar Questions
class Myclass: x=0 def incre(self): self.x+=1 def disp(self): print(self.x)ob1=Myclass()ob1.incre()ob1.disp()ob2=Myclass()ob2.incre()ob2.disp()0 01 21101
What will be the output of the following code?class MyClass: def __init__(self, value=5): self.value = valueobj1 = MyClass()obj2 = MyClass(10)print(obj1.value, obj2.value)
What is the output of the following code? python class A: def display(self): print("Class A") class B(A): def display(self): print("Class B") class C(A): def display(self): print("Class C") obj = B() obj.display() obj = C() obj.display()
What will be the output of below Python code?class A(): def __init__(self,count=100): self.count=countobj1=A()obj2=A(102)print(obj1.count)print(obj2.count)
what is the output of following python code? class myclass: def __init__(self,a): self.a = a print(self.a)o=myclass()
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.