Knowee
Questions
Features
Study Tools

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()

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be:

1 1

Here's the step by step explanation:

  1. A class named Myclass is defined with a class variable x initialized to 0.

  2. Two methods are defined within the class: incre and disp. The incre method increments the value of x by 1 and the disp method prints the value of x.

  3. An object ob1 of Myclass is created. The incre method is called on ob1, which increments ob1's x value by 1 (from 0 to 1). Then, the disp method is called on ob1, which prints ob1's x value, which is 1.

  4. Another object ob2 of Myclass is created. The incre method is called on ob2, which increments ob2's x value by 1 (from 0 to 1). Then, the disp method is called on ob2, which prints ob2's x value, which is 1.

So, the output of the code is 1 and 1.

This problem has been solved

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()

1/3

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.