Bird
Raised Fist0

What will be the output of the following Python code?

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

class StudyRoom(Room):
    def __init__(self, name, has_desk):
        super().__init__(name)
        self.has_desk = has_desk

room = StudyRoom('QuietZone', True)
print(room.name, room.has_desk)
AQuietZone True
BQuietZone False
CError due to missing super call
DError due to incorrect attribute access
Step-by-Step Solution
Solution:
  1. Step 1: Understand Class Initialization

    The StudyRoom constructor calls super().__init__(name) to initialize the base Room class's name attribute.
  2. Step 2: Check Attribute Assignment

    The has_desk attribute is set to True during instantiation.
  3. Step 3: Output

    Printing room.name and room.has_desk outputs QuietZone True.
  4. Final Answer:

    QuietZone True -> Option A
  5. Quick Check:

    Correct super call and attribute access [OK]
Quick Trick: super() initializes base class attributes [OK]
Common Mistakes:
MISTAKES
  • Forgetting to call super().__init__
  • Misnaming attributes
  • Assuming default values incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes