Exam PCAP All QuestionsBrowse all questions from this exam
Question 39

The following class hierarchy is given. What is the expected out of the code?

    Correct Answer: D

    The code defines a class hierarchy where Class B and Class C both inherit from Class A. When the method 'do()' is called on an instance of B or C, it invokes 'self.b()', which refers to the 'b()' method of Class A. The 'b()' method in Class A then calls 'self.a()'. Due to polymorphism, 'self.a()' refers to the overridden 'a()' method in the subclass. Therefore, for an instance of B, 'self.a()' refers to the 'a()' method in Class B, printing 'B'. Similarly, for an instance of C, 'self.a()' refers to the 'a()' method in Class C, printing 'C'. Thus, the expected output of the code is 'BC'.

Discussion
BenKaOption: D

No. Answer is D. B and C are subclass of A. Calling self.b in any of these class is a cas of polymorphism and the object on which a() will be applied is self from B and C respectively. So the output is BC.

FR99Option: D

Test the code, answer is definitely D. BC

sadako11Option: D

If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden. In other words A method of a parent class gets overridden by simply defining a method with the same name in the child class. If a method is overridden in a class, the original method can still be accessed, but we have to do it by calling the method directly with the class name.

TheFivePipsOption: D

I really highly reccommend anyone confused by this to step through the code in a debugger. You have to remember that the self variables are refrences to the actual objects( like obj1 = B() and obj2 = C()). When you try to access any object's entity, Python will try to (in this order): find it inside the object itself; find it in all classes involved in the object's inheritance line from bottom to top; This applies everytime we get sent to another class looking for the function. It will always check if its in the original object, that is calling the function originally, first(because it is refrencing the object by using the self keyword), before moving onto the class it is inherited from.

macxszOption: D

answer: D. BC

rocky48Option: D

Answer is D.

NiteshSinghOption: D

I ran the code and answer is correct. It is BC

[Removed]

Construcotors are not defined so the question is also weird

AshitAdhikari

A constructor is always implicitly called. In case is missing definition of the constructor inside the class, a default constructor is invoked (which need not be defined).

[Removed]Option: C

answer should be C: AA. It is calling the methods defined in the class A

wi11

absolutely wrong

seaverickOption: D

class A: def a (self): print("A",end=' ') def b (self): self.a() class B(A): def a (self): print("B",end=' ') def do (self): self.b() class C(A): def a (self): print("C",end=' ') def do (self): self.b() B().do() C().do() Output -> B C Ans is D

Valcon_doo_NoviSadOption: D

Expected output is BC. In both B and C class, method a() overrides its counterpart from the base class A.

BackyOption: D

Answer is D the output BC assumes running from a file when running in shell, the format will be B on one line and C on another line

TestPythOption: D

D ==> ANSWER