Knowee
Questions
Features
Study Tools

class Sample: num=0 def __init__(self, var): Sample.num+=1 self.var=var print("The object value is = ", var) print("The count of object created = ", Sample.num) S1=Sample(15) S2=Sample(35) S3=Sample(45) write the output of this code

Question

class Sample: num=0 def init(self, var): Sample.num+=1 self.var=var print("The object value is = ", var) print("The count of object created = ", Sample.num) S1=Sample(15) S2=Sample(35) S3=Sample(45) write the output of this code

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

Solution

The output of this code will be:

The object value is = 15 The count of object created = 1 The object value is = 35 The count of object created = 2 The object value is = 45 The count of object created = 3

Explanation: This is a Python code. The class 'Sample' has a class variable 'num' which is incremented each time an object of the class is created. The 'init' method is a special method in Python classes which is automatically called when an object is created. In this method, the 'num' variable is incremented and the value passed to the object is printed. So, for each object creation, the value passed and the count of objects created so far is printed.

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)

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

What will be the output of the following code?class MyClass:    class_variable = 10    def __init__(self, instance_variable):        self.instance_variable = instance_variableobj1 = MyClass(20)obj2 = MyClass(30)MyClass.class_variable = 40print(obj1.class_variable, obj2.class_variable)10 10 20 3040 4020 40

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.