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) The program runs properly and prints 45 The program has an error because the value of members of a class can’t be changed from outside the classThe program runs properly and prints 1 The program has an error because the value of members outside a class can only be changed as self.a=45
Question
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) The program runs properly and prints 45 The program has an error because the value of members of a class can’t be changed from outside the classThe program runs properly and prints 1 The program has an error because the value of members outside a class can only be changed as self.a=45
Solution
The program runs properly and prints 45.
Here's the step by step explanation:
-
A class named
Demois defined with an initializer method (__init__). This method is automatically called when an object of this class is created. It sets the instance variableato 1 and__b(a private variable) to 1. -
A method named
getis defined which returns the value of the private variable__b. -
An object
objof the classDemois created. This automatically calls the__init__method and setsaand__bto 1. -
The value of
ain the objectobjis changed to 45. -
The value of
ain the objectobjis printed, which is 45.
The program does not have an error because the value of public members of a class can be changed from outside the class. The variable a is a public member of the class, so it can be changed from outside the class. The variable __b is a private member, so it can't be accessed directly from outside the class, but it doesn't affect the program because it's not being accessed or changed.
Similar Questions
class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a) The program runs properly and prints 45 The program has an error because the value of members of a class can’t be changed from outside the classThe program runs properly and prints 1 The program has an error because the value of members outside a class can only be changed as self.a=45Clear ResponseSave & Next
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 Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__b obj = Demo()print(obj.get())The program has an error because there isn’t any function to return self.aThe program has an error because b is private and display(self) is returning a private memberThe program has an error because b is private and hence can’t be printedThe program runs fine and 1 is printed
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 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
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.