Dependency injection and inversion of control – Server Side interview prep

261 interview-style multiple-choice questions on Dependency injection and inversion of control, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 79 key terms with the flashcards first, then test yourself.

Take the practice test · Flashcards · All study tools

What it covers

  1. 15.1 Dependency injection and inversion of control basics
  2. 15.2 Injection styles and dependency design
  3. 15.3 The composition root and wiring
  4. 15.4 Lifetimes and scopes
  5. 15.5 Disposal, startup validation and container behavior
  6. 15.6 DI anti-patterns and code smells
  7. 15.7 Configuration, options and abstracting the environment
  8. 15.8 Testing with dependency injection
  9. 15.9 DI across frameworks and languages

Key terms

15.1 Dependency injection and inversion of control basics

Dependency injection
Supplying an object's dependencies from outside
Inversion of control
Framework controls creation and flow
Composition root
Where the object graph is assembled
Service locator
Global registry queried for dependencies
Hollywood principle
"Don't call us, we'll call you"
DI container
Builds object graphs from registrations
Pure DI
Hand wiring without a container
Seam
A place to change behavior without editing code
Volatile dependency
I/O, time or randomness
Temporal coupling
Hidden required call order

15.2 Injection styles and dependency design

Constructor over-injection
Too many constructor dependencies
Circular dependency
Two services that depend on each other
Leaky abstraction
Interface exposing implementation details
Ambient context
Static globally accessible dependency
Property injection
Setting optional dependencies via setters
Method injection
Passing a dependency per call
Role interface
Small interface for one client role
Keyed service
Registration chosen by key
Bastard injection
Constructor with a local default
Interception
Code wrapped around calls via proxies

15.3 The composition root and wiring

Assembly scanning
Registering types by convention
TryAdd
Register only if none exists
Open generic registration
One registration for all `T`
Factory registration
Lambda that builds a service
Conforming container
Library tied to one container
Source-generated container
Compile-time DI wiring

15.4 Lifetimes and scopes

Transient
New instance per resolution
Scoped
One instance per scope, often per request
Singleton lifetime
One instance for the container's life
Captive dependency
Long-lived service holding a short-lived one
Scope
A unit of work's lifetime
Scope validation
Catches scoped lifetime misuse
Prototype scope
Spring's new-instance-per-request
Scoped proxy
Proxy resolving the current scope's bean
DbContext pooling
Reusing reset contexts

15.5 Disposal, startup validation and container behavior

Disposal ownership
Whoever creates an object disposes it
GetRequiredService
Resolve or throw
ASP0000
Warning about building a second provider
Hosted service
Background service run by the host
ShutdownTimeout
How long the host waits on stop
Container verification
Checking all wiring up front

15.6 DI anti-patterns and code smells

Control freak
Creating volatile dependencies with `new`
Constrained construction
Forced constructor signatures
Static facade
Static API over the container
Speculative generality
Abstractions added just in case
Primitive dependency
Injecting bare strings or numbers

15.7 Configuration, options and abstracting the environment

Options pattern
Typed settings bound from configuration
IOptionsSnapshot
Options recomputed per scope
IOptionsMonitor
Singleton options with change notifications
ValidateOnStart
Validate options when the host starts
Named options
Several instances of one options type
TimeProvider
.NET 8 clock and timer abstraction
IConfigureOptions
Configurator class with injected deps
Feature management
Flags checked through an injected service
ConfigurationProperties
Spring typed configuration binding

15.8 Testing with dependency injection

Fake
Working lightweight implementation for tests
Mock
Test double that verifies interactions
Test double
Any stand-in for a real dependency in tests
ValidateOnBuild
Container option checking registrations at startup
Auto-mocking container
Fills all constructor params with mocks
Strict mock
Throws on calls without a setup
Sociable test
Unit test with real collaborators
Contract test suite
Same tests run against fake and real
FakeLogger
Test logger that records entries
MockitoBean
Spring test bean override with a mock
Fake HttpMessageHandler
Canned HTTP responses for HttpClient

15.9 DI across frameworks and languages

