Which statement best describes dependency injection?

Prepare for the Object-Oriented Programming Test with flashcards and multiple choice questions. Each question comes with hints and explanations to enhance understanding. Master OOP concepts and succeed in your exam!

Multiple Choice

Which statement best describes dependency injection?

Explanation:
Dependency injection is a pattern where an object's collaborators are supplied from outside, rather than created by the object itself. This reduces coupling because the class depends on abstractions (interfaces) rather than concrete implementations, so the same class can work with different dependencies without changing its code. It also makes testing easier, since you can inject mock or fake dependencies during tests to verify behavior without relying on real services. This approach best describes why it’s useful: dependencies provided from outside let you swap implementations, configuration, or test doubles without touching the dependent class. In contrast, having dependencies resolved at compile time and fixed means the class hard-codes specific implementations, which tightens coupling and harms flexibility and testability. Creating dependencies inside the class with new further increases coupling because the class decides exactly which collaborators to instantiate, making it harder to substitute them in tests or in different environments. And dependency injection is a broad pattern that applies to regular objects, not only to singletons.

Dependency injection is a pattern where an object's collaborators are supplied from outside, rather than created by the object itself. This reduces coupling because the class depends on abstractions (interfaces) rather than concrete implementations, so the same class can work with different dependencies without changing its code. It also makes testing easier, since you can inject mock or fake dependencies during tests to verify behavior without relying on real services.

This approach best describes why it’s useful: dependencies provided from outside let you swap implementations, configuration, or test doubles without touching the dependent class. In contrast, having dependencies resolved at compile time and fixed means the class hard-codes specific implementations, which tightens coupling and harms flexibility and testability. Creating dependencies inside the class with new further increases coupling because the class decides exactly which collaborators to instantiate, making it harder to substitute them in tests or in different environments. And dependency injection is a broad pattern that applies to regular objects, not only to singletons.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy