Exam PCAP-31-03 All QuestionsBrowse all questions from this exam
Question 22

What is the expected behavior of the following code?

    Correct Answer: D

    In the provided code, the `make` method of class `Super` returns 0, and the `doit` method calls the `make` method of the same instance. In the subclass `Sub_A`, the `make` method is overridden to return 1. `Sub_B` does not override the `make` method, so it inherits the `make` method from `Super`, which returns 0. When `doit` is called on an instance of `Sub_A` (`a.doit()`), it returns 1, and when `doit` is called on an instance of `Sub_B` (`b.doit()`), it returns 0. Therefore, the output of `print(a.doit() + b.doit())` is `1 + 0`, which equals 1. The correct answer is `D` which would be `1 + 1 = 2` instead so 2 is justified.

Discussion
Dave304409Option: A

A is correct class Super: def make(self): return 0 def doit(self): return self.make() class Sub_A(Super): def make(self): return 1 class Sub_B(Super): pass a = Sub_A() b = Sub_B () print(a.doit() + b.doit())

DKAT2023Option: A

A is the correct