Qualifier
Annotation choosing among same-type beans
Primary bean
Default bean for a type
Component scanning
Finding annotated classes as beans
Auto-configuration back-off
Default beans skipped if you define one
CDI
Jakarta Contexts and Dependency Injection
Dagger
Compile-time DI for Java and Kotlin
Hilt
Dagger setup for Android
Koin
Kotlin DSL DI resolved at runtime
Multi provider
Angular token holding a list
dependency_overrides
FastAPI test hook for swapping deps
Wire
Go compile-time DI code generator
Fx
Go runtime DI and lifecycle framework
provide/inject
Vue ancestor-to-descendant values

Sample practice questions

  1. What is inversion of control (IoC)?

    1. Reversing the order in which a program's methods are called at runtime
    2. Letting end users take control of the server's configuration settings
    3. Running the program's code backwards, step by step, while debugging it
    4. A framework calls your code and supplies its dependencies, not the reverse

    Answer: D. A framework calls your code and supplies its dependencies, not the reverse DI is one form of IoC.

  2. What is "constructor over-injection" a symptom of?

    1. A bug inside the DI container itself
    2. A slow application startup sequence
    3. A class with too many responsibilities
    4. Too few interfaces defined in the app

    Answer: C. A class with too many responsibilities Consider splitting the class or introducing a facade.

  3. What is the composition root?

    1. The base class that every service in the application must inherit from
    2. The database's root user account, which owns every schema and table
    3. The one place near the entry point, like `Program.cs`, where the graph is wired
    4. The root folder of the repository, where the solution file normally lives

    Answer: C. The one place near the entry point, like `Program.cs`, where the graph is wired Only the composition root should know about the container.

  4. In .NET DI, what does a transient lifetime mean?

    1. One instance per thread that resolves it
    2. A new instance every time it's requested
    3. One instance per incoming HTTP request
    4. One shared instance for the whole application

    Answer: B. A new instance every time it's requested Use transient for lightweight, stateless services.

  5. Who disposes services that implement `IDisposable` in the .NET container?

    1. The caller, which must always call `Dispose` on every resolved service
    2. The container, when the scope (or root) that created them is disposed
    3. The garbage collector, which calls `Dispose` as soon as they're unused
    4. Nobody; services resolved from the container are never disposed at all

    Answer: B. The container, when the scope (or root) that created them is disposed Don't dispose injected services yourself.

  6. What is the Service Locator pattern, and why is it often called an anti-pattern?

    1. A pattern for discovering microservices; it's considered too insecure
    2. Classes fetch dependencies from a global registry, hiding what they need
    3. A logging pattern for tracing services; it's considered too verbose
    4. A DNS service for locating servers; it's considered too slow for apps

    Answer: B. Classes fetch dependencies from a global registry, hiding what they need Constructor injection makes the same needs visible in the signature.

  7. Why inject an `IClock` (or `TimeProvider`) instead of calling `DateTime.Now` directly?

    1. ASP.NET Core forbids calling `DateTime.Now` directly
    2. Injecting a clock makes the code run noticeably faster
    3. Tests can supply a fixed time, making logic deterministic
    4. `DateTime.Now` doesn't work inside registered services

    Answer: C. Tests can supply a fixed time, making logic deterministic .NET 8 added `TimeProvider` for exactly this.

  8. How does DI make unit testing easier?

    1. It generates the unit tests automatically
    2. It makes the test suite run faster on the CPU
    3. It removes the need for assertions in tests
    4. Tests can pass fakes or mocks, isolating the code under test

    Answer: D. Tests can pass fakes or mocks, isolating the code under test No database or network is needed for a unit test.

  9. Does a Spring bean with a single constructor need `@Autowired` on it?

    1. Yes; otherwise Spring falls back to field injection for all dependencies
    2. No; Spring ignores constructors entirely and only injects through setters
    3. Yes; without it Spring calls the constructor with null for every argument
    4. No; since Spring 4.3 a lone constructor is used for injection automatically

    Answer: D. No; since Spring 4.3 a lone constructor is used for injection automatically The annotation is needed only to choose among several constructors.

  10. Which injection style is generally preferred, and why?

    1. Service locator: it keeps constructors short by hiding dependencies away
    2. Method injection: every call gets fresh dependencies passed in explicitly
    3. Constructor injection: dependencies are explicit and objects fully built
    4. Property injection: it needs less code and lets dependencies be optional

    Answer: C. Constructor injection: dependencies are explicit and objects fully built Property injection suits truly optional dependencies.

Practice all 261 questions

More Server Side