Bird
Raised Fist0

Which of the following is the correct way to define a Group class that holds multiple User objects in Python?

easy🧠 Conceptual Q12 of Q15
LLD - Design — Splitwise (Expense Sharing)
Which of the following is the correct way to define a Group class that holds multiple User objects in Python?
Aclass Group: def __init__(self): self.users = []
Bclass Group: def __init__(self): self.user = {}
Cclass Group: def __init__(self): self.expenses = []
Dclass Group: def __init__(self): self.members = None
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct attribute for multiple users

    A list is suitable to hold multiple User objects, so self.users = [] is correct.
  2. Step 2: Check other options for correctness

    class Group: def __init__(self): self.user = {} uses a dict named user which is not typical for holding users; class Group: def __init__(self): self.expenses = [] uses expenses which belongs to Expense class; class Group: def __init__(self): self.members = None sets members to None which is not a collection.
  3. Final Answer:

    class Group: def __init__(self): self.users = [] -> Option A
  4. Quick Check:

    Group holds list of users = self.users = [] [OK]
Quick Trick: Group holds list of users with self.users = [] [OK]
Common Mistakes:
MISTAKES
  • Using dict instead of list for users
  • Confusing expenses with users
  • Initializing members as None instead of a list

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes