Bird
Raised Fist0

Given these classes:

hard🚀 Application Q9 of Q15
Python - Polymorphism and Dynamic Behavior
Given these classes:
class Printer:
    def print(self):
        return "Printing document"

class Scanner:
    def print(self):
        return "Scanning document"

devices = [Printer(), Scanner()]
for device in devices:
    print(device.print())

What does this example illustrate about polymorphism?
APolymorphism requires a common base class
BDifferent classes implement the same method name differently
CMethods must have different names in different classes
DInheritance is mandatory for polymorphism
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method usage

    Both Printer and Scanner have a print() method but return different strings.
  2. Step 2: Understand polymorphism concept

    This shows polymorphism by same method name with different behaviors in unrelated classes.
  3. Final Answer:

    Different classes implement the same method name differently -> Option B
  4. Quick Check:

    Polymorphism = same method, different implementations [OK]
Quick Trick: Polymorphism works without inheritance if method names match [OK]
Common Mistakes:
MISTAKES
  • Assuming inheritance is required
  • Thinking method names must differ
  • Believing polymorphism needs a base class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes