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
Solution
The program runs fine and 1 is printed.
Here's the step by step explanation:
-
A class named
Demois defined with an__init__method and adisplaymethod. 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. -
In the
__init__method, two instance variablesself.aandself.__bare defined and both are assigned the value 1. Here,self.ais a public member andself.__bis a private member. In Python, private members are denoted by prefixing the member name with double underscore__. -
The
displaymethod returns the value of the private memberself.__b. -
An object
objfor the classDemois created. This will call the__init__method and initializeself.aandself.__bto 1. -
print(obj.a)is called which prints the value ofself.afor the objectobj. Sinceself.ais a public member, it can be accessed directly using the object. Hence, 1 is printed. -
There is no error in the program. The private member
self.__bcan 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.
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
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.