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) @Componentizeb) @Component
c) @SpringComponent
d) @BeanClass
Answer:
b) @Component
2. Which annotation is used to create a RESTFul web service endpoint in Spring Boot?
a) @Controllerb) @WebService
c) @WebEndpoint
d) @RestController
Answer:
d) @RestController
b) @ErrorHandle
c) @CatchException
d) @ExceptionResolver
3. To handle exceptions in Spring MVC, which annotation would you use on a method?
a) @ExceptionHandlerb) @ErrorHandle
c) @CatchException
d) @ExceptionResolver
Answer:
a) @ExceptionHandler
b) @ValueBind
c) @ConfigurationProperties
d) @PropertyMap
4. Which annotation helps in binding the application.properties value to a POJO?
a) @PropertyBindb) @ValueBind
c) @ConfigurationProperties
d) @PropertyMap
Answer:
c) @ConfigurationProperties
b) @IntervalRun
c) @Scheduled
d) @Timer
5. Which annotation is used to run a method at a fixed interval in Spring?
a) @Repeaterb) @IntervalRun
c) @Scheduled
d) @Timer
Answer:
c) @Scheduled
b) Automatic dependency injection
c) To create new bean instances
d) To destroy bean instances
6. What is the purpose of @Autowired in Spring?
a) Manual injection of a beanb) Automatic dependency injection
c) To create new bean instances
d) To destroy bean instances
Answer:
b) Automatic dependency injection
b) @Transaction
c) @BeginTransaction
d) @Transactional
7. Which annotation marks a method to be transactional in Spring?
a) @TransactionalMethodb) @Transaction
c) @BeginTransaction
d) @Transactional
Answer:
d) @Transactional
b) @ComponentScan
c) @Bootstrap
d) @SpringApplication
8. For component scanning in a Spring Boot application, which annotation is pivotal?
a) @ScanComponentsb) @ComponentScan
c) @Bootstrap
d) @SpringApplication
Answer:
b) @ComponentScan
b) @RepositoryBean
c) @JpaRepository
d) @Repository
9. Which annotation helps in creating a JPA repository?
a) @EntityRepositoryb) @RepositoryBean
c) @JpaRepository
d) @Repository
Answer:
d) @Repository
b) @RequestValue
c) @ParamValue
d) @WebParam
10. How do you expose a method parameter to web requests in Spring MVC?
a) @RequestParamb) @RequestValue
c) @ParamValue
d) @WebParam
Answer:
a) @RequestParam
b) Resolves ambiguity during auto-wiring
c) Defines bean scope
d) Destroys a bean
11. What does @Qualifier do in Spring?
a) Specifies bean creation orderb) Resolves ambiguity during auto-wiring
c) Defines bean scope
d) Destroys a bean
Answer:
b) Resolves ambiguity during auto-wiring
b) @ConfigProfile
c) @ActiveProfile
d) @SpringProfile
12. To specify a specific profile for a Spring configuration, you'd use:
a) @Profileb) @ConfigProfile
c) @ActiveProfile
d) @SpringProfile
Answer:
a) @Profile
b) @Conditional
c) @IfBean
d) @ConditionalBean
13. Which Spring Boot annotation is used for conditionally loading beans?
a) @LoadOnConditionb) @Conditional
c) @IfBean
d) @ConditionalBean
Answer:
b) @Conditional
b) @MVC
c) @Controller
d) @WebComponent
14. Which annotation marks a class as a Spring MVC Controller?
a) @WebControllerb) @MVC
c) @Controller
d) @WebComponent
Answer:
c) @Controller
b) @InjectProperty
c) @Property
d) @Value
15. How do you inject a configuration property in Spring Boot?
a) @ConfigPropertyb) @InjectProperty
c) @Property
d) @Value
Answer:
d) @Value
b) @ConfigClass
c) @Configuration
d) @Setup
16. To mark a class as a Spring Configuration, you'd use:
a) @SpringConfigb) @ConfigClass
c) @Configuration
d) @Setup
Answer:
c) @Configuration
b) @Slf4j
c) @Log
d) @Logging
17. Which annotation is specifically for injecting logging capability?
a) @Loggerb) @Slf4j
c) @Log
d) @Logging
Answer:
b) @Slf4j
b) @EntityClass
c) @TableEntity
d) @Entity
18. Which annotation is used to create a JPA entity?
a) @JPAClassb) @EntityClass
c) @TableEntity
d) @Entity
Answer:
d) @Entity
b) @MockBean
c) @InjectMock
d) @MockInject
19. Which Spring annotation is used to inject mock beans during testing?
a) @TestBeanb) @MockBean
c) @InjectMock
d) @MockInject
Answer:
b) @MockBean
b) Map bean dependencies
c) Request a new bean instance
20. What is the purpose of @RequestMapping in Spring MVC?
a) Map web requests to specific handler classes/methodsb) Map bean dependencies
c) Request a new bean instance
d) Manage request parameters
Answer:
a) Map web requests to specific handler classes/methods
b) @BeforeInvoke
c) @PreMethod
d) @ModelAttribute
21. Which annotation ensures a method is invoked before a controller method?
a) @BeforeActionb) @BeforeInvoke
c) @PreMethod
d) @ModelAttribute
Answer:
d) @ModelAttribute
b) @RestController
c) @RepositoryRestResource
d) @EntityEndpoint
22. How do you create a RESTful web service CRUD repository in Spring Data REST?
a) @RestRepositoryb) @RestController
c) @RepositoryRestResource
d) @EntityEndpoint
Answer:
c) @RepositoryRestResource
b) @FeignClient
c) @WebClient
d) @HttpBean
23. To consume services as a REST client in Spring Boot, you'd annotate a bean with:
a) @RestClientb) @FeignClient
c) @WebClient
d) @HttpBean
Answer:
b) @FeignClient
b) @MetricsEndpoint
c) @Endpoint
d) @Monitor
24. Which Spring Boot annotation helps to expose custom actuator endpoints?
a) @ActuatorEndpointb) @MetricsEndpoint
c) @Endpoint
d) @Monitor
Answer:
c) @Endpoint
b) @BeforeEach
c) @BeforeBean
d) @Prioritize
25. Which annotation ensures certain beans are initialized before the current bean?
a) @DependsOnb) @BeforeEach
c) @BeforeBean
d) @Prioritize
Answer:
a) @DependsOn