Design patterns interview questions
Creational, structural and behavioral patterns in Java.
98 questions with answers and explanations · 96 flashcards
Key ideas
- Creational: Factory Method, Builder, Singleton. Structural: Adapter, Decorator, Proxy. Behavioral: Strategy, Observer, Template Method.
- Java I/O is the textbook Decorator:
new BufferedInputStream(new FileInputStream(f)). - Strategy swaps an algorithm behind an interface; with lambdas it's often a functional interface.
- Singletons need care with threads and serialization. An
enumsingleton handles both.
Questions (page 1 of 2)
- new BufferedInputStream(new FileInputStream(file)) adds buffering to a stream by wrapping it. Which pattern is this?Multiple choiceMedium
- Classify each design pattern.Matrix / gridMedium
- Explain the Strategy pattern with a concrete Java example. When would you use it?Interview answerMedium
- A code review describes a ConfigManager singleton. Highlight the statements that describe real problems.Highlight textHard
- Your checkout flow wants one clean charge(order) call, but each payment provider ships a different SDK. Which design choice best protects the checkout core…Multiple choiceMedium
- Which principle is most closely aligned with the Factory Pattern?Multiple choiceMedium
- Which scenario is least suited for the Decorator pattern?Multiple choiceMedium
- Why is it important for a decorator to share the same interface as the object it decorates?Multiple choiceEasy
- In an e-commerce application, you want to delay loading product images until they are actually needed. Which design pattern is most suitable for this scenario?Multiple choiceEasy
- You are designing a system with multiple types of notifications (email, SMS, push). How can the Factory Pattern help in managing these types?Multiple choiceHard
- Which situation is the clearest example of over-engineering with the Strategy Pattern?Multiple choiceHard
- Consider the following code snippet. What is the best design pattern to refactor it?Multiple choiceMedium
- How would you explain a key advantage of using decorators over creating subclasses for each new behavior in an interview?Multiple choiceEasy
- How would you explain a typical sign that the Decorator pattern might be useful in an interview?Multiple choiceEasy
- Which of the following best describes the role of the component interface in the Decorator pattern?Multiple choiceEasy
- In implementing a Decorator, why is it crucial to maintain the same interface as the original object?Multiple choiceHard
- You have a legacy XML-based system that needs to integrate with a new JSON-based service. Which design pattern should you use?Multiple choiceHard
- Which design pattern allows adding responsibilities to objects dynamically at runtime?Multiple choiceEasy
- When using the Decorator pattern, how can you ensure that new functionality does not break existing behavior?Multiple choiceHard
- Which signal most strongly shows that a design pattern is actually justified in a checkout codebase?Multiple choiceEasy
- In a payment processing system, you notice that adding a new payment processor requires modifying multiple classes. Which design pattern might help refactor…Multiple choiceHard
- How can you optimize the performance impact when using multiple decorators in a Java application?Multiple choiceMedium
- You are tasked with implementing a system for creating different types of shipping methods in an e-commerce application. What is the primary benefit of using…Multiple choiceMedium
- Which aspect of the Factory Pattern helps in reducing tight coupling between classes?Multiple choiceEasy
- How would you explain a common architectural smell that suggests the need for the Factory Pattern in an interview?Multiple choiceMedium
- Which structural element does a Decorator class typically contains?Multiple choiceEasy
- Which of the following best describes the role of a Factory Method in the Factory Pattern?Multiple choiceEasy
- In the context of an e-commerce application, you want to add both gift wrapping and seasonal discounts to the base price of an item without altering the…Multiple choiceEasy
- How would you explain a common mistake when implementing the Factory Pattern in a Java interview?Multiple choiceMedium
- When is the Adapter pattern most useful in integrating two systems?Multiple choiceEasy
- How does the Decorator pattern help align with the Open/Closed Principle?Multiple choiceMedium
- Which design pattern should you choose when you need to act as an intermediary to control access to an object?Multiple choiceEasy
- Which of the following is NOT a characteristic of the Decorator pattern?Multiple choiceMedium
- How does the Factory Pattern enhance flexibility in an application?Multiple choiceEasy
- How does the Factory Pattern align with the Dependency Inversion Principle?Multiple choiceMedium
- How would you explain a potential disadvantage of using the Factory Pattern in a system with a small number of product types in an interview?Multiple choiceMedium
- In a checkout system, you need to apply multiple discounts. Which pattern helps avoid creating numerous subclasses for each combination of discounts?Multiple choiceMedium
- Which is a key benefit of using the Factory Pattern in software design?Multiple choiceEasy
- What testing benefit do you gain by putting provider selection in a factory and provider quirks in adapters?Multiple choiceMedium
- In which scenario might the Factory Pattern be more suitable than the Builder Pattern?Multiple choiceMedium
- In the context of the Factory Pattern, what is a 'concrete product'?Multiple choiceEasy
- Which of the following statements regarding the Factory Pattern and Spring's Inversion of Control (IoC) is true?Multiple choiceHard
- How might over-engineering manifest when using the Factory Pattern in a Java application?Multiple choiceHard
- Which pattern would you choose to apply different pricing strategies without altering the client code?Multiple choiceMedium
- In an e-commerce application, you need to implement a payment processor that can handle different payment methods. Which design pattern is most suitable for…Multiple choiceMedium
- While implementing the Decorator pattern, a developer accidentally introduces a cyclic dependency between decorators. What could be a potential consequence?Multiple choiceHard
- In a checkout flow, a legacy system provides order details in XML, but the new system uses JSON. What role does an Adapter play here?Multiple choiceHard
- How can you avoid the runtime performance overhead when applying multiple decorators?Multiple choiceHard
- What principle does the Decorator pattern adhere to by allowing the dynamic addition of responsibilities to objects?Multiple choiceMedium
- Which design pattern is primarily used to delay operations that are resource-intensive?Multiple choiceEasy