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)100100100102102102Error
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)100100100102102102Error
Solution 1
The output of the Python code will be:
100 102
Explanation:
-
The class A is defined with an initializer method (init) that takes one argument 'count' with a default value of 100.
-
An object 'obj1' is created from class A without passing any argument, so 'count' will take the default value 100.
-
Another object 'obj2' is created from class A with an argument 102, so 'count' will take the value 102.
-
When we print 'count' for 'obj1', it will print 100.
-
When we print 'count' for 'obj2', it will print 102.
Solution 2
The output of the Python code will be:
100 102
Explanation:
-
Class A is defined with an initializer method (init) that takes one argument 'count' with a default value of 100.
-
An instance of class A, obj1, is created without providing a value for 'count'. Therefore, the default value of 100 is used.
-
Another instance of class A, obj2, is created, this time with a value of 102 provided for 'count'.
-
When the 'count' attribute of obj1 is printed, the output is 100.
-
When the 'count' attribute of obj2 is printed, the output is 102.
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)5 1010 50 10None None
Which of the following is correct?class A: def __init__(self): self.count=5 self.count=count+1a=A()print(a.count)560Error
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.