Bird
Raised Fist0

Which of the following code snippets correctly shows polymorphism for chess pieces in a low-level design?

easy🧠 Conceptual Q12 of Q15
LLD - Design — Chess Game
Which of the following code snippets correctly shows polymorphism for chess pieces in a low-level design?
Aclass Piece { move() { /* generic move */ } } class Pawn extends Piece { move() { /* pawn move */ } }
Bclass Pawn { move() { /* pawn move */ } } class Knight { jump() { /* knight jump */ } }
Cfunction move(piece) { if(piece.type == 'pawn') { /* move */ } else { /* no move */ } }
Dclass Piece { move() { console.log('move'); } } let piece = new Piece(); piece.move();
Step-by-Step Solution
Solution:
  1. Step 1: Identify polymorphism in code

    Polymorphism requires a base class with a method overridden by subclasses. class Piece { move() { /* generic move */ } } class Pawn extends Piece { move() { /* pawn move */ } } shows a base Piece class with move(), overridden by Pawn.
  2. Step 2: Check other options for polymorphism

    class Pawn { move() { /* pawn move */ } } class Knight { jump() { /* knight jump */ } } lacks shared method names; function move(piece) { if(piece.type == 'pawn') { /* move */ } else { /* no move */ } } uses conditional logic, not polymorphism; class Piece { move() { console.log('move'); } } let piece = new Piece(); piece.move(); has no subclassing.
  3. Final Answer:

    class Piece { move() { /* generic move */ } } class Pawn extends Piece { move() { /* pawn move */ } } -> Option A
  4. Quick Check:

    Base class + overridden method = polymorphism [OK]
Quick Trick: Look for base class with overridden methods [OK]
Common Mistakes:
MISTAKES
  • Confusing conditional logic with polymorphism
  • Missing method overriding in subclasses
  • Ignoring inheritance structure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes