Which of the following correctly describes the lifecycle of a singleton-scoped Spring bean?
Answer: B
Singleton beans are instantiated once per Spring container and reused for the application's lifetime, making them memory-efficient for stateless objects.
Q.22Easy
What is the difference between @Component and @Service annotations in Spring?
Answer: A
@Component is a generic stereotype, while @Service is a specialized annotation indicating the class contains business logic, improving code readability.
Q.23Medium
In a Spring Boot application, what does the @SpringBootApplication annotation combine?
Answer: A
@SpringBootApplication is a convenience annotation that combines three key annotations for configuration, component scanning, and auto-configuration.
Q.24Medium
Consider a scenario where you have two bean implementations of the same interface. How would you specify which one to inject using Spring annotations?
Answer: C
Both @Primary (default choice) and @Qualifier (explicit selection by name) can resolve bean ambiguity. @Qualifier is more explicit and takes precedence over @Primary.
Q.25Medium
What is the role of PropertySource annotation in Spring Framework?
Answer: A
@PropertySource allows loading properties from external files (like .properties or .yml) into the Spring Environment for use throughout the application.
Advertisement
Q.26Medium
In Spring Data JPA, what does the @Repository annotation signify?
Answer: A
@Repository marks a class as a data access component and enables Spring to translate persistence-specific exceptions into Spring's DataAccessException hierarchy.
Q.27Easy
What is the primary advantage of using Spring Dependency Injection over manual object creation?
Answer: A
DI reduces tight coupling between classes, making code more maintainable, testable, and flexible for changes or mock implementations during testing.
Q.28Hard
How does Spring handle circular dependency between two beans?
Answer: A
Spring can resolve circular dependencies when at least one injection is done via setter method rather than constructor, allowing lazy initialization.
Q.29Medium
What is the function of the BeanFactory interface in Spring?
Answer: A
BeanFactory is the core interface for accessing beans, providing lazy initialization. ApplicationContext extends BeanFactory with additional enterprise features.
Q.30Medium
In Spring AOP, what does the term 'Join Point' refer to?
Answer: A
A Join Point is a candidate point in program execution (method call, exception throw) where advice can be applied. Pointcuts select specific Join Points.
Q.31Medium
What does the @Transactional annotation do in Spring?
Answer: A
@Transactional delegates transaction management to Spring, automatically committing on success and rolling back on exceptions, following ACID principles.
Q.32Hard
An enterprise application requires different bean implementations based on runtime conditions. Which Spring feature is most suitable?
Answer: D
@Conditional provides fine-grained control, @Profile handles environment-specific beans, and factory beans offer explicit creation logic. Choice depends on use case requirements.
Q.33Medium
What is the significance of the @ConfigurationProperties annotation in Spring Boot?
Answer: A
@ConfigurationProperties enables binding of external properties (from application.properties/yml) to POJO classes, providing type safety and validation.
Q.34Hard
Consider a Spring application where method execution time tracking is required across multiple methods. Which approach is most appropriate?
Answer: A
Spring AOP with a custom aspect provides centralized, reusable cross-cutting concern implementation without code duplication in individual methods.
Q.35Easy
What is the default behavior of Spring when multiple beans of the same type are available for autowiring?
Answer: A
Spring raises NoUniqueBeanDefinitionException when ambiguity exists. Use @Primary or @Qualifier to resolve the ambiguity.
Q.36Hard
In Spring framework, what is the purpose of FactoryBean interface?
Answer: A
FactoryBean allows creating beans with sophisticated initialization logic through getObject() method, useful for complex object creation.
Q.37Medium
What does the @EnableCaching annotation enable in a Spring application?
Answer: A
@EnableCaching activates Spring's caching infrastructure, allowing method-level caching through annotations like @Cacheable to store and retrieve results.
Q.38Medium
When would you use request-scoped beans in a Spring web application?
Answer: A
Request-scoped beans are instantiated per HTTP request and destroyed after request completion, ideal for storing user-specific or request-specific information.
Q.39Medium
What is the difference between constructor injection and setter injection in Spring?
Answer: A
Constructor injection enforces required dependencies and creates immutable objects; setter injection allows optional dependencies and partial initialization.
Q.40Easy
Which annotation is used to mark a class as a Spring Bean in the latest Spring Framework 6.0?
Answer: D
@Component, @Service, @Repository, and @Controller are all stereotype annotations that mark classes as Spring Beans. @Bean is used for method-level bean definition.