Bird
Raised Fist0

Identify the bug in the following Room class code:

medium📝 Analysis Q7 of Q15
LLD - Design — Hotel Booking System
Identify the bug in the following Room class code:
class Room:
    def __init__(self, number):
        number = number
        self.is_available = True
AThe is_available attribute should be set to False initially
BThe assignment should be self.number = number instead of number = number
CThe __init__ method is missing a return statement
DThe class should inherit from a base Room class
Step-by-Step Solution
Solution:
  1. Step 1: Analyze attribute assignment

    The line 'number = number' assigns the parameter to itself, not to an instance variable.
  2. Step 2: Correct assignment

    It should be 'self.number = number' to store the value in the instance attribute.
  3. Step 3: Other options

    is_available being True initially is valid; __init__ does not require a return; inheritance is not mandatory here.
  4. Final Answer:

    The assignment should be self.number = number instead of number = number -> Option B
  5. Quick Check:

    Instance variables require self prefix [OK]
Quick Trick: Instance variables must be assigned with self. prefix [OK]
Common Mistakes:
MISTAKES
  • Assigning parameter to itself instead of instance variable
  • Expecting __init__ to return a value
  • Changing default availability without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes