Bird
Raised Fist0

What will be the output of the following code snippet?

medium📝 Analysis Q4 of Q15
LLD - Design — Hotel Booking System
What will be the output of the following code snippet?
class Room:
    def __init__(self, number):
        self.number = number
        self.is_available = True

class Booking:
    def __init__(self, room):
        self.room = room
        self.room.is_available = False

room1 = Room(202)
booking1 = Booking(room1)
print(room1.is_available)
AFalse
BTrue
CNone
DError
Step-by-Step Solution
Solution:
  1. Step 1: Initialize Room object

    The Room object room1 is created with number 202 and is_available set to True.
  2. Step 2: Initialize Booking object

    The Booking object booking1 is created with room1 passed as parameter. Inside Booking's constructor, room1.is_available is set to False.
  3. Step 3: Print availability

    Printing room1.is_available will output False because the Booking constructor changed it.
  4. Final Answer:

    False -> Option A
  5. Quick Check:

    Booking modifies room availability correctly [OK]
Quick Trick: Booking constructor changes room availability to False [OK]
Common Mistakes:
MISTAKES
  • Assuming room availability remains True after booking
  • Confusing room number with availability status
  • Expecting an error due to missing code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes