Dependency Inversion Principle (DIP)
High-level modules should not depend on low-level modules — both depend on abstractions. The foundation of testable, flexible architecture.
Questions in this quiz
10 questions on Dependency Inversion Principle (DIP). Work through them here, then start the quiz to answer them and see your score.
1.The Dependency Inversion Principle has two rules. Which answer states them correctly?
- AAll classes should be abstract. Never use concrete implementations.
- BUse dependency injection for all object creation in every class.
- CHigh-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.
- DHigh-level classes should control low-level classes directly for better performance.
2.Which version follows DIP?
// Version A class OrderService { private MySQLDatabase db = new MySQLDatabase(); public void save(Order o) { db.save(o); } } // Version B — with interface interface OrderRepository { void save(Order o); } class OrderService { private final OrderRepository repo; public OrderService(OrderRepository repo) { this.repo = repo; } public void save(Order o) { repo.save(o); } }- AVersion B — OrderService depends on an abstraction (OrderRepository), not a concrete class
- BVersion A — it is simpler and more direct
- CBoth follow DIP equally
- DNeither — DIP requires using a DI framework like Spring
3.What is the relationship between Dependency Inversion (DIP) and Dependency Injection (DI)?
- ADIP is the principle (depend on abstractions); DI is a technique that achieves it by providing dependencies from outside rather than creating them with new
- BDIP is about class-level design; DI only applies inside Spring/CDI frameworks
- CThey are the same thing
- DDependency Injection violates DIP because it uses concrete classes
4.Why does OrderService directly instantiating `new EmailService()` violate DIP?
- AEmailService is in a different package
- BEmailService should be a static utility class
- COrderService (high-level) has a hard dependency on a specific EmailService implementation — it can't be changed or mocked without modifying OrderService
- DThe new keyword is forbidden in SOLID-compliant code
5.Which injection type does Spring's @Autowired on a constructor enable?
- ASetter injection only
- BField injection only
- CConstructor injection — the dependencies are provided when the object is created, making them required and the class immutable
- DStatic injection via class-level configuration
6.In terms of DIP, who should define the abstraction (interface) — the high-level module or the low-level module?
- AThe low-level module — it knows what it can provide
- BThe framework (e.g., Spring) should auto-generate interfaces
- CNeither — a separate "interfaces" package should define all abstractions
- DThe high-level module — it defines what it needs; the low-level module implements that interface
7.Constructor injection vs field injection (@Autowired on a private field) — which is preferred and why?
- AConstructor injection — dependencies are explicit, the class can be immutable, and tests don't need Spring to inject them
- BField injection — less boilerplate code to write
- CBoth are equally good
- DSetter injection — most flexible of all three
8.A service directly calls a static utility method: PaymentUtils.processPayment(order). Does this violate DIP?
- AOnly if PaymentUtils is in a different package
- BNo — static methods don't create objects, so DIP doesn't apply
- CNo — static methods are always fine in DIP
- DYes — static method calls create a hard dependency that cannot be abstracted, mocked, or substituted
9.What is the main testability benefit of following DIP?
- ATests run faster because less code is compiled
- BDependencies can be replaced with mocks or stubs in tests without modifying the class under test
- CDIP makes integration tests easier to write
- DTests no longer need assertions
10.All five SOLID principles work together. Which pairing best describes how DIP builds on the others?
- ADIP enables SRP (separate concerns behind interfaces), OCP (extend by injecting new implementations), LSP (any compliant implementation can be injected), and ISP (inject only the interface you need)
- BDIP only pairs with OCP and nothing else
- CDIP is independent — it can be applied without considering the other principles
- DDIP contradicts ISP because interfaces make code more complex