What will be the output of the following code?class A(): def disp(self): print("A disp()")class B(A): pass#Driver's codeobj = B()obj.disp()Options: Pick one correct answer from belowInvalid syntax for inheritanceError because when the object is created, the argument must be passedNothing is printedA disp()
Question
What will be the output of the following code?class A(): def disp(self): print("A disp()")class B(A): pass#Driver's codeobj = B()obj.disp()Options: Pick one correct answer from belowInvalid syntax for inheritanceError because when the object is created, the argument must be passedNothing is printedA disp()
Solution
The output of the code will be "A disp()".
Here's the step by step explanation:
-
Class A is defined with a method disp() that prints "A disp()".
-
Class B is defined which inherits from Class A but doesn't override the disp() method.
-
An object of Class B is created.
-
The disp() method is called on the object of Class B. Since Class B doesn't have its own disp() method, it will look up to its parent class, which is Class A.
-
The disp() method of Class A is called which prints "A disp()".
So, the correct answer is "A disp()".
Similar Questions
what will be the output of the following code?class Myclass: x=0 def incre(self): self.x+=1 def disp(self): print(self.x)ob1=Myclass()ob1.incre()ob1.disp()ob2=Myclass()ob2.incre()ob2.disp()
What will be the output of the following code?class A(): def disp(self): print("A disp()")class B(A): pass#Driver's codeobj = B()obj.disp()Options: Pick one correct answer from belowInvalid syntax for inheritanceError because when the object is created, the argument must be passedNothing is printedA disp()
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()
what is the output of following python code? class myclass: def __init__(self,a): self.a = a print(self.a)o=myclass()
What will be the output of the following code?class MyClass: def __init__(self, value=5): self.value = valueobj1 = MyClass()obj2 = MyClass(10)print(obj1.value, obj2.value)
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.