When would you use request-scoped beans in a Spring web application?
ATo store request-specific data that should not be shared across different HTTP requests
BTo reduce memory consumption by reusing beans across requests
CTo improve application performance significantly
DTo simplify configuration by avoiding singleton bean management
Correct Answer:
A. To store request-specific data that should not be shared across different HTTP requests
EXPLANATION
Request-scoped beans are instantiated per HTTP request and destroyed after request completion, ideal for storing user-specific or request-specific information.
What does the @EnableCaching annotation enable in a Spring application?
ADeclarative caching support using @Cacheable, @CacheEvict, and similar annotations
BAutomatic HTTP response caching by the server
CDatabase query result caching with Redis integration
DBrowser-level caching of static resources
Correct Answer:
A. Declarative caching support using @Cacheable, @CacheEvict, and similar annotations
EXPLANATION
@EnableCaching activates Spring's caching infrastructure, allowing method-level caching through annotations like @Cacheable to store and retrieve results.
What is the significance of the @ConfigurationProperties annotation in Spring Boot?
AIt binds external configuration properties to Java objects with type-safe access
BIt encrypts sensitive configuration values automatically
CIt validates configuration file syntax during application startup
DIt generates configuration files from Java classes
Correct Answer:
A. It binds external configuration properties to Java objects with type-safe access
EXPLANATION
@ConfigurationProperties enables binding of external properties (from application.properties/yml) to POJO classes, providing type safety and validation.
What does the @Transactional annotation do in Spring?
AManages database transactions, handling commit and rollback automatically
BEncrypts data being transmitted across the network
CLogs all database operations performed by the method
DValidates input parameters before method execution
Correct Answer:
A. Manages database transactions, handling commit and rollback automatically
EXPLANATION
@Transactional delegates transaction management to Spring, automatically committing on success and rolling back on exceptions, following ACID principles.
In Spring AOP, what does the term 'Join Point' refer to?
AA specific point during program execution where an aspect can be applied
BThe code written in an aspect to handle cross-cutting concerns
CA method that connects multiple beans together
DThe configuration file that defines AOP rules
Correct Answer:
A. A specific point during program execution where an aspect can be applied
EXPLANATION
A Join Point is a candidate point in program execution (method call, exception throw) where advice can be applied. Pointcuts select specific Join Points.
What is the function of the BeanFactory interface in Spring?
ATo create and manage bean instances with lazy initialization capability
BTo provide enterprise features like AOP and transaction management
CTo store all bean metadata in an internal registry
DTo enforce naming conventions for beans
Correct Answer:
A. To create and manage bean instances with lazy initialization capability
EXPLANATION
BeanFactory is the core interface for accessing beans, providing lazy initialization. ApplicationContext extends BeanFactory with additional enterprise features.
In Spring Data JPA, what does the @Repository annotation signify?
AIt marks a class as a data access object and enables exception translation
BIt automatically generates SQL queries from method names
CIt configures the database connection pool
DIt enables caching for database queries
Correct Answer:
A. It marks a class as a data access object and enables exception translation
EXPLANATION
@Repository marks a class as a data access component and enables Spring to translate persistence-specific exceptions into Spring's DataAccessException hierarchy.
What is the role of PropertySource annotation in Spring Framework?
ATo define external property files and load their values into the environment
BTo create new properties dynamically at runtime
CTo encrypt sensitive properties automatically
DTo validate property values against defined constraints
Correct Answer:
A. To define external property files and load their values into the environment
EXPLANATION
@PropertySource allows loading properties from external files (like .properties or .yml) into the Spring Environment for use throughout the application.
Consider a scenario where you have two bean implementations of the same interface. How would you specify which one to inject using Spring annotations?
AUse @Primary on one bean and @Autowired on the injection point
BUse @Qualifier annotation with the bean name at the injection point
CUse @Autowired with @Primary or @Qualifier; both approaches work
DUse @Bean with explicit configuration in @Configuration class
Correct Answer:
C. Use @Autowired with @Primary or @Qualifier; both approaches work
EXPLANATION
Both @Primary (default choice) and @Qualifier (explicit selection by name) can resolve bean ambiguity. @Qualifier is more explicit and takes precedence over @Primary.