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 display(self): return self.__bobj = Demo()print(obj.a)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 runs fine and 1 is printedThe program has an error as you can’t name a class member using __b

Question

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.__bobj = Demo()print(obj.a)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 runs fine and 1 is printedThe program has an error as you can’t name a class member using __b

...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 an __init__ method and a display method. The __init__ method is a special method in Python classes which is automatically called when an object for the class is instantiated. It is used to initialize the attributes of the class.

  2. In the __init__ method, two instance variables self.a and self.__b are defined and both are assigned the value 1. Here, self.a is a public member and self.__b is a private member. In Python, private members are denoted by prefixing the member name with double underscore __.

  3. The display method returns the value of the private member self.__b.

  4. An object obj for the class Demo is created. This will call the __init__ method and initialize self.a and self.__b to 1.

  5. print(obj.a) is called which prints the value of self.a for the object obj. Since self.a is a public member, it can be accessed directly using the object. Hence, 1 is printed.

  6. There is no error in the program. The private member self.__b can be returned by a method in the same class. Also, you can name a class member using __b. The double underscore __ is used to denote private members in Python.

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

What will be the output of the following Python code? class student: def __init__(self): self.marks = 97 self.__cgpa = 8.7 def display(self): print(self.marks)obj=student()print(obj._student__cgpa) The program runs fine and 8.7 is printed Error because private class members can’t be accessed Error because the proper syntax for name mangling hasn’t been implemented The program runs fine but nothing is printedClear ResponseSave & Next

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.