Bird
0
0

What will be the output of the following Python code snippet?

medium📝 Analysis Q5 of 15
LLD - Design — Library Management System
What will be the output of the following Python code snippet?
class Member:
    def __init__(self, name):
        self.name = name

class Book:
    def __init__(self, title):
        self.title = title

class Loan:
    def __init__(self, book, member):
        self.book = book
        self.member = member

book = Book('The Hobbit')
member = Member('Alice')
loan = Loan(book, member)
print(loan.book.title, loan.member.name)
AThe Hobbit member
Bbook member
CThe Hobbit Alice
DError: AttributeError
Step-by-Step Solution
Solution:
  1. Step 1: Understand class initializations

    The Book and Member classes initialize with title and name respectively.
  2. Step 2: Loan class stores references

    The Loan class stores references to Book and Member instances.
  3. Step 3: Print statement

    loan.book.title accesses 'The Hobbit' and loan.member.name accesses 'Alice'.
  4. Final Answer:

    The Hobbit Alice -> Option C
  5. Quick Check:

    Accessing attributes correctly [OK]
Quick Trick: Access attributes via object references [OK]
Common Mistakes:
MISTAKES
  • Confusing instance variables with class variables
  • Trying to print object directly without attribute
  • Misunderstanding object references

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes