Spring Framework MCQ

Test your understanding of the Spring Framework fundamentals with this beginner-friendly quiz! Here are 40+ multiple-choice questions that cover the foundational concepts. Dive in and see how much you know!

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

1. What is minimum Java version requires to use Spring Framework 6?

a) Java 8
b) Java 17
c) Java 11
d) Java 15

Answer:

b) Java 17

Explanation:

Spring Framework 6 requires Java 17 or later.

2. What is the core concept behind the Spring Framework?

a) Servlets
b) Inversion of Control (IoC)
c) JSP
d) JDBC

Answer:

b) Inversion of Control (IoC)

Explanation:

The core concept behind Spring is the Inversion of Control (IoC) which means that the control of objects is transferred from the developer to the Spring container.

3. What is Dependency Injection (DI) in Spring Framework?

a) A design pattern
b) A way to achieve Inversion of Control
c) A module in Spring
d) A Spring annotation

Answer:

b) A way to achieve Inversion of Control

Explanation:

Dependency Injection is a technique to implement Inversion of Control. It allows the system to be more decoupled and modular.

4. Which of the following is NOT a module in the Spring Framework?

a) Spring AOP
b) Spring MVC
c) Spring JPA
d) Spring JDBC

Answer:

c) Spring JPA

Explanation:

There’s no module named “Spring JPA”. JPA stands for Java Persistence API which is used with Spring Data JPA, but it’s not a core module of Spring.

5. What is the primary function of the Spring IoC container?

a) Transaction management
b) Aspect-oriented programming
c) Bean lifecycle management
d) Data access

Answer:

c) Bean lifecycle management

Explanation:

The primary function of the Spring IoC (Inversion of Control) container is to manage the lifecycle of beans, including their creation, wiring, and destruction.

6. Which annotation is used to mark a class as a Spring bean?

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

Answer:

b) @Component

Explanation:

The @Component annotation is a generic stereotype to denote a Spring-managed bean. Though @Repository, and @Service are also correct, they are specialized stereotypes with additional semantics.

7. What does the Spring @Autowired annotation do?

a) Creates a new instance of a bean
b) Performs dependency injection
c) Initialises the application context
d) Specifies a bean’s lifecycle method

Answer:

b) Performs dependency injection

Explanation:

The @Autowired annotation tells Spring to inject (or wire) a dependency into a bean automatically.

8. Which module in Spring is used for AOP (Aspect-Oriented Programming)?

a) Spring ORM
b) Spring AOP
c) Spring DAO
d) Spring Web

Answer:

Explanation:

Answer: b) Spring AOP

9. In which layer is Spring MVC used?

a) Data layer
b) Business layer
c) Presentation layer
d) Integration layer

Answer:

c) Presentation layer

Explanation:

Spring MVC is used in the presentation layer to handle web requests and construct responses.

10. Which annotation is used to inject a dependency in Spring?

a) @Inject
b) @Autowired
c) @Resource
d) All of the above

Answer:

d) All of the above

Explanation:

All these annotations can be used to inject dependencies in Spring-based applications.

11. Which Spring annotation is used to handle exceptions at the controller level?

a) @ExceptionHandler
b) @ControllerAdvice
c) @ErrorHandle
d) @ThrowException

Answer:

a) @ExceptionHandler

Explanation:

@ExceptionHandler is used in Spring MVC to handle specific exceptions at the controller level.

12. Which annotation is used to make a Java class serve RESTful requests?

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

Answer:

a) @RestController

Explanation:

@RestController is a specialized version of the controller that includes @Controller and @ResponseBody annotations, making it suited for building RESTful APIs.

13. What does @Qualifier annotation do?

a) Qualifies a bean’s priority
b) Determines the autowiring mode
c) Resolves the autowiring ambiguity
d) Specifies a bean’s scope

Answer:

c) Resolves the autowiring ambiguity

Explanation:

When multiple beans of the same type exist, @Qualifier is used to specify which bean should be autowired.

14. Which of these is a pointcut expression type in Spring AOP?

a) execution
b) resolution
c) initiation
d) injection

Answer:

a) execution

Explanation:

In Spring AOP, “execution” is a pointcut designator (PCD) used to match method execution join points.

15. What is the default scope of a Spring bean?

a) prototype
b) request
c) session
d) singleton

