Memento Pattern — Concept Check
Questions on Memento Pattern. Captures and externalizes an object's internal state so it can be restored later, without violating encapsulation.
Questions in this quiz
7 questions on Memento Pattern — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A game character takes damage and moves to a new location. What should the Caretaker do to ensure the game can be undone to the previous state?
- AInspect the Memento's internal state before saving
- BUse Java serialization to save the state
- CDirectly modify the Memento's fields to reflect changes
- DSave the current state using the Originator's save method
2.In the Memento pattern, what role does the Caretaker play?
- AIt creates and restores Mementos
- BIt exposes the internal state of the Originator
- CIt manages the history of Mementos without inspecting them
- DIt modifies the Memento's internal state
3.A developer claims the Caretaker can inspect Memento's fields to optimize memory usage. What is the flaw in this approach?
- AIt violates the encapsulation principle of the Memento pattern
- BIt allows the Originator to bypass the Caretaker
- CIt helps in reducing the complexity of the system
- DIt is correct and improves performance
4.After a game character gains a level, the Caretaker saves the state. What happens if the Caretaker restores this state later?
- AThe character's level and health are restored to the saved state
- BThe character's location is restored but not health
- CThe character's level is reset but health remains the same
- DOnly the character's name is restored
5.A developer uses Java serialization instead of Memento for saving states. What might be a drawback of this approach?
- ASerialization automatically manages the history of states
- BSerialization exposes internal state, violating encapsulation
- CSerialization is faster and more efficient
- DSerialization is the recommended approach for Memento pattern
6.A game character is saved and then takes damage. What is the Caretaker's role in undoing this damage?
- APrevent the Originator from taking damage
- BRestore the character using the last saved Memento
- CDirectly change the character's health
- DModify the Memento to reflect the character's new health
7.What is a key design constraint of the Memento pattern?
- AThe Memento should be visible to all components
- BThe Caretaker should modify Memento's fields
- CThe Originator must expose its internal state
- DThe Caretaker must treat the Memento as a black box