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

...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 automatically called when an object of this class is created. It sets the instance variable a to 1 and __b (a private variable) to 1.

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

  3. An object obj of the class Demo is created. This automatically calls the __init__ method and sets a and __b to 1.

  4. The value of a in the object obj is changed to 45.

  5. The value of a in the object obj is 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.

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

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.