Answer:

d) singleton

Explanation:

By default, beans in Spring are singletons, meaning only one instance of the bean is created for the entire application.

16. What does the @Required annotation do?

a) Makes a bean’s property mandatory
b) Indicates that a field should be autowired
c) Specifies a bean’s lifecycle method
d) Specifies a bean’s scope

Answer:

a) Makes a bean’s property mandatory

Explanation:

The @Required annotation indicates that the affected bean property must be populated during bean configuration.

17. What does AOP stand for?

a) Additional Operation Protocol
b) Asynchronous Operating Procedure
c) Aspect-Oriented Programming
d) Angular Object Protocol

Answer:

c) Aspect-Oriented Programming

Explanation:

AOP stands for Aspect-Oriented Programming. It provides a way to add cross-cutting concerns in a modular way.

18. Which of the following is NOT an advice type in Spring AOP?

a) Before
b) After
c) Around
d) Across

Answer:

d) Across

Explanation:

Spring AOP supports Before, After, and Around advice types, among others. “Across” isn’t one of them.

19. Which annotation denotes a transaction boundary in Spring?

a) @Boundary
b) @Transaction
c) @Transact
d) @Transactional

Answer:

d) @Transactional

Explanation:

The @Transactional annotation in Spring is used to denote a transaction boundary.

20. Which module in Spring provides support for JDBC operations?

a) Spring Web
b) Spring ORM
c) Spring JDBC
d) Spring Boot

Answer:

c) Spring JDBC

Explanation:

Spring JDBC module provides a more convenient way to handle database operations compared to traditional JDBC.

21. How can you execute a method after bean initialization in Spring?

a) Use @AfterBean
b) Use afterPropertiesSet() from InitializingBean interface
c) Use @PostConstruct
d) Use @ExecuteAfterInit

Answer:

b) Use afterPropertiesSet() from InitializingBean interface

Explanation:

Implementing the afterPropertiesSet() method from the InitializingBean interface allows executing custom logic after the bean has been initialized.

22. What is Spring Boot mainly used for?

a) AOP
b) Database connectivity
c) Rapid application development
d) Web services only

Answer:

c) Rapid application development

Explanation:

Spring Boot provides various tools and defaults to facilitate rapid application development.

23. How can you fetch a bean from the Spring application context?

a) getContext().getBean(“beanName”)
b) ApplicationContext.getBean(“beanName”)
c) Beans.getBean(“beanName”)
d) SpringFactory.getBean(“beanName”)

Answer:

b) ApplicationContext.getBean(“beanName”)

Explanation:

To retrieve a bean from the application context, use the getBean method on an instance of ApplicationContext.

24. What is the purpose of BeanFactory in Spring?

a) Database connectivity
b) Aspect definition
c) Bean creation and management
d) Transaction management

Answer:

c) Bean creation and management

Explanation:

BeanFactory is the root interface for accessing the Spring container, providing bean creation and management functionalities.

25. Which of the following is NOT a Spring bean scope?

a) singleton
b) prototype
c) global
d) request

Answer:

c) global

Explanation:

Spring supports various bean scopes like singleton, prototype, request, etc., but “global” is not one of them.

26. Which annotation is used to mark a class as a Spring Repository?

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

Answer:

d) @Repository

Explanation:

@Repository is a stereotype annotation to mark a class as a Data Access Object (DAO).

27. Which module in Spring deals with cloud-based features?

a) Spring AOP
b) Spring Boot
c) Spring Cloud
d) Spring Data

Answer:

c) Spring Cloud

Explanation:

Spring Cloud provides a suite of tools for building cloud-native applications.

28. What does @Value annotation in Spring do?

a) Sets a default value to a bean property
b) Injects a property value from a property source
c) Marks a method as a bean in the configuration class
d) Specifies a bean’s scope

Answer:

b) Injects a property value from a property source

Explanation:

@Value is used to inject values from property files or default values into bean properties.

29. Which of the following is not a type of metadata provided by Spring?

a) Annotation-based
b) Java-based
c) XML-based
d) YAML-based

Answer:

d) YAML-based

Explanation:

Spring supports annotation-based, Java-based, and XML-based metadata for bean configuration, but not YAML-based.

30. How can you execute a method before bean destruction in Spring?

a) @PreDestroy
b) @Destroy
c) @BeforeDestroy
d) @Delete

Answer:

a) @PreDestroy

Explanation:

The @PreDestroy annotation indicates a method to be executed before a bean is removed from the container.

31. What does the JdbcTemplate class in Spring JDBC provide?

a) AOP support
b) RESTful API support
c) Simplified error handling in JDBC
d) ORM capabilities

Answer:

c) Simplified error handling in JDBC

Explanation:

JdbcTemplate simplifies the use of JDBC and helps to avoid common errors, offering a more streamlined way to handle database operations.

32. Which of the following is a way to introduce a bean’s metadata to the Spring container?

a) Annotating the bean class
b) Using JavaConfig (@Configuration classes)
c) Writing bean definitions in an XML file
d) All of the above

Answer:

d) All of the above

Explanation:

Spring offers multiple ways to provide bean metadata including annotations, JavaConfig, and XML configurations.

33. Which annotation is used to mark a class as configuration class in Spring?

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

Answer:

c) @Configuration

Explanation:

The @Configuration annotation indicates that the class can be used by the Spring IoC container as a source of bean definitions.

34. Which of these is NOT a type of Dependency Injection in Spring?

a) Setter Injection
b) Constructor Injection
c) Field Injection
d) Annotation Injection

Answer:

d) Annotation Injection

Explanation:

Spring supports Constructor, Setter, and Field injections. Annotations like @Autowired facilitate these injections but “Annotation Injection” itself isn’t a type.

35. Which Spring module provides features related to sending emails?

a) Spring Web
b) Spring Messaging
c) Spring Mail
d) Spring Cloud

Answer:

c) Spring Mail

Explanation:

The Spring Mail module abstracts away the complexities of sending emails, simplifying the task.

36. Which of the following is NOT a core module of the Spring framework?

a) Spring AOP
b) Spring MVC
c) Spring Boot
d) Spring Beans

Answer:

c) Spring Boot

Explanation:

Spring Boot is not a core module of the Spring framework. It’s a project built on top of the Spring framework to simplify microservice development and deployment.

37. Which of the following is true about the @Controller annotation in Spring?

a) It is used to indicate a RESTful web service
b) It is used to mark classes as Spring beans
c) It is used to indicate a class as a Spring MVC controller
d) It is used to configure beans

Answer:

c) It is used to indicate a class as a Spring MVC controller

Explanation:

The @Controller annotation indicates that a class is a Spring MVC controller that can handle HTTP requests.

38. How can you handle exceptions in Spring MVC?

a) Using @ExceptionHandler
b) Using @ControllerAdvice
c) Using @ErrorCatch
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

@ExceptionHandler is used for handling exceptions specific to a controller, while @ControllerAdvice can be used to handle exceptions across multiple controllers.

39. What does the @SpringBootApplication annotation do in Spring Boot?

a) It starts the Spring Boot application
b) It is a convenience annotation that combines @Configuration, @EnableAutoConfiguration, and @ComponentScan
c) It is used to specify the entry point for a Spring Boot application
d) It configures properties for the Spring Boot application

Answer:

b) It is a convenience annotation that combines @Configuration, @EnableAutoConfiguration, and @ComponentScan

Explanation:

@SpringBootApplication is a convenience annotation often used in the main class, combining several other annotations to bootstrap a Spring Boot application.

40. Which annotation is used to define a pointcut expression in Spring AOP?

a) @Pointcut
b) @Aspect
c) @Around
d) @CutPoint

Answer:

a) @Pointcut

Explanation:

The @Pointcut annotation is used to declare a pointcut expression in Spring AOP, defining where the aspect should be applied.

41. What is the main purpose of the @Aspect annotation in Spring AOP?

a) It ensures a bean is thread-safe
b) It marks a class as a Spring MVC controller
c) It indicates a class is a Spring bean
d) It defines a class as an aspect

Answer:

d) It defines a class as an aspect

Explanation:

The @Aspect annotation is used to declare a class as an aspect in Spring AOP.

42. What is a Pointcut in Spring AOP?

a) A condition for triggering an Aspect
b) A method where an Aspect is applied
c) A Spring Bean
d) A running thread

Answer:

a) A condition for triggering an Aspect

Explanation:

A pointcut is an expression that captures where in the codebase an aspect should be applied.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top