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.6Easy
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.7Easy
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.8Easy
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.9Easy
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.10Easy
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.11Easy
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.
Q.12Easy
What is the primary purpose of the ApplicationContext in Spring Framework?
Answer: B
ApplicationContext is the core container that instantiates, configures, and manages beans. It also resolves dependencies and manages bean lifecycle.
Q.13Easy
Which of the following is NOT a valid Spring Bean scope in Spring Framework 2024?
Answer: D
Spring supports singleton, prototype, request, session, application, and websocket scopes. 'Eternal' is not a valid scope in Spring Framework.
Q.14Easy
What does the @Scope annotation with value 'prototype' mean in Spring Framework?
Answer: A
Prototype scope creates a new bean instance each time it's requested, unlike singleton which creates only one instance for the entire application.
Q.15Easy
Which of the following best describes the purpose of Spring's Stereotype annotations (@Service, @Repository)?
Answer: B
Stereotype annotations (@Service, @Repository, @Controller) enable classpath component scanning and convey the architectural role of the class.