Spring Dependency Injection MCQ - Multiple Choice Questions and Answers

At the heart of the Spring framework lies the concept of Dependency Injection (DI). For beginners, DI might seem like a complex idea, but it’s an invaluable tool for crafting clean, decoupled, and testable code. Think of it as the magic glue that intelligently assembles our applications, ensuring components get what they need to function properly. This blog post provides a set of multiple-choice questions aimed at helping newcomers reinforce their understanding of Spring DI.

1. What is the primary goal of Dependency Injection (DI) in Spring?

a) Memory management
b) Decoupling application components
c) Providing annotations
d) Generating Java bytecode

Answer:

b) Decoupling application components

Explanation:

Dependency Injection promotes the decoupling of components. Instead of components searching for their dependencies, they are provided (injected) with them, thus improving modularity and testability.

2. Which of the following is NOT a type of Dependency Injection in Spring?

a) Constructor Injection
b) Setter Injection
c) Method Injection
d) Field Injection

Answer:

c) Method Injection

Explanation:

Spring primarily supports Constructor, Setter, and Field Injection. Method Injection has a different context in Spring and doesn't directly pertain to the DI as beginners understand it.

3. Which annotation is commonly used for autowiring in Spring?

a) @InjectBean
b) @Auto
c) @Dependency
d) @Autowired

Answer:

d) @Autowired

Explanation:

The @Autowired annotation in Spring indicates that a particular field or constructor should be auto-wired by Spring's DI facilities.

4. If a bean has multiple qualifying dependencies, which annotation can help in specifying the right one?

a) @Select
b) @Choose
c) @Qualifier
d) @Pick

Answer:

c) @Qualifier

Explanation:

The @Qualifier annotation is used alongside @Autowired to specify which particular bean should be injected when there are multiple candidates.

5. Which of the following is the default scope of a Spring Bean?

a) prototype
b) request
c) singleton
d) global

Answer:

c) singleton

Explanation:

The default scope of a Spring bean is "singleton", meaning only one instance is created and managed by the Spring container.

6. Which of the following is true when using @Autowired on a setter method?

a) The setter method becomes optional
b) The related property can't be injected using XML
c) The setter method can have any name
d) The setter method can have multiple parameters

Answer:

c) The setter method can have any name

Explanation:

While traditionally, setter methods follow the setPropertyName pattern, when using @Autowired, the method name does not have to follow this convention.

7. What happens if no matching bean is found for an @Autowired dependency by default?

a) Spring creates a new bean
b) The application runs but with null dependency
c) An exception is thrown
d) The property is left uninitialized

Answer:

c) An exception is thrown

Explanation:

If Spring can't resolve an @Autowired dependency and no explicit handling is set, it will throw a NoSuchBeanDefinitionException.

8. How can you ensure that only one instance of a bean is created in a Spring container?

a) Set bean scope to "singleton"
b) Set bean scope to "prototype"
c) Set bean scope to "global"
d) Use @OneInstance annotation

Answer:

a) Set bean scope to "singleton"

Explanation:

In Spring, the "singleton" scope ensures that only one instance of a bean is created in the container.

9. What does the @Value annotation do in Spring?

a) It ensures that the property value remains constant
b) It injects values from properties files or literals
c) It checks the value of a bean's property
d) It sets a default value for a bean's property

Answer:

b) It injects values from properties files or literals

Explanation:

The @Value annotation in Spring is used to inject values, often from properties files or directly as hardcoded literals.

10. Which of the following annotations makes a particular bean the primary candidate for autowiring when multiple beans of the same type exist?

a) @First
b) @Primary
c) @MainBean
d) @Root

Answer:

b) @Primary

Explanation:

The @Primary annotation indicates that a bean should be given precedence when multiple beans match a single autowiring destination.


Related Spring MCQ Posts

Grasping the concept of Dependency Injection is pivotal for anyone diving into the Spring framework. It not only forms the basis for many advanced features but also shapes the way applications are structured and developed in Spring. We hope these MCQs provided a fun and insightful way for beginners to test and solidify their understanding. Remember, every expert was once a beginner. Keep learning and exploring!

Comments