SOLID Principles Interview Questions with Examples
Last updated 2026-11-19
SOLID questions in interviews come in two forms: define the principle, and spot the violation in a code snippet. A definition earns little unless you can show the smell and the fix, so each answer below pairs the principle with a short before-and-after in Java.
The principles build on OOPs interview questions and especially inheritance and polymorphism. They are applied in design patterns and in low-level design rounds; the round plan is in how to crack a low-level design interview.
| Principles | SRP, OCP, LSP, ISP, DIP |
|---|---|
| Code in | Java |
| Typical task | Spot the violation and refactor |
How to spot violations quickly
- SRP: a class description needs the word "and", or it changes for unrelated reasons.
- OCP: adding a feature means editing a growing if-else or switch on type.
- LSP: a subclass throws UnsupportedOperationException or ignores part of the parent contract.
- ISP: implementers leave methods empty because the interface forces them.
- DIP: a high-level class creates concrete collaborators with new, so it cannot be tested alone.
A balanced view
SOLID is a set of heuristics, not laws. Applying every principle to a 200-line script produces interfaces with one implementation and indirection with no benefit. Say that in an interview: you apply a principle when you feel the pain it removes, such as a class that keeps breaking or code you cannot unit test.
SOLID Principles Interview Questions with Examples: questions with worked answers
1 What does SOLID stand for?
easySingle Responsibility: a class should have one reason to change. Open/Closed: open for extension, closed for modification. Liskov Substitution: subtypes must be usable wherever their base type is expected. Interface Segregation: clients should not depend on methods they do not use. Dependency Inversion: high-level modules depend on abstractions, not on concrete low-level modules. The aim is code that is easier to change, test and reason about.
2 Explain the Single Responsibility Principle with a violation and fix.
mediumA class has a single responsibility if it has one reason to change, meaning one actor whose requirements drive it. Here an Invoice class calculates totals, formats a PDF and saves to the database, so a tax rule change, a layout change and a schema change all edit the same class. Split it into InvoiceCalculator, InvoicePrinter and InvoiceRepository, each changing for one reason. Do not split so far that you have a class per method; the test is the reason to change.
javaclass Invoice { double total() { /* tax rules */ return 0; } } class InvoicePrinter { void print(Invoice i) { /* layout */ } } interface InvoiceRepository { void save(Invoice i); }3 Explain the Open/Closed Principle by refactoring a discount calculator.
mediumThe violation is a method with an if-else on customer type: adding a new type means editing and retesting the method. Refactor to a Discount interface with one class per rule, so new rules are added as new classes and the calculator never changes. Java lambdas or enums with behaviour achieve the same. The caveat is that you cannot predict every extension, so introduce the abstraction when the second or third variant appears.
java// Violation double discount(String type, double amt) { if (type.equals("STUDENT")) return amt * 0.10; else if (type.equals("SENIOR")) return amt * 0.15; return 0; } // Open/closed version interface Discount { double apply(double amt); } class StudentDiscount implements Discount { public double apply(double a) { return a * 0.10; } } class SeniorDiscount implements Discount { public double apply(double a) { return a * 0.15; } } double total(double amt, Discount d) { return amt - d.apply(amt); }4 Show a Liskov Substitution violation with Bird and Penguin and fix it.
hardIf Bird declares fly() and Penguin extends Bird, Penguin must throw or do nothing, so code that loops over birds calling fly() breaks when a Penguin is in the list. The subclass cannot honour the parent's promise. Fix by modelling capabilities: Bird has eat(), and a separate Flyable interface adds fly(), implemented by Sparrow but not Penguin. Callers that need flying accept Flyable. Other LSP smells are strengthening preconditions, weakening postconditions and overrides that throw unexpected exceptions.
javaabstract class Bird { abstract void eat(); } interface Flyable { void fly(); } class Sparrow extends Bird implements Flyable { void eat() { } public void fly() { } } class Penguin extends Bird { void eat() { } } // no fly(), nothing to violate5 Explain the Interface Segregation Principle with the printer example.
mediumA single Machine interface with print(), scan() and fax() forces a basic printer to implement scan and fax with empty or throwing bodies. Those clients depend on methods they do not use, and a change to fax() recompiles them. Split into Printer, Scanner and Fax interfaces and let a multifunction device implement all three. Clients then depend only on what they call, and implementers have nothing to stub.
javainterface Printer { void print(String doc); } interface Scanner { String scan(); } class BasicPrinter implements Printer { public void print(String d) { } } class AllInOne implements Printer, Scanner { public void print(String d) { } public String scan() { return ""; } }6 Explain the Dependency Inversion Principle. How is it different from dependency injection?
mediumDIP says high-level policy should depend on an abstraction, and low-level details should implement that abstraction, so source dependencies point at interfaces. If OrderService constructs a MySqlOrderRepository with new, it is welded to MySQL and cannot be unit tested without a database. Dependency injection is a technique that supports DIP: the collaborator is passed in, through the constructor, by a framework or by hand. You can inject a concrete class without inverting anything, and you can invert without a framework.
javainterface OrderRepository { void save(Order o); } class OrderService { private final OrderRepository repo; OrderService(OrderRepository repo) { this.repo = repo; } // injected abstraction void place(Order o) { repo.save(o); } }7 Identify the SOLID violation: a method switches on shape type to compute area.
mediumIt violates Open/Closed, because each new shape means editing the switch, and often Single Responsibility too if the same class draws and calculates. The fix is polymorphism: a Shape interface with area(), and Circle and Rectangle implementing it. The total is then the sum of shape.area() over a list, and a new Triangle class requires no edit to existing code.
javainterface Shape { double area(); } record Circle(double r) implements Shape { public double area() { return Math.PI * r * r; } } record Rect(double w, double h) implements Shape { public double area() { return w * h; } } double totalArea(List<Shape> shapes) { return shapes.stream().mapToDouble(Shape::area).sum(); }8 How does SOLID make code easier to test?
mediumDIP lets you replace a database, clock or payment client with a fake, so a unit test runs in memory. SRP keeps each class small, so a test sets up less. ISP means test doubles implement a few methods, not a dozen. LSP means a fake that honours the interface behaves like the real thing. Code that violates them usually needs heavy mocking or integration tests for what should be simple logic.
9 What is the difference between SRP and high cohesion?
mediumThey describe nearly the same quality from two angles. Cohesion measures how strongly a class's members belong together. SRP gives a test to achieve it: group things that change for the same reason. A class can be cohesive about utility functions and still violate SRP if two different teams request changes to it for different reasons. SRP pays more attention to who asks for change, cohesion to the internal relatedness.
10 Where can SOLID go wrong? Give examples of over-engineering.
hardInterfaces with a single implementation and no test double, factories that create one class, and a class per tiny step make code harder to follow without any benefit. OCP applied to every possible axis of change adds abstractions for variations that never come. Balance it with YAGNI and KISS: wait for the second use before abstracting, keep the first version simple, and refactor when change actually hurts.
11 Does the Open/Closed Principle forbid editing existing code?
mediumNo. Fixing a bug or changing a requirement means editing code. The principle says that new behaviour of a known kind should be addable by writing new code, not by modifying and risking tested code. It is realised through abstraction points where variation is expected, such as strategies, plug-ins or configuration, not by never touching a file.
12 Refactor a Notification class that sends email, SMS and push with if-else to follow SOLID.
hardDefine a Channel interface with send(Message). Implement EmailChannel, SmsChannel and PushChannel, each holding its own client configuration. NotificationService receives a list or map of channels in its constructor and loops over the channels enabled for the user, so DIP and OCP are satisfied: adding WhatsApp is a new class plus one registration line. SRP shows up because formatting a message, choosing channels and delivering are separate concerns. This is the strategy pattern, covered in design patterns questions.
javainterface Channel { void send(String to, String body); } class NotificationService { private final List<Channel> channels; NotificationService(List<Channel> channels) { this.channels = channels; } void notifyUser(String to, String body) { for (Channel c : channels) c.send(to, body); } }13 What other principles do interviewers pair with SOLID?
mediumDRY: avoid duplicating knowledge. KISS: prefer the simplest design that works. YAGNI: do not build features before they are needed. The Law of Demeter: talk to your immediate collaborators, not to a chain like a.getB().getC().doIt(). Composition over inheritance. Knowing the tension among them, for example DRY versus accidental duplication, shows maturity.
Frequently asked questions
Which SOLID principle is hardest to explain?
Liskov substitution, because it is about behavioural contracts rather than syntax. Use the Square and Rectangle or Bird and Penguin example.
Is SOLID only for Java?
No. It applies to any object-oriented or modular design, and the ideas carry over to Python, C#, TypeScript and C++.
Do I need a framework for dependency injection?
No. Passing dependencies through a constructor is enough. Frameworks like Spring automate the wiring.
Are SOLID principles asked in fresher interviews?
Often at least the five names and one example. Experienced rounds ask you to refactor code.
Related guides
More in CS Fundamentals Interview Questions
- System Design Interview Questions for Freshers
- System Design Interview Questions with Worked Designs
- TCP vs UDP Interview Questions with Socket Code and Numericals
- TCP/IP Interview Questions: Handshake, Congestion Control and Numericals
- Transactions and ACID Interview Questions
- What Happens When You Type a URL in the Browser: The Complete Answer
- Abstraction and Encapsulation Interview Questions
- Cache Memory and Computer Organization Interview Questions with Numericals
- Caching Interview Questions and Answers
- Cloud Computing Basics Interview Questions with Answers
- Computer Architecture Interview Questions with Numericals
- Computer Networks Interview Questions with Worked Answers and Numericals