Free Practice Quiz Question List

7 Composition and Delegation Online Quiz Questions

Use this free practice quiz with 20 questions to review 7 Composition and Delegation, test your knowledge, and prepare for your next test or exam.

20 questions
01
True or false
1 point

True or false: In object-oriented design, composition expresses a has-a relationship between objects.

  1. A

    True

  2. B

    False

02
Choose one
1 point

Which situation best illustrates delegation?

  1. A

    A class stores a collaborator but performs every operation itself.

  2. B

    A class receives a request and asks a collaborator to carry out the operation.

  3. C

    A subclass inherits fields from its superclass.

  4. D

    A class creates all of its parts internally.

03
Choose one
1 point

A service receives a collaborator as a constructor parameter instead of creating it internally. What design technique is being used?

  1. A

    The class creates a concrete dependency inside its method.

  2. B

    The class receives its dependency through its constructor.

  3. C

    The class inherits the dependency's implementation.

  4. D

    The class accesses the dependency's private fields directly.

04
Choose all
1 point

An AccountService depends on the NotificationSender interface rather than on EmailSender. Which two benefits follow from this design? Select all correct choices.

  1. A

    The collaborator can be replaced by another implementation.

  2. B

    The containing class automatically becomes a subtype of the collaborator.

  3. C

    The same service can work with different implementations such as email or SMS.

  4. D

    The collaborator's private fields become directly accessible.

05
Choose one
1 point

In the material's distinction between strong and weak composition, what is characteristic of weaker composition?

  1. A

    The component may be shared or replaced by the caller.

  2. B

    The component must be created and destroyed only by the containing object.

  3. C

    The component must be a superclass of the containing object.

  4. D

    The component's fields must be public.

06
True or false
1 point

True or false: Because WeatherStation collaborates with TemperatureSensor through its public operations, it can remain independent of the sensor's private representation.

  1. A

    True

  2. B

    False

07
Choose all
1 point

Which two situations favor composition over inheritance? Select all correct choices.

  1. A

    The relationship is naturally uses-a rather than is-a.

  2. B

    The subtype fully satisfies a stable superclass contract.

  3. C

    The behavior may need to vary independently of the containing class.

  4. D

    The main goal is to expose every inherited method.

08
Written response
1 point

What relationship does inheritance express between a subclass and its superclass?

09
Written response
1 point

What design pattern is illustrated when UserAccount contains a replaceable NotificationPolicy and delegates notification behavior to it?

10
Fill in the blank
1 point

Complete the statements: When one object passes responsibility for carrying out an operation to another object, this is called . The object performing the delegated work is the .

11
Fill in the blank
1 point

Complete the comparison: Composition most directly represents a relationship, whereas inheritance most directly represents an relationship.

12
Open ended
1 point

A developer proposes making Report extend Printer solely because reports can be printed. Explain the design you would recommend and why it better represents the relationship.

13
Choose one
1 point

An application may support email, SMS, push, and in-app notifications, and the notification mechanism may change independently of the account. Which design best addresses this requirement?

  1. A

    Give UserAccount a NotificationPolicy collaborator and delegate notification to it.

  2. B

    Create a separate UserAccount subclass for every notification mechanism.

  3. C

    Make UserAccount directly implement every notification protocol.

  4. D

    Copy the notification implementation into each account method.

14
Choose one
1 point

Which condition is most important when deciding whether inheritance is appropriate?

  1. A

    The subclass can reuse any convenient implementation detail.

  2. B

    The subclass genuinely satisfies the supertype's meaning and contract.

  3. C

    The subclass exposes more methods than its superclass.

  4. D

    The subclass avoids having any collaborators.

15
Choose one
1 point

Which statement correctly describes the relationship between Car and Engine in the composition example?

  1. A

    A Car is an Engine.

  2. B

    A Car has an Engine.

  3. C

    An Engine is a Car.

  4. D

    A Car inherits all of Engine's implementation.

16
Choose one
1 point

A class creates its own OrderLine objects and controls how long they exist. What kind of composition does this illustrate?

  1. A

    The contained object is always shared by unrelated objects.

  2. B

    The contained object must implement an interface.

  3. C

    The containing object creates and controls the lifetime of its part.

  4. D

    The containing object can never replace its part.

17
True or false
1 point

True or false: When a class uses composition to contain another object, the containing class automatically becomes a subtype of the contained object's class.

  1. A

    True

  2. B

    False

18
Written response
1 point

In the redesigned UserAccount, notification behavior is supplied through a replaceable NotificationPolicy object. What design pattern does this illustrate?

19
Choose one
1 point

Why does declaring AccountService.sender as a NotificationSender make the service flexible?

  1. A

    It must create a separate AccountService subclass for every notification channel.

  2. B

    It can collaborate with different implementations of NotificationSender.

  3. C

    It can access the private fields of every sender implementation.

  4. D

    It eliminates the need for any object to implement send.

20
Written response
1 point

What principle requires a subtype to remain usable wherever its superclass is expected?