Knowee
Questions
Features
Study Tools

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.

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.

...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), which sets the instance variable 'a' to 1 and the private instance variable '__b' to 1.

  2. A method named 'get' is defined which returns the value of the private instance variable '__b'.

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

  4. The instance variable 'a' of the 'obj' instance is set to 45.

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

The private instance variable '__b' is not accessed or modified in this code, so it doesn't affect the output. The statement about not being able to change the value of members of a class from outside the class is not correct in this case, as we can see that 'obj.a' is successfully changed to 45.

This problem has been solved

Similar Questions

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)

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

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.