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)
Question
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)
Solution 1
The output of the code will be:
5 10
Explanation:
-
A class named MyClass is defined with an init method. The init method is a special method in Python, which is automatically called when an object of the class is created. It is used to initialize the attributes of the class.
-
The init method takes two parameters - 'self' and 'value'. 'self' is a reference to the current instance of the class and is used to access variables and methods associated with that instance. 'value' is an optional parameter with a default value of 5.
-
An object 'obj1' of MyClass is created without passing any argument. So, the default value of 'value' (which is 5) is used.
-
Another object 'obj2' of MyClass is created, this time passing 10 as the argument. So, 'value' for 'obj2' is set to 10.
-
Finally, the values of 'value' for 'obj1' and 'obj2' are printed, which are 5 and 10 respectively.
Solution 2
The output of the code will be:
5 10
Explanation:
-
A class named MyClass is defined with an init method. The init method is a special method in Python, which is automatically called when an object of the class is created. It is used to initialize the attributes of the class.
-
The init method takes two parameters - 'self' and 'value'. 'self' is a reference to the current instance of the class and is used to access variables and methods associated with that instance. 'value' is a parameter with a default value of 5.
-
An object 'obj1' of MyClass is created without passing any argument. So, the default value 5 is used to initialize 'value'.
-
Another object 'obj2' of MyClass is created, this time passing the argument 10. So, 'value' is initialized with 10 for 'obj2'.
-
Finally, the values of 'value' for 'obj1' and 'obj2' are printed, which are 5 and 10 respectively.
Solution 3
The output of the code will be:
5 10
Explanation:
-
A class named MyClass is defined with an init method. The init method is a special method in Python, which is automatically called when an object of the class is created. It is used to initialize the attributes of the class.
-
The init method takes two parameters - 'self' and 'value'. 'self' is a reference to the current instance of the class and is used to access variables and methods associated with that instance. 'value' is a parameter with a default value of 5.
-
An object 'obj1' of MyClass is created without passing any argument. So, the default value of 'value' (which is 5) is used.
-
Another object 'obj2' of MyClass is created, this time passing 10 as the argument. So, 'value' for 'obj2' is set to 10.
-
Finally, the values of 'value' for 'obj1' and 'obj2' are printed, which are 5 and 10 respectively.
Solution 4
The output of the code will be:
5 10
Explanation:
-
A class named MyClass is defined with an init method. The init method is a special method in Python, which is automatically called when an object of the class is created. It is used to initialize the attributes of the class.
-
The init method takes two parameters - 'self' and 'value'. 'self' is a reference to the current instance of the class and is used to access variables and methods associated with that instance. 'value' is a parameter with a default value of 5.
-
An object 'obj1' of MyClass is created without passing any argument. So, the default value of 'value' (which is 5) is used.
-
Another object 'obj2' of MyClass is created, this time passing 10 as an argument. So, 'value' for 'obj2' is set to 10.
-
Finally, the values of 'value' for 'obj1' and 'obj2' are printed, which are 5 and 10 respectively.
Solution 5
The output of the code will be:
5 10
Explanation:
-
A class named MyClass is defined with an init method. The init method is a special method in Python, which is automatically called when an object of the class is created. It is used to initialize the attributes of the class.
-
The init method takes two parameters - 'self' and 'value'. 'self' is a reference to the current instance of the class and is used to access variables and methods associated with that instance. 'value' is a parameter with a default value of 5.
-
An object 'obj1' of MyClass is created without passing any argument. So, the default value of 'value' i.e., 5 is used.
-
Another object 'obj2' of MyClass is created by passing an argument 10. So, this value overrides the default value of 'value'.
-
When we print obj1.value and obj2.value, it prints the 'value' attribute of obj1 and obj2 respectively. Hence, the output is 5 10.
Similar Questions
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)
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)
What will be the output of the following Python code?class fruits: def __init__(self): self.price = 100 self.__bags = 5 def display(self): print(self.__bags)obj=fruits()obj.display()
What will be the output of the following Python code?class A: def __init__(self): self.__i = 1 self.j = 5 def display(self): print(self.__i, self.j)class B(A): def __init__(self): super().__init__() self.__i = 2 self.j = 7 c = B()c.display()2 71 51 72 5
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.