Spring Beans MCQ - Multiple Choice Questions and Answers

A bean is an object managed by the Spring container. The beauty of beans is their life cycle, configurability, and the ease with which they can be managed. Through this blog post, beginners can challenge their budding understanding of Spring beans with a set of insightful multiple-choice questions.

1. In Spring, what is a Bean?

a) A type of vegetable
b) An annotation
c) A design pattern
d) An object managed by the Spring container

Answer:

d) An object managed by the Spring container

Explanation:

In the context of Spring, a bean refers to an object that is instantiated, configured, and managed by the Spring container.

2. Which of the following annotations declares a Spring Bean?

a) @Beanify
b) @Bean
c) @Object
d) @Component

Answer:

b) @Bean

Explanation:

The @Bean annotation is used in configuration classes to declare a method as a bean producer. However, note that @Component is also used to define beans via component scanning.

3. If a class is annotated with @Component, how does Spring treat it?

a) Ignores it
b) As a prototype bean
c) As a configuration class
d) As a singleton bean

Answer:

d) As a singleton bean

Explanation:

By default, Spring treats classes annotated with @Component (and similar stereotypes like @Service, @Repository, and @Controller) as beans with singleton scope.

4. Which XML element is used to define a bean in XML-based configuration?

a) <object>
b) <declare>
c) <bean>
d) <instance>

Answer:

c) <bean>

Explanation:

In XML-based configuration, the <bean> element is used to define and configure a bean.

5. How can you specify that a bean should be created only once in the Spring container?

a) Set bean scope to "once"
b) Set bean scope to "singleton"
c) Set bean scope to "unique"
d) Set bean scope to "single"

Answer:

b) Set bean scope to "singleton"

Explanation:

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

6. Which of the following annotations is NOT used to indicate a Spring Bean?

a) @Controller
b) @Service
c) @Entity
d) @Repository

Answer:

c) @Entity

Explanation:

While @Entity is a commonly used annotation in Spring applications, it pertains to JPA and ORM rather than directly denoting a Spring Bean.

7. 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.

8. If a Spring Bean does not have any explicit destructor method specified, which method does Spring try to call when the container shuts down?

a) terminate()
b) finish()
c) shutdown()
d) destroy()

Answer:

d) destroy()

Explanation:

If a bean does not have an explicit destructor method defined and it implements the DisposableBean interface, Spring will call the destroy() method during container shutdown.

9. Which annotation can be used to autowire a bean by type?

a) @Autowire
b) @Resource
c) @Inject
d) @Autowired

Answer:

d) @Autowired

Explanation:

The @Autowired annotation in Spring is used to autowire beans by type.

10. How can you define the order of bean initialization when multiple beans depend on each other?

a) Using @Order annotation
b) By specifying depends-on attribute
c) Using @Priority annotation
d) Order is always random

Answer:

b) By specifying depends-on attribute

Explanation:

The "depends-on" attribute can be used to specify bean initialization order by explicitly mentioning which beans need to be initialized before the current bean.


Related Spring MCQ Posts

Diving deep into the realm of Spring beans offers a fresh perspective on how object creation, wiring, and management can be so seamlessly handled by a framework. These MCQs are just the tip of the iceberg. As you journey through Spring, you'll find beans to be your consistent companions, helping you craft robust applications. We encourage beginners to keep exploring, experimenting, and questioning. Happy Coding!

Comments