DI & IoC Questions with Answers

20 practice questions on DI & IoC (Spring Boot & JPA) with answers and step-by-step solutions. Try 10 samples below, then practise all 20 free in the AptiNest app.

Sample questions

Question 1Easy

Which is a benefit of dependency injection for unit testing?

  1. It disables logging
  2. It removes the need for test data
  3. Tests no longer need assertions
  4. Mocks can be passed in easily

Answer: D. Mocks can be passed in easily

Explanation

Since collaborators come from outside, a test can supply fakes or mocks through the constructor.

Step-by-step solution

  1. With DI, a class receives its collaborators from outside.
  2. A test can pass in a mock or fake through the constructor.
  3. The class is then tested without the real dependency.
  4. So the benefit is that mocks can be passed in easily.

Why the other options are wrong

  • A. DI does not disable logging.
  • B. Tests still need test data; DI does not remove that need.
  • C. Tests still need assertions to check results.

Tip: Constructor injection makes classes easy to test with fakes.

Question 2Easy

@Component tells Spring to:

  1. Create a database table
  2. Register it as a bean
  3. Expose a REST endpoint
  4. Compile the class faster

Answer: B. Register it as a bean

Explanation

Component scanning picks up @Component and its specialisations.

Step-by-step solution

  1. @Component marks a class for component scanning.
  2. Spring then creates an object of that class and manages it as a bean.
  3. So the meaning is option B, register it as a bean.

Why the other options are wrong

  • A. @Component does not create database tables.
  • C. REST endpoints come from controller annotations and mapping annotations, not @Component alone.
  • D. Annotations do not change how fast the class compiles.

Tip: @Component, @Service, @Repository and @Controller are all specialisations that register beans.

Question 3Easy

Inversion of Control (IoC) in Spring means:

  1. Classes create all their own dependencies
  2. The container creates and wires objects
  3. Control flow runs backwards
  4. The database controls the application

Answer: B. The container creates and wires objects

Explanation

Instead of code calling new for its collaborators, the Spring container manages object creation and wiring.

Step-by-step solution

  1. Without IoC, each class creates its own collaborators with new.
  2. With IoC, the Spring container creates objects and wires them.
  3. Control of object creation moves from the class to the container.
  4. So the answer is: the container creates and wires objects.

Why the other options are wrong

  • A. This is the opposite of IoC; classes do not create their own dependencies.
  • C. Control flow does not run backwards; control of object creation is inverted.
  • D. The database has no role in IoC.

Tip: IoC = the framework creates and connects objects, not your code.

Question 4Easy

What is the default scope of a Spring bean?

  1. Singleton
  2. Request
  3. Prototype
  4. Session

Answer: A. Singleton

Explanation

One shared instance per application context unless another scope is declared.

Step-by-step solution

  1. Default bean scope is singleton.
  2. One shared instance per context.

Why the other options are wrong

  • B. Web scope, not default.
  • C. Must be declared.
  • D. Web scope, not default.

Question 5Medium

Two beans implement the same interface NotificationService. What happens when a third bean injects NotificationService with no qualifiers?

  1. The first alphabetically is chosen
  2. Spring picks one at random
  3. Startup fails: no unique bean
  4. Both are injected as a list automatically

Answer: C. Startup fails: no unique bean

Explanation

Spring throws NoUniqueBeanDefinitionException; resolve it with @Primary, @Qualifier, or by injecting a List.

Step-by-step solution

  1. Two beans match the type.
  2. With no @Primary or @Qualifier Spring cannot choose.
  3. It throws NoUniqueBeanDefinitionException.

Why the other options are wrong

  • A. No alphabetical rule.
  • B. No random choice.
  • D. Only a List/Collection parameter gets all of them.

Question 6Medium

Which annotation marks a method that runs once after a bean's dependencies have been injected?

  1. @Bean
  2. @PostConstruct
  3. @Autowired
  4. @PreDestroy

Answer: B. @PostConstruct

Explanation

@PostConstruct (jakarta.annotation) runs initialisation logic; @PreDestroy runs before the bean is destroyed.

Step-by-step solution

  1. The question asks for the method that runs after dependencies are injected.
  2. @PostConstruct marks it for initialisation after injection.
  3. @PreDestroy runs before the bean is destroyed, and @Bean declares a bean.
  4. So the answer is @PostConstruct.

Why the other options are wrong

  • A. @Bean declares a bean in a configuration class.
  • C. @Autowired requests injection; it does not run a method after it.
  • D. @PreDestroy runs before destruction, not after injection.

Tip: @PostConstruct = after injection. @PreDestroy = before destruction.

Question 7Medium

Which annotation selects a specific bean by name at the injection point?

  1. @Value
  2. @Primary
  3. @Profile
  4. @Qualifier

Answer: D. @Qualifier

Explanation

@Qualifier("smsService") picks the bean with that qualifier or name.

Step-by-step solution

  1. The question asks for an annotation that selects a bean by name.
  2. @Qualifier takes a name, such as @Qualifier("smsService").
  3. The other annotations set values, profiles or preferences.
  4. So the answer is @Qualifier.

Why the other options are wrong

  • A. @Value injects a property or literal value.
  • B. @Primary sets a default, not a name.
  • C. @Profile activates beans for an environment.

Tip: @Qualifier names the bean; @Primary sets the default.

Question 8Medium

Which annotation marks one of several candidate beans as the default choice for injection?

  1. @Qualifier
  2. @Primary
  3. @Lazy
  4. @Order

Answer: B. @Primary

Explanation

@Primary gives a bean preference when multiple candidates match.

Step-by-step solution

  1. When several beans match an injection, Spring must pick one.
  2. @Primary marks one bean as the preferred default.
  3. @Qualifier picks a named bean, and @Lazy and @Order do different jobs.
  4. So the answer is @Primary.

Why the other options are wrong

  • A. @Qualifier selects a specific bean, not the default.
  • C. @Lazy delays bean creation.
  • D. @Order sets the order of beans in a list, not the default choice.

Tip: @Primary = default choice. @Qualifier = pick by name.

Question 9Hard

A prototype-scoped bean is injected into a singleton bean via constructor injection. How many prototype instances does the singleton see over its lifetime?

  1. A new one per method call
  2. One per HTTP request
  3. Zero
  4. One

Answer: D. One

Explanation

Injection happens once when the singleton is created; use ObjectProvider or lookup methods to get fresh prototypes.

Step-by-step solution

  1. Constructor injection runs once, when the singleton is created.
  2. The prototype bean is created at that point and injected once.
  3. The singleton keeps that same instance for its whole life.
  4. So the singleton sees one prototype instance over its lifetime.

Why the other options are wrong

  • A. A new instance per call needs lookup or ObjectProvider, not plain injection.
  • B. A new one per HTTP request is a request scope, not prototype injection.
  • C. The singleton does get one instance, so zero is wrong.

Tip: Prototype beans injected into singletons are created once; use ObjectProvider for fresh ones.

Question 10Hard

Bean A needs B in its constructor and B needs A in its constructor. In a default Spring Boot 3 application, what happens?

  1. Spring resolves it silently with proxies
  2. Startup fails
  3. A is created twice
  4. B is injected as null

Answer: B. Startup fails

Explanation

Constructor cycles cannot be resolved, and Spring Boot (since 2.6) also prohibits circular references by default.

Step-by-step solution

  1. Each bean needs the other in its constructor.
  2. A constructor cannot finish until the other bean exists, so the chain never ends.
  3. Since Spring Boot 2.6, circular references are prohibited by default.
  4. So startup fails.

Why the other options are wrong

  • A. Silent proxy resolution does not happen by default; circular references are blocked.
  • C. A is not created twice; the cycle is detected and startup stops.
  • D. B is not injected as null; the application fails to start.

Tip: Break constructor cycles with redesign, or as a last resort with setter injection and spring.main.allow-circular-references.

Practise all 20 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 →

Related topics

See all Spring Boot & JPA topics · All topics