Sample questions
Question 1Easy
Which is a benefit of dependency injection for unit testing?
- It disables logging
- It removes the need for test data
- Tests no longer need assertions
- 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
- With DI, a class receives its collaborators from outside.
- A test can pass in a mock or fake through the constructor.
- The class is then tested without the real dependency.
- 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:
- Create a database table
- Register it as a bean
- Expose a REST endpoint
- Compile the class faster
Answer: B. Register it as a bean
Explanation
Component scanning picks up @Component and its specialisations.
Step-by-step solution
- @Component marks a class for component scanning.
- Spring then creates an object of that class and manages it as a bean.
- 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:
- Classes create all their own dependencies
- The container creates and wires objects
- Control flow runs backwards
- 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
- Without IoC, each class creates its own collaborators with new.
- With IoC, the Spring container creates objects and wires them.
- Control of object creation moves from the class to the container.
- 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?
- Singleton
- Request
- Prototype
- Session
Answer: A. Singleton
Explanation
One shared instance per application context unless another scope is declared.
Step-by-step solution
- Default bean scope is singleton.
- 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?
- The first alphabetically is chosen
- Spring picks one at random
- Startup fails: no unique bean
- 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
- Two beans match the type.
- With no @Primary or @Qualifier Spring cannot choose.
- 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?
- @Bean
- @PostConstruct
- @Autowired
- @PreDestroy
Answer: B. @PostConstruct
Explanation
@PostConstruct (jakarta.annotation) runs initialisation logic; @PreDestroy runs before the bean is destroyed.
Step-by-step solution
- The question asks for the method that runs after dependencies are injected.
- @PostConstruct marks it for initialisation after injection.
- @PreDestroy runs before the bean is destroyed, and @Bean declares a bean.
- 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?
- @Value
- @Primary
- @Profile
- @Qualifier
Answer: D. @Qualifier
Explanation
@Qualifier("smsService") picks the bean with that qualifier or name.
Step-by-step solution
- The question asks for an annotation that selects a bean by name.
- @Qualifier takes a name, such as @Qualifier("smsService").
- The other annotations set values, profiles or preferences.
- 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?
- @Qualifier
- @Primary
- @Lazy
- @Order
Answer: B. @Primary
Explanation
@Primary gives a bean preference when multiple candidates match.
Step-by-step solution
- When several beans match an injection, Spring must pick one.
- @Primary marks one bean as the preferred default.
- @Qualifier picks a named bean, and @Lazy and @Order do different jobs.
- 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?
- A new one per method call
- One per HTTP request
- Zero
- 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
- Constructor injection runs once, when the singleton is created.
- The prototype bean is created at that point and injected once.
- The singleton keeps that same instance for its whole life.
- 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?
- Spring resolves it silently with proxies
- Startup fails
- A is created twice
- 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
- Each bean needs the other in its constructor.
- A constructor cannot finish until the other bean exists, so the chain never ends.
- Since Spring Boot 2.6, circular references are prohibited by default.
- 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 →