Bird
Raised Fist0

Find the bug in this code:

medium📝 Analysis Q7 of Q15
LLD - Design — Chess Game
Find the bug in this code:
class Piece:
    def move(self):
        return "Moving"

class Rook(Piece):
    def move(self):
        print("Rook moves straight")

r = Rook()
result = r.move()
print(result)
ARook.move() should return a string, not print it.
BBase class move() should print, not return.
CNo bug; code works as expected.
DRook class should not override move().
Step-by-Step Solution
Solution:
  1. Step 1: Analyze Rook.move() method

    Rook.move() prints a message but does not return anything, so returns None by default.
  2. Step 2: Check usage of move() return value

    result = r.move() assigns None, so print(result) outputs None, which is likely unintended.
  3. Final Answer:

    Rook.move() should return a string, not print it. -> Option A
  4. Quick Check:

    Override should match base method return type [OK]
Quick Trick: Override methods should return expected values, not just print [OK]
Common Mistakes:
MISTAKES
  • Confusing print with return
  • Ignoring return value usage
  • Assuming no bug if output appears

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes