Visitor Pattern — Concept Check
Questions on Visitor Pattern. Lets you add new operations to an object structure without modifying the classes, using double dispatch.
Questions in this quiz
8 questions on Visitor Pattern — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In a Visitor pattern, what happens when you add a new operation to an object structure?
- AYou add methods to all existing Visitors.
- BYou need to modify all existing element classes.
- CYou add a new Visitor class without modifying existing elements.
- DYou must rewrite the entire object structure.
2.Given an arithmetic expression tree, why might you choose the Visitor pattern to implement operations like evaluation and printing?
- AIt simplifies the structure by merging operations and elements.
- BIt allows adding new operations without changing the expression node classes.
- CIt makes the expression tree immutable.
- DIt reduces the number of classes needed.
3.A developer claims that adding a new element type in a Visitor pattern is straightforward. What is the reality?
- AOnly the new element needs to be implemented.
- BThe new element type automatically integrates with existing Visitors.
- CNo changes are needed in existing Visitors.
- DAll existing Visitors must be updated to handle the new element type.
4.What is the role of double dispatch in the Visitor pattern?
- AIt allows elements to be visited twice for accuracy.
- BIt ensures that only one method is executed per visit.
- CIt determines the method to execute based on both element and visitor types.
- DIt simplifies the visitor interface by reducing methods.
5.In the Visitor pattern, why might a developer mistakenly believe it violates the Open/Closed Principle for operations?
- AThey think adding new operations requires modifying all elements.
- BThey believe it limits the number of operations.
- CThey assume the pattern requires frequent structural changes.
- DThey confuse the need to update Visitors for new elements with operations.
6.In an expression tree using the Visitor pattern, what happens when you add a new Visitor for simplification?
- AThe expression nodes remain unchanged, and simplification logic is added.
- BThe tree structure must be altered to accommodate simplification.
- CAll expression nodes must be updated for simplification.
- DExisting Visitors need to be modified to support simplification.
7.Why might someone incorrectly think the Visitor pattern is unsuitable for frequently changing operations?
- AThey confuse operation changes with element changes.
- BThey believe it requires modifying elements for each change.
- CThey assume it makes the codebase less flexible.
- DThey think it complicates the addition of new operations.
8.What output would you expect if you use a PrintVisitor on the expression tree representing (3 + 4) * 2?
- A(3 + 4) * 2.0
- B(3 + 4) * 2
- C((3.0 + 4.0) * 2.0)
- D3.0 + 4.0 * 2.0