Spring and Spring Boot Annotations MCQ (Multiple Choice Questions)

In this post, we will present 25+ MCQ (Multiple Choice Questions) to test your knowledge of Spring and Spring Boot annotations.

1. Which annotation is used to make a Java class as a Spring component?

a) @Componentize
b) @Component
c) @SpringComponent
d) @BeanClass

Answer: 

b) @Component

2. Which annotation is used to create a RESTFul web service endpoint in Spring Boot?

a) @Controller
b) @WebService
c) @WebEndpoint
d) @RestController

Answer: 

d) @RestController

3. To handle exceptions in Spring MVC, which annotation would you use on a method?

a) @ExceptionHandler
b) @ErrorHandle
c) @CatchException
d) @ExceptionResolver

Answer:

 a) @ExceptionHandler

4. Which annotation helps in binding the application.properties value to a POJO?

a) @PropertyBind
b) @ValueBind
c) @ConfigurationProperties
d) @PropertyMap

Answer: 

c) @ConfigurationProperties

5. Which annotation is used to run a method at a fixed interval in Spring?

a) @Repeater
b) @IntervalRun
c) @Scheduled
d) @Timer

Answer: 

c) @Scheduled

6. What is the purpose of @Autowired in Spring?

a) Manual injection of a bean
b) Automatic dependency injection
c) To create new bean instances
d) To destroy bean instances

Answer: 

b) Automatic dependency injection

7. Which annotation marks a method to be transactional in Spring?

a) @TransactionalMethod
b) @Transaction
c) @BeginTransaction
d) @Transactional

Answer: 

d) @Transactional

8. For component scanning in a Spring Boot application, which annotation is pivotal?

a) @ScanComponents
b) @ComponentScan
c) @Bootstrap
d) @SpringApplication

Answer: 

b) @ComponentScan

9. Which annotation helps in creating a JPA repository?

a) @EntityRepository
b) @RepositoryBean
c) @JpaRepository
d) @Repository

Answer: 

d) @Repository

10. How do you expose a method parameter to web requests in Spring MVC?

a) @RequestParam
b) @RequestValue
c) @ParamValue
d) @WebParam

Answer: 

a) @RequestParam

11. What does @Qualifier do in Spring?

a) Specifies bean creation order
b) Resolves ambiguity during auto-wiring
c) Defines bean scope
d) Destroys a bean

Answer: 

b) Resolves ambiguity during auto-wiring

12. To specify a specific profile for a Spring configuration, you'd use:

a) @Profile
b) @ConfigProfile
c) @ActiveProfile
d) @SpringProfile

Answer: 

a) @Profile

13. Which Spring Boot annotation is used for conditionally loading beans?

a) @LoadOnCondition
b) @Conditional
c) @IfBean
d) @ConditionalBean

Answer: 

b) @Conditional

14. Which annotation marks a class as a Spring MVC Controller?

a) @WebController
b) @MVC
c) @Controller
d) @WebComponent

Answer: 

c) @Controller

15. How do you inject a configuration property in Spring Boot?

a) @ConfigProperty
b) @InjectProperty
c) @Property
d) @Value

Answer: 

d) @Value

16. To mark a class as a Spring Configuration, you'd use:

a) @SpringConfig
b) @ConfigClass
c) @Configuration
d) @Setup

Answer: 

c) @Configuration

17. Which annotation is specifically for injecting logging capability?

a) @Logger
b) @Slf4j
c) @Log
d) @Logging

Answer: 

b) @Slf4j

18. Which annotation is used to create a JPA entity?

a) @JPAClass
b) @EntityClass
c) @TableEntity
d) @Entity

Answer: 

d) @Entity

19. Which Spring annotation is used to inject mock beans during testing?

a) @TestBean
b) @MockBean
c) @InjectMock
d) @MockInject

Answer:

 b) @MockBean

20. What is the purpose of @RequestMapping in Spring MVC?

a) Map web requests to specific handler classes/methods
b) Map bean dependencies
c) Request a new bean instance
d) Manage request parameters

Answer: 

a) Map web requests to specific handler classes/methods

21. Which annotation ensures a method is invoked before a controller method?

a) @BeforeAction
b) @BeforeInvoke
c) @PreMethod
d) @ModelAttribute

Answer: 

d) @ModelAttribute

22. How do you create a RESTful web service CRUD repository in Spring Data REST?

a) @RestRepository
b) @RestController
c) @RepositoryRestResource
d) @EntityEndpoint

Answer: 

c) @RepositoryRestResource

23. To consume services as a REST client in Spring Boot, you'd annotate a bean with:

a) @RestClient
b) @FeignClient
c) @WebClient
d) @HttpBean

Answer: 

b) @FeignClient

24. Which Spring Boot annotation helps to expose custom actuator endpoints?

a) @ActuatorEndpoint
b) @MetricsEndpoint
c) @Endpoint
d) @Monitor

Answer: 

c) @Endpoint

25. Which annotation ensures certain beans are initialized before the current bean?

a) @DependsOn
b) @BeforeEach
c) @BeforeBean
d) @Prioritize

Answer: 

a) @DependsOn

Comments