Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The program runs properly and prints 45.

Here's the step by step explanation:

  1. A class named 'Demo' is defined with an initializer method (__init__). This method is used to initialize the attributes of an instance. In this case, the attributes 'a' and '__b' are initialized with the value 1.

  2. A method named 'get' is defined which returns the value of the attribute '__b'.

  3. An instance of the class 'Demo' is created and assigned to the variable 'obj'.

  4. The attribute 'a' of the instance 'obj' is changed to 45.

  5. The value of 'obj.a' is printed, which is 45.

The attribute 'a' can be accessed and modified directly because it is a public member of the class. On the other hand, '__b' is a private member, so it can't be accessed or modified directly from outside the class. However, this doesn't affect the execution of the given code.

This problem has been solved

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)

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

class Demo: def __init__(self): self.a = 1 self.__b = 1  def display(self): return self.__b obj = Demo()print(obj.__b) 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

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 code?class MyClass:    def __init__(self, value=5):        self.value = valueobj1 = MyClass()obj2 = MyClass(10)print(obj1.value, obj2.value)

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.