Member-only story
Java Design Patterns Practice Test & Interview Questions
13 min readNov 29, 2024
Q#1. In a chat application, you want users to receive updates about messages in a chat room only when they are subscribed to that room. The system should also manage the communication between different chat rooms. Which combination of patterns is suitable? (Scenario-based, Multi-select)
- A) Observer Pattern
- B) Adapter Pattern
- C) Chain of Responsibility Pattern
- D) Mediator Pattern
Answer: A) Observer Pattern and D) Mediator Pattern
Explanation:
- A) Observer Pattern: Correct. The Observer pattern suits the need for notifying subscribers of updates in a chat room.
- B) Adapter Pattern: Incorrect. Given scenario doesn’t satisfy the features of Adapter pattern.
- C) Chain of Responsibility Pattern: Incorrect. Given scenario doesn’t satisfy the features of the Chain of Responsibility pattern.
- D) Mediator Pattern: Correct. Mediator pattern facilitates coordinated communication among different chat rooms.
Q#2. What technique should be used in a Singleton class to prevent multiple instances from being created when the object is serialized and then deserialized? (Concept-based, Single-select)