Decorator Pattern — Concept Check
Questions on Decorator Pattern. Decorator wraps an object to add behaviour dynamically, composing features at runtime without modifying the original class.
Questions in this quiz
8 questions on Decorator Pattern — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a Coffee object wrapped with two Milk decorators and one Vanilla decorator. What is the final description?
- ACoffee, Milk, Milk, Vanilla
- BCoffee, Vanilla, Milk
- CCoffee, Milk, Vanilla, Milk
- DCoffee, Vanilla, Milk, Milk
2.In the Java I/O stream hierarchy, which decorator adds buffering to an InputStream?
- AFileInputStream
- BBufferedInputStream
- CDataInputStream
- DGZIPInputStream
3.A developer wants to add logging functionality to a function without altering its code. Which pattern should they use?
- ASingleton pattern
- BDecorator pattern
- CObserver pattern
- DFactory pattern
4.When stacking decorators, what determines the order of behavior application?
- AThe order of decorator class definitions
- BThe order in which decorators are wrapped
- CThe order of constructor parameters
- DThe order of method calls
5.Given a pipeline of function decorators using andThen, what happens if you reverse the order of decorators?
- AThe pipeline becomes invalid
- BThe final output remains the same
- CThe final output changes
- DThe pipeline executes faster
6.Which of the following is NOT a benefit of using the Decorator pattern?
- AReduces the need for subclassing
- BAllows dynamic behavior changes
- CEnforces a strict class hierarchy
- DSimplifies the code by reducing classes
7.How does the Decorator pattern differ from subclassing when extending behavior?
- AIt adds behavior at runtime
- BIt uses inheritance
- CIt requires less memory
- DIt modifies the original class
8.What is a primary advantage of using decorators in Java I/O streams?
- ASimpler code structure
- BGuaranteed thread safety
- CComposable features without class explosion
- DFaster execution than raw streams