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