Bird
Raised Fist0
LLDsystem_design~10 mins

Relationships (association, aggregation, composition) in LLD - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an association relationship between two classes.

LLD
class Teacher:
    def __init__(self, name):
        self.name = name

class Student:
    def __init__(self, name, teacher):
        self.name = name
        self.teacher = [1]
Drag options to blanks, or click blank then click option'
Ateacher
Bname
CStudent
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'teacher' causes the student to store a string, not the teacher object.
Using 'self' or 'Student' is incorrect as they do not refer to the teacher instance.
2fill in blank
medium

Complete the code to show aggregation where a Library has many Books.

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

class Library:
    def __init__(self):
        self.books = []

    def add_book(self, book):
        self.books.[1](book)
Drag options to blanks, or click blank then click option'
Aremove
Bappend
Cclear
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'remove' or 'pop' deletes items instead of adding.
Using 'clear' empties the list, which is not intended.
3fill in blank
hard

Fix the error in the composition example where a Car has an Engine.

LLD
class Engine:
    def __init__(self, power):
        self.power = power

class Car:
    def __init__(self, power):
        self.engine = [1](power)
Drag options to blanks, or click blank then click option'
Apower
Bengine
CEngine
DCar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'engine' or 'power' as a function causes errors.
Using 'Car' tries to create a Car inside Car, which is incorrect.
4fill in blank
hard

Fill both blanks to complete the aggregation example where a Team has multiple Players.

LLD
class Player:
    def __init__(self, name):
        self.name = name

class Team:
    def __init__(self):
        self.players = []

    def add_player(self, player):
        self.players.[1](player)

    def remove_player(self, player):
        self.players.[2](player)
Drag options to blanks, or click blank then click option'
Aappend
Bpop
Cremove
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop to remove by value causes errors.
Using clear removes all players, not just one.
5fill in blank
hard

Fill all three blanks to complete the composition example where a House has Rooms with names and sizes.

LLD
class Room:
    def __init__(self, name, size):
        self.name = [1]
        self.size = [2]

class House:
    def __init__(self):
        self.rooms = []

    def add_room(self, name, size):
        room = [3](name, size)
        self.rooms.append(room)
Drag options to blanks, or click blank then click option'
Aname
Bsize
CRoom
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up parameter names or using 'self' incorrectly.
Not calling the Room class to create a new object.

Practice

(1/5)
1. Which relationship describes two objects connected without ownership, where both can exist independently?
easy
A. Inheritance
B. Association
C. Composition
D. Aggregation

Solution

  1. Step 1: Understand relationship types

    Association means objects are linked but do not own each other and can exist separately.
  2. Step 2: Compare with other relationships

    Aggregation and composition imply ownership; inheritance is a different concept.
  3. Final Answer:

    Association -> Option B
  4. Quick Check:

    Association = connected without ownership [OK]
Hint: No ownership means association, not aggregation or composition [OK]
Common Mistakes:
  • Confusing aggregation with association
  • Thinking composition allows independent existence
  • Mixing inheritance with relationships
2. Which of the following correctly shows a composition relationship in UML notation?
easy
A. A solid diamond at the owner end connected to the part
B. A hollow diamond at the owner end connected to the part
C. A plain line with no diamonds between objects
D. A dashed arrow pointing from part to owner

Solution

  1. Step 1: Recall UML notation for composition

    Composition uses a solid diamond at the owner end to show strong ownership.
  2. Step 2: Differentiate from aggregation and association

    Aggregation uses hollow diamond; association uses plain line; dashed arrow is for dependency.
  3. Final Answer:

    A solid diamond at the owner end connected to the part -> Option A
  4. Quick Check:

    Composition = solid diamond [OK]
Hint: Solid diamond means composition, hollow means aggregation [OK]
Common Mistakes:
  • Mixing hollow and solid diamonds
  • Confusing association line with composition
  • Using dashed arrows for composition
3. Consider this code snippet representing relationships:
class Engine:
    pass

class Car:
    def __init__(self):
        self.engine = Engine()
What type of relationship does Car have with Engine?
medium
A. Association
B. Aggregation
C. Inheritance
D. Composition

Solution

  1. Step 1: Analyze ownership in code

    Car creates and owns Engine instance inside its constructor, implying strong ownership.
  2. Step 2: Match with relationship definitions

    Since Engine cannot exist meaningfully without Car here, this is composition.
  3. Final Answer:

    Composition -> Option D
  4. Quick Check:

    Car owns Engine fully = Composition [OK]
Hint: Creating owned object inside means composition [OK]
Common Mistakes:
  • Thinking it's aggregation because of ownership
  • Confusing association with composition
  • Assuming inheritance from code
4. Identify the error in this aggregation example:
class Wheel:
    pass

class Car:
    def __init__(self, wheels):
        self.wheels = wheels

car = Car([Wheel(), Wheel()])
What is wrong with this aggregation?
medium
A. Wheels cannot exist independently in aggregation
B. Car should not hold wheels as a list
C. No error; this is correct aggregation
D. Wheels should be created inside Car for composition

Solution

  1. Step 1: Understand aggregation ownership

    Aggregation means Car owns wheels but wheels can exist independently.
  2. Step 2: Check code for ownership and independence

    Wheels are created outside Car and passed in, so they can exist alone, matching aggregation.
  3. Final Answer:

    No error; this is correct aggregation -> Option C
  4. Quick Check:

    Aggregation allows independent parts [OK]
Hint: Aggregation parts created outside owner, passed in [OK]
Common Mistakes:
  • Thinking wheels must be created inside Car
  • Confusing aggregation with composition
  • Assuming list usage is wrong
5. You are designing a library system where a Library contains many Books. Books can exist outside the library and be moved between libraries. Which relationship best fits this scenario and why?
hard
A. Aggregation, because library owns books but books can exist alone
B. Composition, because books depend fully on the library
C. Association, because library and books are loosely connected
D. Inheritance, because books inherit properties from library

Solution

  1. Step 1: Analyze ownership and existence

    Books can exist independently and move between libraries, so they are not fully owned.
  2. Step 2: Match relationship type

    Aggregation fits because library owns books but books can exist alone and be shared.
  3. Final Answer:

    Aggregation, because library owns books but books can exist alone -> Option A
  4. Quick Check:

    Aggregation = ownership with independent parts [OK]
Hint: If parts can exist alone, use aggregation [OK]
Common Mistakes:
  • Choosing composition thinking books depend fully
  • Selecting association ignoring ownership
  • Confusing inheritance with ownership
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard