True or false: In object-oriented design, composition expresses a has-a relationship between objects.
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.
Which situation best illustrates delegation?
- A
A class stores a collaborator but performs every operation itself.
- B
A class receives a request and asks a collaborator to carry out the operation.
- C
A subclass inherits fields from its superclass.
- D
A class creates all of its parts internally.
A service receives a collaborator as a constructor parameter instead of creating it internally. What design technique is being used?
- A
The class creates a concrete dependency inside its method.
- B
The class receives its dependency through its constructor.
- C
The class inherits the dependency's implementation.
- D
The class accesses the dependency's private fields directly.
An AccountService depends on the NotificationSender interface rather than on EmailSender. Which two benefits follow from this design? Select all correct choices.
- A
The collaborator can be replaced by another implementation.
- B
The containing class automatically becomes a subtype of the collaborator.
- C
The same service can work with different implementations such as email or SMS.
- D
The collaborator's private fields become directly accessible.
In the material's distinction between strong and weak composition, what is characteristic of weaker composition?
- A
The component may be shared or replaced by the caller.
- B
The component must be created and destroyed only by the containing object.
- C
The component must be a superclass of the containing object.
- D
The component's fields must be public.
True or false: Because WeatherStation collaborates with TemperatureSensor through its public operations, it can remain independent of the sensor's private representation.
- A
True
- B
False
Which two situations favor composition over inheritance? Select all correct choices.
- A
The relationship is naturally uses-a rather than is-a.
- B
The subtype fully satisfies a stable superclass contract.
- C
The behavior may need to vary independently of the containing class.
- D
The main goal is to expose every inherited method.
What relationship does inheritance express between a subclass and its superclass?
What design pattern is illustrated when UserAccount contains a replaceable NotificationPolicy and delegates notification behavior to it?
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 .
Complete the comparison: Composition most directly represents a relationship, whereas inheritance most directly represents an relationship.
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.
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?
- A
Give UserAccount a NotificationPolicy collaborator and delegate notification to it.
- B
Create a separate UserAccount subclass for every notification mechanism.
- C
Make UserAccount directly implement every notification protocol.
- D
Copy the notification implementation into each account method.
Which condition is most important when deciding whether inheritance is appropriate?
- A
The subclass can reuse any convenient implementation detail.
- B
The subclass genuinely satisfies the supertype's meaning and contract.
- C
The subclass exposes more methods than its superclass.
- D
The subclass avoids having any collaborators.
Which statement correctly describes the relationship between Car and Engine in the composition example?
- A
A
Caris anEngine. - B
A
Carhas anEngine. - C
An
Engineis aCar. - D
A
Carinherits all ofEngine's implementation.
A class creates its own OrderLine objects and controls how long they exist. What kind of composition does this illustrate?
- A
The contained object is always shared by unrelated objects.
- B
The contained object must implement an interface.
- C
The containing object creates and controls the lifetime of its part.
- D
The containing object can never replace its part.
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.
- A
True
- B
False
In the redesigned UserAccount, notification behavior is supplied through a replaceable NotificationPolicy object. What design pattern does this illustrate?
Why does declaring AccountService.sender as a NotificationSender make the service flexible?
- A
It must create a separate
AccountServicesubclass for every notification channel. - B
It can collaborate with different implementations of
NotificationSender. - C
It can access the private fields of every sender implementation.
- D
It eliminates the need for any object to implement
send.
What principle requires a subtype to remain usable wherever its superclass is expected?