Spring Online Test - MCQ Questions

Welcome to the Spring Online Test! We will present 35 MCQs (Multiple-Choice Questions) to test your knowledge of the Spring framework and its modules, such as Spring core, Spring MVC, Spring Boot, Spring Data JPA, Spring AOP, and more.

You can select the correct answer for each question and submit the test. You will get your online test score after finishing the complete test.

1. What annotation is used to mark a class as a Spring bean in the Spring Framework?

a) @Bean
b) @Entity
c) @Component
d) @Service

2. Which annotation should be used to autowire a dependency in Spring?

a) @Autowired
b) @Auto
c) @Dependency
d) @Inject

3. How do you define a bean with a prototype scope in Spring?

a) @Prototype
b) @Bean(scope = "prototype")
c) @Scope("prototype")
d) @BeanType("prototype")

4. What is the purpose of the @Transactional annotation in Spring?

a) To define a transaction boundary
b) To automatically inject dependencies
c) To create a new bean instance
d) To mark a method that should not be included in the transaction

5. What does the Spring Framework use to resolve view names to actual views?

a) ViewResolver
b) ModelAndView
c) DispatcherServlet
d) WebResolver

6. Which Spring module provides support for creating RESTful Web Services?

a) Spring MVC
b) Spring Boot
c) Spring Core
d) Spring REST

7. How can you handle exceptions in Spring MVC applications?

a) @ControllerAdvice
b) @ExceptionHandler
c) Both a and b
d) @ErrorHandling

8. What does the following Spring expression do?

@Value("#{systemProperties['user.region']}")
private String userRegion;
a) Injects the value of the user.region system property into the userRegion field
b) Sets the userRegion field to a static string value 'user.region'
c) Creates a new system property named 'user.region'
d) Checks the system properties for 'user.region' at runtime

9. Which interface in Spring can be implemented to perform specific actions upon bean initialization and destruction?

a) InitializingBean and DisposableBean
b) BeanFactoryAware
c) ApplicationContextAware
d) BeanNameAware

10. What annotation is used in Spring Data JPA to define a repository interface?

a) @Repository
b) @Entity
c) @Table
d) @DataRepository

11. What is the primary use of the @RequestBody annotation in Spring MVC?

@PostMapping("/users")
public ResponseEntity<String> addUser(@RequestBody User user) {
    // Add user to the database
    return ResponseEntity.ok("User added");
}
a) To bind a method return value to the response body
b) To bind the request body to a method parameter
c) To send a request to the body of a controller method
d) To receive the request body as a response

12. Which annotation configures a method to be triggered by cron expression in Spring?

a) @Scheduled(cron="*/5 * * * * *")
b) @Cron("*/5 * * * * *")
c) @Execute(cron="*/5 * * * * *")
d) @Timer(cron="*/5 * * * * *")

13. What is the role of the @EnableAutoConfiguration annotation in Spring Boot?

a) To manually configure beans
b) To enable Spring Boot’s auto-configuration feature
c) To disable specific auto-configuration classes
d) To configure properties files

14. How do you map a specific exception to an HTTP status code in Spring MVC?

a) @ResponseStatus
b) @HttpError
c) @ErrorCode
d) @ExceptionHandler

15. Which component is responsible for handling web requests in Spring MVC?

a) HandlerMapping
b) ModelAndView
c) DispatcherServlet
d) ViewResolver

16. What is the role of the @Aspect annotation in Spring AOP?

a) It defines a class as a configuration class
b) It marks a class as an aspect part of AOP
c) It designates a service layer class
d) It marks a class as a data repository

17. What is the purpose of the @Transactional annotation in Spring?

a) To ensure a method runs within a database transaction context
b) To create a new application context
c) To initiate a new thread
d) To mark a configuration setting

18. What Spring module is primarily focused on the configuration and management of application contexts?

a) Spring MVC
b) Spring Core
c) Spring Security
d) Spring Boot

19. What does the @Value annotation do in Spring?

a) Injects property values into beans
b) Sets fixed values for method arguments
c) Calculates the value of a bean's properties based on a formula
d) Marks a field as important for logging

20. Which AOP concept describes a situation where aspects are applied?

a) Join point
b) Pointcut
c) Advice
d) Aspect

21. What is the primary benefit of using DI (Dependency Injection) in Spring?

a) It allows the framework to control the application flow
b) It provides a mechanism for encoding data
c) It facilitates better testing capabilities by decoupling class dependencies
d) It enhances the performance of applications

22. What annotation is used to run a Spring Boot application?

a) @SpringBootConfiguration
b) @EnableAutoConfiguration
c) @SpringBootApplication
d) @ApplicationRunner

23. Which annotation is used in Spring Boot to externalize configuration to a properties file?

a) @ConfigurationProperties(prefix="app")
b) @PropertySource("classpath:app.properties")
c) @EnableConfigurationProperties
d) Both a and b

24. What does the @EnableJpaRepositories annotation do in a Spring Boot application?

a) Enables support for JPA repositories.
b) Creates JPA repositories implementations automatically.
c) Specifies the packages to scan for JPA repositories.
d) Both a and c

25. How do you handle application events in Spring Boot?

a) Implementing ApplicationListener<>
b) Using @EventListener
c) Both a and b
d) Using @HandleEvent

26. What is the function of the @Profile annotation in a Spring Boot application?

a) To provide versioning capability for different beans
b) To define beans only available in certain Spring profiles
c) To enhance performance profiling
d) To configure application profiles based on environments

27. What is the use of the @Scheduled annotation in Spring Boot?

a) To configure batch jobs
b) To execute a method at fixed intervals
c) To manage asynchronous tasks
d) To launch an application at a scheduled time

28. What annotation is used to mark a class as a JPA entity in Spring Data JPA?

a) @Entity
b) @Table
c) @Model
d) @JPAEntity

29. What does the @Repository annotation do in Spring Data JPA?

a) It designates a class as a repository that can interact with the database.
b) It automatically generates SQL queries for the application.
c) It provides translations of database access exceptions to Spring's DataAccessException.
d) Both a and c

30. In Spring MVC, what is the purpose of the @RequestMapping annotation?

a) To map a URL to a method in a controller
b) To request data from the model
c) To specify which view should be returned by the controller
d) To validate form inputs

31. Which annotation in Spring MVC is used to bind a method parameter to a named model attribute?

a) @ModelAttribute
b) @RequestParam
c) @PathVariable
d) @RequestBody

32. How do you define a named query in a JPA entity?

a) Using the @Query annotation on the repository interface
b) Using the @NamedQuery annotation on the entity class
c) Defining the query directly in the @Entity annotation
d) By implementing a custom repository method

33. What Spring MVC annotation is used to automatically deserialize a JSON payload into a Java object?

a) @RequestBody
b) @ResponseBody
c) @Model
d) @JsonParam

34. What is the role of the @Service annotation in Spring?

a) Marks a class as a DAO
b) Marks a class as a controller
c) Defines a class as a service layer component
d) Configures a class to publish REST endpoints

35. How does Spring Data JPA handle method transactions by default?

a) Requires manual configuration for each method
b) Provides no transaction support
c) Each method is transactional by default
d) Only reads are transactional

Comments