QuizzesComposite Pattern — Concept Check
intermediate
java

Composite Pattern — Concept Check

Questions on Composite Pattern. Composite lets clients treat individual objects and compositions of objects uniformly — perfect for tree structures like file systems and UI hierarchies.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Composite Pattern — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.A developer is implementing a file system using the Composite pattern. They create a `Directory` class to hold `File` objects. What method should they add to `Directory` to calculate total size?

    • AImplement `size()` to sum child sizes
    • BUse `getSize()` to fetch size directly
    • CAdd `calculateSize()` to loop through children
    • DOverride `name()` to include size
  2. 2.In a UI framework using the Composite pattern, what happens when a `Container` component's `paintComponent()` method is called?

    • AIt paints only leaf components
    • BOnly the container is painted
    • CChild components must be painted separately
    • DIt paints itself and all child components
  3. 3.A developer mistakenly believes that adding a new operation in the Composite pattern requires changes to both `Leaf` and `Composite` classes. What is the correct approach?

    • AImplement the operation only in `Leaf`
    • BImplement the operation only in `Composite`
    • CModify `Leaf` and `Composite` separately
    • DAdd the operation to the `Component` interface
  4. 4.In a file system using the Composite pattern, what happens if you call `print()` on a `Directory` object?

    • AIt prints only files, not subdirectories
    • BIt prints the directory and all its contents
    • COnly the directory name is printed
    • DSubdirectories must be printed separately
  5. 5.A developer uses the Composite pattern to build a menu system. They believe each menu item must be a `Composite`. What is a better approach?

    • AUse `Leaf` for menu items without submenus
    • BUse `Composite` only for top-level menus
    • CTreat all menu items as `Leaf`
    • DMake all menu items `Composite`
  6. 6.Which of the following is an example of a `Leaf` in the Composite pattern?

    • AA `Directory` with files
    • BA `JButton` in a Swing UI
    • CA `Container` with components
    • DA `JPanel` containing buttons
  7. 7.A developer claims that in the Composite pattern, a `Leaf` can contain other components. What is the correct understanding?

    • A`Leaf` and `Composite` are interchangeable
    • BA `Leaf` can contain components
    • CA `Leaf` cannot contain other components
    • DA `Leaf` must always be empty
  8. 8.In a Composite pattern implementation, what is the role of the `Component` interface?

    • AStores data for `Composite`
    • BImplements operations for `Leaf` only
    • CHolds children in a hierarchy
    • DDefines common operations for `Leaf` and `Composite`