Bird
Raised Fist0

Given the following pseudo-code, what will be the output when calling move() on each piece in the list?

medium📝 Analysis Q13 of Q15
LLD - Design — Chess Game
Given the following pseudo-code, what will be the output when calling move() on each piece in the list?
class Piece { move() { return 'generic move'; } } class Knight extends Piece { move() { return 'L-shape move'; } } class Bishop extends Piece { move() { return 'diagonal move'; } } pieces = [new Piece(), new Knight(), new Bishop()] for p in pieces: print(p.move())
AL-shape move\ndiagonal move\ngeneric move
Bgeneric move\nL-shape move\ndiagonal move
Cgeneric move\ngeneric move\ngeneric move
DError: move method not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding in subclasses

    Each subclass overrides move() to return its specific move string.
  2. Step 2: Trace the loop calling move()

    For Piece instance, move() returns 'generic move'. For Knight, 'L-shape move'. For Bishop, 'diagonal move'.
  3. Final Answer:

    generic move\nL-shape move\ndiagonal move -> Option B
  4. Quick Check:

    Overridden methods print their own strings [OK]
Quick Trick: Each subclass method overrides base method output [OK]
Common Mistakes:
MISTAKES
  • Assuming base method output for all pieces
  • Mixing order of outputs
  • Expecting runtime errors incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes