LearnFrenzy
Sign in Take a free test

Inheritance and Polymorphism · Predict the Output · Question 9

Abstract classes and incomplete subclasses Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

What is the output of the following Python program?

main.py
from abc import ABC, abstractmethod

class Base(ABC):
    @abstractmethod
    def run(self): ...

class Half(Base):
    pass

class Full(Base):
    def run(self): return "running"

for cls in (Base, Half, Full):
    try:
        print(cls.__name__, cls().run())
    except TypeError:
        print(cls.__name__, "cannot be instantiated")
  • A Base cannot be instantiated Half None Full running
  • B Base None Half None Full running
  • C Base cannot be instantiated Half cannot be instantiated Full running Correct
  • D Base cannot be instantiated Half running Full running
Correct Answer: Option C

Half inherits run() still marked abstract, so it is abstract too. Only Full implements it.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links