Which of the following best describes IoC (Inversion of Control) in Spring?
Answer: A
IoC is a principle where the Spring Container manages object creation and lifecycle, not the application code itself.
Q.902Medium
A developer needs to inject a bean only if it exists, otherwise skip injection. Which annotation should be used?
Answer: B
@Autowired(required=false) allows optional dependency injection. If the bean doesn't exist, no error is thrown.
Q.903Medium
What is the role of ApplicationContext in Spring Framework?
Answer: B
ApplicationContext is the central Spring container that manages bean lifecycle, performs DI, and provides additional features like event publishing and message resolution.
Q.904Medium
Which annotation is used to define configuration classes in Spring that replace XML configuration?
Answer: C
@Configuration marks a class as a source of bean definitions. It's equivalent to an XML <beans> element and works with @Bean methods.
Q.905Medium
Consider a scenario where multiple beans of the same type exist. How can you specify which bean to inject?
Answer: B
@Qualifier specifies the bean name to inject when multiple candidates exist. @Primary can also be used to mark a preferred bean.
Advertisement
Q.906Medium
What is the difference between @Bean and @Component annotations?
Answer: C
@Component marks a class as a bean, while @Bean is used inside @Configuration classes to define beans through factory methods, providing more control.
Q.907Medium
A Spring Boot application starts with ClassPathXmlApplicationContext('application.xml'). What does this do?
Answer: A
ClassPathXmlApplicationContext loads Spring bean definitions from an XML file located in the classpath, allowing traditional XML-based configuration.
Q.908Medium
Which Spring concept ensures that the same bean instance is reused throughout the application lifecycle?
Answer: C
Singleton scope (default) creates one bean instance per application context that is shared and reused across the entire application.
Q.909Hard
An enterprise application needs different bean implementations based on environment (dev/prod). Which approach is most suitable?
Answer: C
@Profile allows defining beans for specific environments (dev, prod, test). Alternatively, @Conditional provides more granular control with custom conditions.
Q.910Hard
What happens when a prototype-scoped bean has a dependency on a singleton-scoped bean in Spring?
Answer: B
A prototype bean can depend on singleton beans without issues. Each prototype instance gets the same singleton instance injected. The reverse (singleton depending on prototype) is problematic.
Q.911Hard
In Spring Framework 2024-25, which feature allows lazy initialization of beans?
Answer: A
@Lazy annotation (available in modern Spring versions) marks beans for lazy initialization. It can be used on class level or with @Bean methods. XML also supports lazy-init attribute.
Q.912Hard
A microservices architecture uses Spring with circular dependency between ServiceA and ServiceB. What is the best solution?
Answer: B
Circular dependencies indicate design issues. Best solutions: refactor code, use setter injection (instead of constructor) combined with @Lazy, or introduce an intermediary service to break the cycle.
Q.913Easy
In Spring Framework, what is the primary purpose of the @Autowired annotation?
Answer: A
@Autowired is used for dependency injection, allowing Spring to automatically resolve and inject bean dependencies without explicit configuration.
Q.914Easy
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.915Easy
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.916Medium
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.917Medium
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.918Medium
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.
Q.919Medium
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.920Easy
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.