OOP & SOLID Middle interview questions and answers

75 questions for a OOP & SOLID (Middle) interview - with correct answers, explanations and links to the docs.

  1. 1.Why is Square extends Rectangle with settable width and height a classic LSP violation?
  2. 2.According to LSP, how may a subclass change a method's preconditions and postconditions?
  3. 3.Which subclass change breaks LSP?
  4. 4.Which return type is allowed when overriding Animal create() in Java?
  5. 5.What is the history rule of LSP?
  6. 6.A ReadOnlyList extends ArrayList and throws from add(). What is the cleaner design?
  7. 7.What is the difference between a role interface and a header interface?
  8. 8.With the Dependency Inversion Principle, who should own the PaymentGateway interface?
  9. 9.Which is NOT a form of dependency injection?
  10. 10.Why is constructor injection usually preferred over setter injection?
  11. 11.What is a composition root?
  12. 12.A constructor takes 9 dependencies. What does this usually mean?
  13. 13.What does Inversion of Control mean in general?
  14. 14.Which code follows "Tell, Don't Ask"?
  15. 15.What does Command-Query Separation say?
  16. 16.Which GRASP principle says to give a responsibility to the class that has the needed data?
  17. 17.What is a Pure Fabrication in GRASP?
  18. 18.Which GRASP principle suggests that Order should create OrderLine objects?
  19. 19.What does Protected Variations in GRASP recommend?
  20. 20.What is the difference between an entity and a value object?
  21. 21.Why should value objects usually be immutable?
  22. 22.How should a method that "changes" an immutable Money look?
  23. 23.What goes wrong here in Java?
  24. 24.Why is equals() with instanceof tricky when a subclass adds a field?
  25. 25.What is the fragile base class problem?
  26. 26.What does this code print?
  27. 27.Why is calling an overridable method from a constructor risky?
  28. 28.How do Template Method and Strategy differ?
  29. 29.What is delegation in OOP?
  30. 30.Why is class Stack extends Array a poor design?
  31. 31.What is the Refused Bequest smell?
  32. 32.Logic that uses Order data lives in OrderPrinter, which calls six getters of Order. Which smell is this?
  33. 33.Adding one field to a customer requires edits in 12 classes. Which smell is this?
  34. 34.What is the Middle Man smell?
  35. 35.What is an anemic domain model?
  36. 36.What is a Data Class smell?
  37. 37.What is the Null Object pattern?
  38. 38.A class has fields street, city, zip repeated in Customer, Warehouse and Invoice. What is the fix?
  39. 39.What is a mixin?
  40. 40.What problem do Java default methods in interfaces solve?
  41. 41.A Java class implements two interfaces that both have a default hello() method. What happens?
  42. 42.What is the diamond problem?
  43. 43.What does this Python code print?
  44. 44.How does a Python Protocol differ from an ABC?
  45. 45.What does @dataclass(frozen=True) give a Python class?
  46. 46.Why might a class have a private constructor and a static create() method?
  47. 47.When does a discriminated union fit better than a class hierarchy?
  48. 48.What does the Liskov Substitution Principle say about exceptions?
  49. 49.What does "program to an interface, not an implementation" mean?
  50. 50.Which version better follows OCP for adding new discount types?
  51. 51.Is it always worth making code open for extension in advance?
  52. 52.Two methods of a payroll system change for different stakeholders: calculatePay for accounting and reportHours for HR. Both live in Employee. Why is that a risk?
  53. 53.What does "depend on abstractions" NOT mean?
  54. 54.What is the difference between a stub and a mock in tests?
  55. 55.Why do tests that mock every collaborator often become fragile?
  56. 56.What is a sealed class or interface in Java and Kotlin?
  57. 57.What is a trait, compared to a mixin?
  58. 58.What does Kotlin's class Repo(db: Db) : Db by db do?
  59. 59.A Report class has generate(), formatAsPdf(), formatAsHtml() and sendByEmail(). What is a good first split?
  60. 60.What is the benefit of making a class final (or sealed in C#) by default?
  61. 61.What is the Law of Demeter often summarized as?
  62. 62.Why isn't list.stream().filter(x).map(y).toList() a Law of Demeter violation?
  63. 63.Which is a good reason to introduce an interface with a single implementation?
  64. 64.What is cohesion measured by, informally?
  65. 65.Which type of coupling is considered the worst?
  66. 66.What does Object.freeze(this) at the end of a JavaScript constructor achieve?
  67. 67.What is defensive copying?
  68. 68.Which statement about abstract classes in TypeScript is true?
  69. 69.Why does instanceof not work with TypeScript interfaces?
  70. 70.How does Python's super() behave with multiple inheritance?
  71. 71.Why is it good practice for a value object to validate itself in the constructor?
  72. 72.What is a class invariant?
  73. 73.A team adds isAdmin, isGuest and isBanned checks with if across 40 places. Which refactoring is usually suggested?
  74. 74.What is the Divergent Change smell?
  75. 75.What does protected on a constructor in TypeScript allow?