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.42Easy
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.43Medium
In Spring Framework, what does the @Autowired annotation do when autowiring candidates are ambiguous?
Answer: D
When multiple beans match the injection point, Spring throws an exception unless @Primary is used to indicate the preferred bean or @Qualifier specifies which bean to inject.
Q.44Medium
What is the difference between @Bean and @Component annotations in Spring?
Answer: B
@Component is applied to classes making them beans automatically. @Bean is applied to methods in @Configuration classes, giving explicit control over bean creation.
Q.45Medium
Consider a scenario where a Spring application needs to initialize a database connection pool at startup. Which method should be used?
Answer: D
Spring provides multiple mechanisms for initialization: @PostConstruct, InitializingBean interface, and initMethod in @Bean definition. All are equally valid.
Advertisement
Q.46Medium
What is the role of Spring's PropertyPlaceholderConfigurer in application configuration?
Answer: B
PropertyPlaceholderConfigurer (or @PropertySource in modern Spring) enables externalization of configuration by replacing ${property.name} placeholders with actual values from properties files.
Q.47Medium
In Spring Framework, which interface represents a factory pattern implementation for creating objects?
Answer: D
BeanFactory is the basic container, ObjectFactory is for lazy bean retrieval, and FactoryBean allows custom bean creation logic. All represent factory patterns in Spring.
Q.48Medium
What happens when @Lazy annotation is applied to a singleton bean in Spring?
Answer: A
@Lazy defers bean instantiation until it's first requested, overriding the default eager initialization behavior of singleton beans.
Q.49Medium
Which annotation is used to provide external configuration properties to Spring beans in the 2024 Spring Boot version?
Answer: D
@PropertySource loads properties from files, @ConfigurationProperties binds properties to bean properties, and @Value injects individual values. All are used for configuration.
Q.50Hard
What is the primary difference between BeanFactory and ApplicationContext in Spring?
Answer: A
ApplicationContext extends BeanFactory and adds features like event publishing, message resource handling, and easier AOP integration.
Q.51Hard
In a complex Spring application, when would you prefer to use ObjectFactory<T> over direct bean injection?
Answer: D
ObjectFactory enables lazy initialization, provides new instances for prototype beans, and can help resolve circular dependencies through deferred access.
Q.52Medium
What is the significance of the @Qualifier annotation when used alongside @Autowired in Spring?
Answer: B
@Qualifier resolves ambiguity in autowiring by specifying the exact bean name or custom qualifier to inject when multiple beans of the same type exist.
Q.53Hard
Consider a Spring application where you need different bean implementations based on the deployment environment. Which approach is most suitable?
Answer: D
@Profile is for environment-based activation, @Conditional for custom logic, and @ConditionalOnProperty for property-driven bean registration. Choice depends on specific requirements.
Q.54Easy
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.55Medium
In Spring Framework 6.0, what is the role of the @EnableAutoConfiguration annotation in Spring Boot?
Answer: D
@EnableAutoConfiguration (or @SpringBootApplication which includes it) auto-configures Spring beans based on jar dependencies on the classpath.
Q.56Medium
What is the purpose of using @Bean method parameters in Spring @Configuration classes?
Answer: D
Method parameters in @Bean methods allow Spring to inject dependent beans and establish bean relationships, enabling dependency injection at bean creation time.
Q.57Easy
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.
Q.58Hard
In a multi-threaded Spring application with singleton beans, what thread-safety consideration must be kept in mind?
Answer: B
Spring doesn't enforce thread-safety for singleton beans. Developers must implement thread-safe code, use immutable objects, or utilize synchronization where necessary.