Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Polymorphism and Dynamic Behavior
What will be the output of this code?
from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Bark"

print(Dog().sound())
ABark
BTypeError
CNone
DAttributeError
Step-by-Step Solution
Solution:
  1. Step 1: Understand abstract base class behavior

    The abstract method sound must be implemented in subclass Dog. Here, Dog provides the implementation returning "Bark".
  2. Step 2: Predict output of print statement

    Creating Dog() instance is allowed because all abstract methods are implemented. Calling sound() returns "Bark" which is printed.
  3. Final Answer:

    Bark -> Option A
  4. Quick Check:

    Implemented abstract method returns 'Bark' [OK]
Quick Trick: Subclass must implement all abstract methods to instantiate [OK]
Common Mistakes:
  • Expecting error despite full implementation
  • Confusing abstract method with normal method
  • Thinking abstract base class can be instantiated

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes