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

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

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

Solution

The program runs fine and 1 is printed.

Here's the step by step explanation:

  1. A class named 'Demo' is defined with two attributes 'a' and '__b'. Both are initialized with the value 1. The double underscore before 'b' makes it a private attribute.

  2. A method named 'get' is defined within the class 'Demo'. This method returns the value of the private attribute '__b'.

  3. An object 'obj' of the class 'Demo' is created.

  4. The 'get' method is called using the object 'obj'. This returns the value of '__b', which is 1.

  5. The returned value is printed. Hence, the output of the program is 1.

The program doesn't have any errors. Even though '__b' is a private attribute, it can be accessed within the class it is defined, including in the 'get' method. Therefore, the program runs fine and 1 is printed.

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

What will be the output of the following Python code?class Demo: def __init__(self): self.a = 1 self.__b = 1  def display(self): return self.__b obj = Demo()print(obj.__b)

What will be the output of the following Python code?class A: def __init__(self): self.__x = 1class B(A): def display(self): print(self.__x)def main(): obj = B() obj.display()main()10Error, invalid syntax for object declarationError, private class member can’t be accessed in a subclass

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()  The program has an error because display() is trying to print a private class memberThe program runs fine but nothing is printedThe program runs fine and 5 is printed The program has an error because display() can’t be accessed

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

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.