Object Relationships: Association, Aggregation, Composition Quiz Quiz

Deepen your understanding of object relationships by exploring key differences between association, aggregation, and composition in object-oriented design. This quiz is crafted to strengthen your grasp of these foundational concepts with practical examples and clear explanations.

  1. Identifying Association

    Which scenario best illustrates a simple association relationship in object-oriented programming?

    1. When a house is destroyed, its rooms are also destroyed.
    2. A car and its engine have a relationship where the engine cannot exist without the car.
    3. A teacher can teach many courses, and each course can have many teachers.
    4. A library contains books that cannot exist independently of the library.

    Explanation: In association, objects have a loose relationship where they can exist independently; for example, teachers and courses are related but do not depend on each other's existence. The car and engine example is closer to composition, as the engine typically cannot exist without the car. Books existing only within a library suggests composition, not association. Destruction of a house leading to the destruction of its rooms is another characteristic of composition, not association.

  2. Distinguishing Aggregation

    Which option correctly describes aggregation using a real-life analogy?

    1. A university and its departments, where departments can exist without the university.
    2. A heart and the person in which it resides; the heart cannot function separately.
    3. A computer and its RAM that is permanently fixed and cannot be reused.
    4. A parent and child, where the child ceases to exist if the parent is gone.

    Explanation: Aggregation represents a 'has-a' relationship with independent lifecycles; departments can exist outside the context of a university. The heart and person example is composition since the heart cannot function independently. Computer and RAM that cannot be reused implies composition, not aggregation. Inheritance relationships, like parent and child here implying dependency, are not examples of aggregation.

  3. Recognizing Composition

    In which scenario does composition most accurately take place?

    1. A car and its wheels, where the wheels are removed and used elsewhere.
    2. A person and the books they borrow from a library.
    3. A city and its residents, as residents can move to other cities.
    4. A house and its rooms, where rooms are destroyed if the house is destroyed.

    Explanation: Composition means that the child object’s lifecycle is tied to the parent; if the house is destroyed, its rooms cease to exist, fitting composition. Books borrowed from a library and residents in a city can exist independently, which demonstrates association. Car wheels reused elsewhere indicate aggregation, as wheels can exist independently from the car.

  4. Feature Comparison

    What is a key difference between aggregation and composition in terms of object lifecycle?

    1. In aggregation, the child object has an independent lifecycle from the parent object.
    2. In composition, the child object can outlive the parent object.
    3. In aggregation, the child object cannot exist without the parent object.
    4. Both aggregation and composition require the child to have only one parent object.

    Explanation: Aggregation allows the child to exist independently of the parent, whereas composition tightly couples the lifecycle so the child is destroyed with the parent. Saying composition allows the child to outlive the parent is incorrect; the opposite is true. The notion of aggregation requiring dependency is incorrect, and requiring only one parent applies more strictly to composition, not both.

  5. Diagram Symbolism

    In standard UML diagrams, how is a composition relationship typically represented?

    1. With a solid arrow pointing from parent to child.
    2. With a plain line connecting two classes.
    3. With an empty diamond at the parent class end.
    4. With a filled diamond at the parent class end.

    Explanation: A filled (black) diamond in UML denotes composition, indicating strong ownership and parent-child lifecycle dependency. A plain line illustrates association, not composition. An empty diamond represents aggregation, showing a weaker relationship. A solid arrow is typically used for inheritance or dependency, not for composition.