Bird
Raised Fist0

Given the following code, what will be the output?

medium📝 Analysis Q13 of Q15
LLD - Design — Chess Game
Given the following code, what will be the output?
class Piece:
    def move(self):
        return "Base move"

class Knight(Piece):
    def move(self):
        return "L-shaped move"

pieces = [Piece(), Knight()]
for p in pieces:
    print(p.move())
ABase move\nL-shaped move
BL-shaped move\nL-shaped move
CError: move() not implemented
DBase move\nBase move
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding

    Subclass Knight overrides move() to return "L-shaped move".
  2. Step 2: Trace the loop output

    First object is Piece, prints "Base move"; second is Knight, prints "L-shaped move".
  3. Final Answer:

    Base move\nL-shaped move -> Option A
  4. Quick Check:

    Base class and overridden subclass moves printed [OK]
Quick Trick: Subclass method overrides base method output [OK]
Common Mistakes:
MISTAKES
  • Assuming base method always runs
  • Expecting same output for all pieces
  • Confusing method overriding with overloading

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes