Knowee
Questions
Features
Study Tools

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)

Question

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)

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

Solution

The output of the Python code will be:

100 102

Explanation:

  1. A class named 'A' is defined with an initializer method (init). This method takes one argument 'count' with a default value of 100.

  2. An object 'obj1' is created from the class 'A' without passing any argument. So, the default value of 'count' (which is 100) is used.

  3. Another object 'obj2' is created from the class 'A' with an argument 102. So, this value overrides the default value of 'count'.

  4. When we print 'obj1.count', it prints the value of 'count' for the object 'obj1', which is 100.

  5. When we print 'obj2.count', it prints the value of 'count' for the object 'obj2', which is 102.

This problem has been solved

Similar Questions

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)100100100102102102ErrorClear ResponseSave & Next

What will be the output of the following Python code?(i) class A: (2)def __init__(self,x):self.x = xdef count(self,x):self.x = self.x+1class B(A):def __init__(self, y=0):A.__init__(self, 3)self.y = ydef count(self):self.y += 1def main():obj = B()obj.count(

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 following python code? class myclass:    def __init__(self,a):        self.a = a        print(self.a)o=myclass()

What will be the output of the following Python code?class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a)

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.