Spring Configuration MCQ - Multiple Choice Questions and Answers

Welcome to the Spring Configuration MCQ (Multiple Choice Questions) guide, Here, we present a set of MCQ questions to test your knowledge of Spring Configurations, different types of Spring Configurations, and associated annotations.

Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which of the following is a traditional way to configure beans in Spring?

a) YAML
b) JSON
c) XML
d) CSV

Answer:

c) XML

Explanation:

XML has been the traditional way to configure beans in Spring before the advent of annotation-based and Java-based configurations.

2. Which annotation is used to specify a Spring configuration class?

a) @Configure
b) @Config
c) @SpringConfig
d) @Configuration

Answer:

d) @Configuration

Explanation:

The @Configuration annotation indicates that a class declares one or more @Bean methods and may be processed by the Spring container.

3. Which of the following annotations enables component scanning in Spring?

a) @ComponentScan
b) @ScanComponents
c) @EnableScan
d) @SpringScan

Answer:

a) @ComponentScan

Explanation:

The @ComponentScan annotation is used with @Configuration to specify the packages to scan for annotated components.

4. If you want to import another configuration class, which annotation would you use?

a) @IncludeConfig
b) @Import
c) @UseConfig
d) @FetchConfig

Answer:

b) @Import

Explanation:

The @Import annotation allows for loading @Bean definitions from another configuration class.

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

a) XML-based configuration
b) Annotation-based configuration
c) Java-based configuration
d) Excel-based configuration

Answer:

d) Excel-based configuration

Explanation:

Spring does not support Excel-based configurations. The primary methods are XML, annotation, and Java configurations.

6. Which annotation can be used to inject a value from a properties file?

a) @Value
b) @PropValue
c) @InsertValue
d) @Property

Answer:

a) @Value

Explanation:

The @Value annotation is used to inject values from properties files or literals into fields or method parameters.

7. In Java-based configuration, which method is used to register a bean?

a) registerBean()
b) createBean()
c) bean()
d) @Bean

Answer:

d) @Bean

Explanation:

The @Bean annotation is used in Java configuration classes to indicate that a method produces a bean to be managed by the Spring container.

8. Which of the following is true for @Component annotation?

a) It can only be used on configuration classes
b) It is used for autowiring beans
c) It is a generic stereotype for any Spring-managed component
d) It specifies a post-processing bean

Answer:

c) It is a generic stereotype for any Spring-managed component

Explanation:

@Component is a generic stereotype for indicating a class is a Spring component. It's an umbrella for other annotations like @Service, @Repository, and @Controller.

9. To inject configuration properties in a type-safe manner, which annotation would you use on a class?

a) @ValueObject
b) @PropertyMapping
c) @ConfigProperties
d) @ConfigurationProperties

Answer:

d) @ConfigurationProperties

Explanation:

The @ConfigurationProperties annotation allows you to map configuration properties to a POJO in a type-safe manner.

10. To externalize configuration to properties files, which annotation should be paired with @Configuration?

a) @PropertyHolder
b) @PropertyFile
c) @PropertySource
d) @ExternalProperties

Answer:

c) @PropertySource

Explanation:

The @PropertySource annotation is used along with @Configuration to specify the path to properties files.

11. In a Spring Boot application, which file is commonly used to externalize configuration?

a) application.conf
b) spring-config.yml
c) application.properties
d) springboot.json

Answer:

c) application.properties

Explanation:

Spring Boot applications often use the application.properties file to externalize configuration. There's also an application.yml variant for those who prefer YAML format.

12. Which annotation in Spring can be used to inject values directly from the application's environment?

a) @EnvironmentValue
b) @EnvValue
c) @InjectValue
d) @Value

Answer:

d) @Value

Explanation:

The @Value annotation can be used to inject values from various sources, including the application's environment, by specifying placeholders, for instance: @Value("${envVariableName}").


Related Spring MCQ Posts

Comments