Which annotation is used to mark a class as a Spring Bean in the latest Spring Framework?
Answer: B
@Component is a generic stereotype annotation for any Spring-managed component. @Bean is used on methods, while @Service and @Controller are specialized versions of @Component.
Q.2Easy
What is the primary purpose of Dependency Injection (DI) in Spring Framework?
Answer: B
DI reduces coupling by injecting dependencies rather than creating them internally, making code more testable and maintainable.
Q.3Easy
Which of the following is NOT a valid Spring bean scope in Spring 5.x+?
Answer: D
Valid scopes are singleton, prototype, request, session, application, and websocket. 'eternal' is not a valid scope.
Q.4Easy
What does @Autowired annotation do in Spring?
Answer: A
@Autowired performs type-based dependency injection. Spring looks for a bean of the required type and injects it automatically.
Q.5Easy
Which XML element is used to define a bean in Spring XML configuration?
Answer: B
The <bean> element is the standard XML element used in applicationContext.xml to define Spring beans.
Advertisement
Q.6Medium
A Spring application requires constructor-based dependency injection for a mandatory dependency. Which approach is best?
Answer: B
Constructor-based DI ensures mandatory dependencies are provided at object creation time and is considered a best practice for required dependencies.
Q.7Medium
What is the default scope of a Spring Bean?
Answer: C
By default, Spring creates beans as singletons - one instance per application context, shared across the entire application.
Q.8Medium
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.9Medium
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.10Medium
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.11Medium
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.12Medium
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.
Q.13Medium
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.14Medium
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.15Medium
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.16Hard
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.17Hard
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.18Hard
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.19Hard
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.20Easy
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.