Question 9
Back to Exercise
What is the output of the following Python program?
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!