Spring IOC Container MCQ - Multiple Choice Questions and Answers

Inversion of Control (IOC) is a design principle employed by the Spring framework to invert the control of object instantiation and dependency management from the programmer to the framework itself. This MCQ set aims to evaluate and bolster your understanding of the Spring IOC container. Let's dive right in!

1. Which of the following is responsible for instantiating beans in Spring?

a) BeanFactory
b) ApplicationContext
c) Both a & b
d) None of the above

Answer:

c) Both a & b

Explanation:

Both BeanFactory and ApplicationContext are part of the Spring IOC container and are responsible for instantiating beans. However, ApplicationContext provides more advanced features.

2. Which of the following is NOT a way to configure beans in Spring?

a) XML
b) Java Annotations
c) Property files
d) Java Config classes

Answer:

c) Property files

Explanation:

Property files are generally used for external configurations and not for bean definitions. Bean configurations are typically done using XML, Java Annotations, or Java Config classes.

3. Which annotation denotes a method that will be invoked after bean instantiation?

a) @PostConstruct
b) @Autowired
c) @Bean
d) @Before

Answer:

a) @PostConstruct

Explanation:

@PostConstruct is a JSR-250 annotation indicating the method to be executed after dependency injection is done to perform any initialization.

4. What does the 'prototype' scope mean for a Spring bean?

a) Single instance per Spring container
b) New instance every time it's requested
c) Shared instance for a single HTTP request
d) Shared instance for a single HTTP session

Answer:

b) New instance every time it's requested

Explanation:

'prototype' scope in Spring means a new bean instance will be created every time it is requested from the container.

5. Which of the following is NOT a function of the Spring IOC container?

a) Dependency management
b) Transaction management
c) Bean lifecycle management
d) Aspect-Oriented Programming support

Answer:

b) Transaction management

Explanation:

While Spring does provide transaction management support, it is not the responsibility of the IOC container. The IOC container is mainly concerned with managing bean instantiation, dependencies, and lifecycle.

6. The @Autowired annotation can be used on which of the following?

a) Constructor
b) Setter method
c) Field
d) All of the above

Answer:

d) All of the above

Explanation:

The @Autowired annotation can be used on constructors, setter methods, and fields to inject dependencies automatically.

7. Which Spring module provides the IOC container functionality?

a) Spring AOP
b) Spring Web MVC
c) Spring Core Container
d) Spring Data

Answer:

c) Spring Core Container

Explanation:

The Spring Core Container module provides the foundational building blocks of the Spring framework including the IOC container.

8. In which of the following scenarios would the @Qualifier annotation be particularly useful?

a) When you have a single bean implementation
b) When you have multiple bean implementations for an interface and want to specify which one to use
c) When you want to define a new bean
d) When you want to destroy a bean

Answer:

b) When you have multiple bean implementations for an interface and want to specify which one to use

Explanation:

@Qualifier is used to resolve the ambiguity when multiple beans of the same type exist and one needs to be chosen for autowiring.

9. Which of the following provides event-handling capabilities in the Spring context?

a) BeanFactory
b) ApplicationContext
c) Both a & b
d) None of the above

Answer:

b) ApplicationContext

Explanation:

ApplicationContext provides a way to manage different types of events and has built-in support for event propagation.

10. What is the default scope of a Spring bean if no scope is specified?

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

Answer:

d) singleton

Explanation:

If no scope is specified for a Spring bean, its default scope will be 'singleton', meaning only one instance of the bean will be created for the entire Spring container.


Related Spring MCQ Posts

In conclusion, the Spring IOC container is a fundamental aspect of the Spring framework, facilitating the inversion of control and dependency injection. This quiz aimed to challenge your understanding of its key concepts. Continue to explore and practice to master the vast capabilities of Spring!

Comments