Skip to content
Sharp

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 enum singleton handles both.

Questions (page 1 of 2)

  1. new BufferedInputStream(new FileInputStream(file)) adds buffering to a stream by wrapping it. Which pattern is this?Multiple choiceMedium
  2. Classify each design pattern.Matrix / gridMedium
  3. Explain the Strategy pattern with a concrete Java example. When would you use it?Interview answerMedium
  4. A code review describes a ConfigManager singleton. Highlight the statements that describe real problems.Highlight textHard
  5. 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
  6. Which principle is most closely aligned with the Factory Pattern?Multiple choiceMedium
  7. Which scenario is least suited for the Decorator pattern?Multiple choiceMedium
  8. Why is it important for a decorator to share the same interface as the object it decorates?Multiple choiceEasy
  9. 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
  10. 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
  11. Which situation is the clearest example of over-engineering with the Strategy Pattern?Multiple choiceHard
  12. Consider the following code snippet. What is the best design pattern to refactor it?Multiple choiceMedium
  13. How would you explain a key advantage of using decorators over creating subclasses for each new behavior in an interview?Multiple choiceEasy
  14. How would you explain a typical sign that the Decorator pattern might be useful in an interview?Multiple choiceEasy
  15. Which of the following best describes the role of the component interface in the Decorator pattern?Multiple choiceEasy
  16. In implementing a Decorator, why is it crucial to maintain the same interface as the original object?Multiple choiceHard
  17. 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
  18. Which design pattern allows adding responsibilities to objects dynamically at runtime?Multiple choiceEasy
  19. When using the Decorator pattern, how can you ensure that new functionality does not break existing behavior?Multiple choiceHard
  20. Which signal most strongly shows that a design pattern is actually justified in a checkout codebase?Multiple choiceEasy
  21. 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
  22. How can you optimize the performance impact when using multiple decorators in a Java application?Multiple choiceMedium
  23. 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
  24. Which aspect of the Factory Pattern helps in reducing tight coupling between classes?Multiple choiceEasy
  25. How would you explain a common architectural smell that suggests the need for the Factory Pattern in an interview?Multiple choiceMedium
  26. Which structural element does a Decorator class typically contains?Multiple choiceEasy
  27. Which of the following best describes the role of a Factory Method in the Factory Pattern?Multiple choiceEasy
  28. 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
  29. How would you explain a common mistake when implementing the Factory Pattern in a Java interview?Multiple choiceMedium
  30. When is the Adapter pattern most useful in integrating two systems?Multiple choiceEasy
  31. How does the Decorator pattern help align with the Open/Closed Principle?Multiple choiceMedium
  32. Which design pattern should you choose when you need to act as an intermediary to control access to an object?Multiple choiceEasy
  33. Which of the following is NOT a characteristic of the Decorator pattern?Multiple choiceMedium
  34. How does the Factory Pattern enhance flexibility in an application?Multiple choiceEasy
  35. How does the Factory Pattern align with the Dependency Inversion Principle?Multiple choiceMedium
  36. 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
  37. In a checkout system, you need to apply multiple discounts. Which pattern helps avoid creating numerous subclasses for each combination of discounts?Multiple choiceMedium
  38. Which is a key benefit of using the Factory Pattern in software design?Multiple choiceEasy
  39. What testing benefit do you gain by putting provider selection in a factory and provider quirks in adapters?Multiple choiceMedium
  40. In which scenario might the Factory Pattern be more suitable than the Builder Pattern?Multiple choiceMedium
  41. In the context of the Factory Pattern, what is a 'concrete product'?Multiple choiceEasy
  42. Which of the following statements regarding the Factory Pattern and Spring's Inversion of Control (IoC) is true?Multiple choiceHard
  43. How might over-engineering manifest when using the Factory Pattern in a Java application?Multiple choiceHard
  44. Which pattern would you choose to apply different pricing strategies without altering the client code?Multiple choiceMedium
  45. 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
  46. While implementing the Decorator pattern, a developer accidentally introduces a cyclic dependency between decorators. What could be a potential consequence?Multiple choiceHard
  47. 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
  48. How can you avoid the runtime performance overhead when applying multiple decorators?Multiple choiceHard
  49. What principle does the Decorator pattern adhere to by allowing the dynamic addition of responsibilities to objects?Multiple choiceMedium
  50. Which design pattern is primarily used to delay operations that are resource-intensive?Multiple choiceEasy