Which of the following is correct?class A: def __init__(self): self.count=5 self.count=count+1a=A()print(a.count)560Error
Question
Which of the following is correct?class A: def init(self): self.count=5 self.count=count+1a=A()print(a.count)560Error
Solution
The correct answer is "Error".
Here's why:
In the __init__ method of class A, self.count=count+1 is incorrect because count is not defined. It should be self.count=self.count+1.
So, when you try to create an object of class A using a=A(), Python will throw an error because it doesn't know what count is in the context of self.count=count+1.
Here's the corrected code:
class A:
def __init__(self):
self.count=5
self.count=self.count+1
a=A()
print(a.count)
This will output 6 because we're initializing self.count to 5 and then immediately incrementing it by 1.
Similar Questions
Which of the following is correct?class A: def __init__(self): self.count=5 self.count=count+1a=A()print(a.count)560ErrorClear ResponseSave & Submit
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
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 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 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
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.