What will be the output of the following Python code? [code] class Demo: def __init__(self): self.a = 1 self.__b

Thảo Justin

New member
What will be the output of the following Python code?
Code:
class Demo:
     def __init__(self):
         self.a = 1
         self.__b = 1
     def get(self):
         return self.__b
obj = Demo()
print(obj.get())
A. The program has an error because there isn’t any function to return self.a
B. The program has an error because b is private and display(self) is returning a private member
C. The program has an error because b is private and hence can’t be printed
D. The program runs fine and 1 is printed
 
Answer: d
Explanation: Here, get(self) is a member of the class. Hence, it can even return a private member of the class. Because of this reason, the program runs fine and 1 is printed.
 

Tìm kiếm

Back
Top