Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.

Unlimited Access

Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.

Exam PCAP topic 1 question 39 discussion

Actual exam question from Python Institute's PCAP
Question #: 39
Topic #: 1
[All PCAP Questions]

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

  • A. BB
  • B. CC
  • C. AA
  • D. BC
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
BenKa
Highly Voted 3 years, 8 months ago
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.
upvoted 14 times
...
FR99
Highly Voted 3 years, 6 months ago
Test the code, answer is definitely D. BC
upvoted 5 times
...
seaverick
Most Recent 2 months, 4 weeks ago
Selected Answer: 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
upvoted 1 times
...
TheFivePips
4 months, 2 weeks ago
Selected Answer: 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.
upvoted 2 times
...
Valcon_doo_NoviSad
6 months, 2 weeks ago
Selected Answer: D
Expected output is BC. In both B and C class, method a() overrides its counterpart from the base class A.
upvoted 1 times
...
macxsz
1 year, 11 months ago
Selected Answer: D
answer: D. BC
upvoted 2 times
...
rocky48
2 years, 1 month ago
Selected Answer: D
Answer is D.
upvoted 2 times
...
Backy
2 years, 2 months ago
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
upvoted 1 times
...
TestPyth
2 years, 3 months ago
D ==> ANSWER
upvoted 1 times
...
NiteshSingh
2 years, 6 months ago
I ran the code and answer is correct. It is BC
upvoted 2 times
...
sadako11
3 years, 4 months ago
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.
upvoted 4 times
...
[Removed]
3 years, 8 months ago
Construcotors are not defined so the question is also weird
upvoted 2 times
AshitAdhikari
2 years, 9 months ago
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).
upvoted 1 times
...
...
[Removed]
3 years, 8 months ago
answer should be C: AA. It is calling the methods defined in the class A
upvoted 2 times
wi11
3 years ago
absolutely wrong
upvoted 2 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...