adplus-dvertising
frame-decoration

Question

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

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 runs fine and 1 is printed

d.

The program has an error as you can’t name a class member using __b

Answer: (c).The program runs fine and 1 is printed

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the output of the following code?

Similar Questions

Discover Related MCQs

Q. Methods of a class that provide access to private members of the class are called as ______ and ______

Q. Private members of a class cannot be accessed. True or False?

Q. How many except statements can a try-except block have?

Q. When will the else part of try-except-else be executed?

Q. Can one block of except statements handle multiple exception?

Q. When is the finally block executed?

Q. What happens when ‘1’ == 1 is executed?

Q. Which of the following is not a standard exception in Python?

Q. Syntax errors are also known as parsing errors. Is this statement true or false?

Q. Which of the following statements is true ?

Q. Which of the following statements is true ?

Q. What is Instantiation in terms of OOP terminology?

Q. What is hasattr(obj,name) used for?

Q. What is delattr(obj,name) used for?

Q. __del__ method is used to destroy instances of a class. True or False?

Q. What does print(Test.__name__) display (assuming Test is the name of the class) ?

Q. When defining a subclass in Python that is meant to serve as a subtype, the subtype Python keyword is used. Is the statement true or false?

Q. Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?

Q. What does built-in function type do in context of classes?

Q. Which of the following is not a type of inheritance?