Knowee
Questions
Features
Study Tools

What is the output of the following code?        python    class A:        def display(self):            print("Class A")    class B(A):        def display(self):            print("Class B")    class C(A):        def display(self):            print("Class C")    obj = B()    obj.display()    obj = C()    obj.display()

Question

What is the output of the following code?        python    class A:        def display(self):            print("Class A")    class B(A):        def display(self):            print("Class B")    class C(A):        def display(self):            print("Class C")    obj = B()    obj.display()    obj = C()    obj.display()

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

Solution 1

The output of the code will be:

Class B
Class C

Here's the step by step explanation:

  1. Three classes are defined: A, B, and C. Each of them has a method called display(). Class B and C are subclasses of Class A.

  2. An object obj of Class B is created.

  3. The display() method of the obj object is called. Since obj is an instance of Class B, the display() method of Class B is executed, which prints "Class B".

  4. Then, obj is redefined as an object of Class C.

  5. The display() method of the obj object is called again. This time, since obj is an instance of Class C, the display() method of Class C is executed, which prints "Class C".

This problem has been solved

Solution 2

The output of the code will be:

Class B Class C

Explanation:

  1. The code defines three classes: A, B, and C. Each class has a method called display() that prints a string specific to the class.

  2. An object 'obj' of class B is created and the display() method is called. This prints "Class B".

  3. Then, the 'obj' is reassigned as an object of class C and again the display() method is called. This prints "Class C".

This problem has been solved

Similar Questions

what is the output of following python code? class myclass:    def display(self,a):        print(a)o=myclass()o.display("Hello World")

What is the output of the following code?     class Parent:        def show(self):            print("Parent")    class Child(Parent):        def show(self):            print("Child")    obj = Child()    obj.show()

What will be the output of the following Python code?class A: def __init__(self): self.__i = 1 self.j = 5  def display(self): print(self.__i, self.j)class B(A): def __init__(self): super().__init__() self.__i = 2 self.j = 7 c = B()c.display()2 71 51 72 5

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

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.