Sample questions
Question 1Easy
Which SOLID principle says software entities should be open for extension but closed for modification?
- Liskov Substitution Principle
- Open/Closed Principle
- Dependency Inversion Principle
- Interface Segregation Principle
Answer: B. Open/Closed Principle
Explanation
New behaviour should be added with new code (e.g. a new subclass or strategy) rather than editing tested existing code.
Step-by-step solution
- Open/Closed Principle means entities can be extended but not modified.
- New features are added via inheritance or composition.
- Existing code remains untouched for changes.
- This ensures stability and reusability of code.
- Final answer: Open/Closed Principle.
Why the other options are wrong
- A. Liskov says subtypes must be substitutable for their base type; it is not about extending without changing code.
- C. Dependency Inversion says high-level code should depend on abstractions; it is not about extending without modifying code.
- D. Interface Segregation says clients should not depend on methods they do not use; it is not about extension.
Tip: Remember: 'Open for extension, closed for modification' is the key phrase.
Question 2Easy
What does the 'D' in SOLID stand for?
- Dependency Inversion Principle
- Don't Repeat Yourself
- Data Encapsulation Principle
- Dynamic Dispatch Principle
Answer: A. Dependency Inversion Principle
Explanation
The five principles are Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation and Dependency Inversion.
Step-by-step solution
- SOLID is an acronym for five OOP principles.
- The 'D' stands for Dependency Inversion Principle.
- Dependency Inversion Principle is one of the five SOLID principles.
- Correct answer matches the definition of 'D'.
- Final answer: Dependency Inversion Principle.
Why the other options are wrong
- B. Don't Repeat Yourself is the DRY principle, a separate idea that is not one of the five SOLID principles.
- C. Data Encapsulation Principle is not a SOLID principle; encapsulation is a general OOP concept, not the D.
- D. Dynamic Dispatch is a language mechanism for choosing methods at runtime, not one of the five SOLID principles.
Question 3Easy
The Interface Segregation Principle recommends:
- Many small, client-specific interfaces over one large general one
- One interface per package
- Interfaces with as many methods as possible
- Always using abstract classes instead of interfaces
Answer: A. Many small, client-specific interfaces over one large general one
Explanation
Clients should not be forced to depend on methods they don't use.
Step-by-step solution
- Interface Segregation Principle says clients should only have needed methods.
- So, one big interface with many methods is bad.
- Instead, make many small interfaces for specific needs.
- This avoids forcing clients to use unused methods.
- Final answer: Many small, client-specific interfaces over one large general one.
Why the other options are wrong
- B. Packages do not group interfaces by principle; ISP is about the size and fit of each interface.
- C. Bigger interfaces with many methods are what ISP warns against, so this is the opposite.
- D. ISP does not say to prefer abstract classes; it favours small interfaces that clients need.
Tip: Use small, targeted interfaces to improve code flexibility and reduce dependencies.
Question 4Easy
A ReportGenerator receives a Formatter interface in its constructor, so PDF or CSV formatters can be passed in. Which SOLID principle does this mainly apply?
- Interface Segregation
- Liskov Substitution
- Dependency Inversion
- Single Responsibility
Answer: C. Dependency Inversion
Explanation
The high-level class depends on an abstraction (Formatter) that the concrete formatters implement, rather than on a concrete class.
Step-by-step solution
- ReportGenerator depends on the Formatter interface, not on a specific PDF or CSV class.
- Depending on an abstraction and receiving the detail from outside is Dependency Inversion.
- So the answer is option C, Dependency Inversion.
Why the other options are wrong
- A. Interface Segregation is about keeping interfaces small, not about injecting a dependency.
- B. Liskov Substitution is about subclasses replacing their base class safely.
- D. Single Responsibility is about a class having one reason to change.
Tip: If a class depends on an interface passed in, think Dependency Inversion.
Question 5Medium
An OrderService creates new MySqlOrderRepository() inside its constructor. Which principle does this go against?
- Interface Segregation Principle
- Dependency Inversion Principle
- Single Responsibility Principle
- Liskov Substitution Principle
Answer: B. Dependency Inversion Principle
Explanation
The high-level service depends on a concrete low-level class; DIP says both should depend on an abstraction such as an injected OrderRepository interface.
Step-by-step solution
- OrderService creates MySqlOrderRepository directly in constructor.
- This makes service dependent on a concrete class, not an abstraction.
- Dependency Inversion Principle requires both depend on abstractions, not concrete implementations.
- Creating concrete class inside constructor violates DIP rule.
- Correct answer is Dependency Inversion Principle.
Why the other options are wrong
- A. Interface Segregation is about small client-specific interfaces, not creating a dependency.
- C. Single responsibility is about one reason to change; here the issue is hard-wiring a dependency.
- D. Liskov is about substituting subtypes, and no subtype is involved here.
Tip: Always inject dependencies through interfaces, not concrete classes.
Question 6Medium
A Shape interface declares area() and a new shape type is added by writing a new class, without touching existing classes. Which principle does this satisfy?
- Dependency Inversion Principle
- Liskov Substitution Principle
- Open/Closed Principle
- Interface Segregation Principle
Answer: C. Open/Closed Principle
Explanation
Behaviour is extended by adding code (a new implementation) rather than modifying existing code.
Step-by-step solution
- A new shape added as a new class, with no edits to old classes, is extension.
- That matches the Open/Closed Principle: open to extension, closed to change.
- So the correct answer is option C.
Why the other options are wrong
- A. DIP is about depending on abstractions, not about adding classes.
- B. LSP is about safe subtypes, not about adding new classes.
- D. ISP is about keeping interfaces small.
Tip: New behaviour through new classes = OCP.
Question 7Medium
A subclass Penguin of Bird throws UnsupportedOperationException from fly(), breaking code that calls fly() on every Bird. Which principle is violated?
- Open/Closed Principle
- Single Responsibility Principle
- Dependency Inversion Principle
- Liskov Substitution Principle
Answer: D. Liskov Substitution Principle
Explanation
Subtypes must be usable wherever the base type is expected without surprising the client; Penguin can't honour Bird's contract.
Step-by-step solution
- Penguin is a subclass of Bird, so it should follow Bird's method contract.
- Bird's fly() method is expected to work for all Birds, including Penguins.
- Penguin throws UnsupportedOperationException in fly(), breaking this expectation.
- This violates Liskov Substitution Principle as Penguin cannot replace Bird safely.
- Liskov Substitution Principle ensures subtypes can be used wherever base types are expected.
- Final answer: Liskov Substitution Principle.
Why the other options are wrong
- A. Open/Closed is about extending without modifying code, not about a subclass breaking a contract.
- B. Single responsibility is about a class having one reason to change, not about contracts.
- C. Dependency inversion is about depending on abstractions, not a subtype breaking a parent's method.
Tip: Always ensure subclasses follow the same method contracts as their superclasses.
Question 8Medium
An Invoice class calculates totals, saves itself to a database and emails itself to customers. Which principle does it violate most clearly?
- Liskov Substitution Principle
- Interface Segregation Principle
- Open/Closed Principle
- Single Responsibility Principle
Answer: D. Single Responsibility Principle
Explanation
It has three unrelated reasons to change (pricing rules, storage, email), so the responsibilities should be split.
Step-by-step solution
- Invoice currently does three unrelated things: calculate totals (business rule), save itself to a database (persistence), and email itself (notification).
- Each of those is an independent reason the class could need to change - a pricing-rule change, a storage change, or an email-template change.
- Mixing independent reasons to change into one class is exactly what the Single Responsibility Principle warns against.
Why the other options are wrong
- A. Liskov Substitution is about whether a subclass can safely replace its superclass, unrelated to one class doing too many unrelated jobs.
- B. Interface Segregation is about interfaces forcing clients to implement methods they don't need - a different problem from one class having too many responsibilities.
- C. Open/Closed is about extending behaviour without modifying existing code - a different axis from how many unrelated responsibilities a class holds.
Tip: Count the independent reasons a class could need to change for unrelated business reasons - more than one usually means it should be split.
Question 9Hard
Which change to an overriding method respects the Liskov Substitution Principle?
- Requiring stricter preconditions than the base method
- Throwing new checked exceptions the base never declared
- Accepting a wider range of inputs than the base method required
- Weakening the guarantees of the result
Answer: C. Accepting a wider range of inputs than the base method required
Explanation
Subtypes may weaken preconditions and strengthen postconditions, but not the reverse, or callers written for the base type break.
Step-by-step solution
- LSP says a subclass must be usable anywhere the base class is used.
- An override may accept a wider range of inputs than the base method.
- So accepting wider inputs keeps substitution safe; the correct answer is option C.
Why the other options are wrong
- A. Stricter preconditions break substitution.
- B. New checked exceptions break callers that expect the base contract.
- D. Weaker guarantees on the result break substitution.
Tip: Subclasses may relax input rules and tighten output promises.
Question 10Hard
The Dependency Inversion Principle states that:
- Every class should depend on as many classes as possible
- Low-level modules should create the high-level modules
- High-level modules should depend on abstractions
- Dependencies should be resolved at compile time only
Answer: C. High-level modules should depend on abstractions
Explanation
For example, an OrderService depends on a PaymentGateway interface, and the concrete gateway is injected. This decouples policy from details.
Step-by-step solution
- Normally, high-level policy code would depend directly on low-level implementation details.
- DIP instead has BOTH the high-level and low-level code depend on a shared abstraction (an interface) owned at the boundary between them.
- The concrete low-level implementation is then supplied from outside, often via dependency injection, so it can be swapped freely.
Why the other options are wrong
- A. Depending on 'as many classes as possible' is the opposite of good design and has nothing to do with what DIP actually recommends.
- B. DIP says the reverse of this - high-level modules should not be controlled or instantiated by low-level ones.
- D. DIP is about WHAT you depend on (an abstraction vs. a concrete class), not about WHEN dependencies get resolved (compile time vs. runtime).
Tip: High-level code should only know an interface exists, never the concrete class behind it.
Practise all 21 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →