Java Programming — Spring Framework Basics
Java OOP, collections, multithreading
58 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 58 questions in Spring Framework Basics
In a multi-threaded Spring application with singleton beans, what thread-safety consideration must be kept in mind?
A Singleton beans are automatically thread-safe by Spring
B Developers must ensure singleton bean implementations are thread-safe
C Use synchronized keyword on all methods in singleton beans
D Convert singleton beans to prototype scope for thread safety
Correct Answer:  B. Developers must ensure singleton bean implementations are thread-safe
EXPLANATION

Spring doesn't enforce thread-safety for singleton beans. Developers must implement thread-safe code, use immutable objects, or utilize synchronization where necessary.

Take Test
Which of the following best describes the purpose of Spring's Stereotype annotations (@Service, @Repository)?
A They improve code execution performance
B They mark classes for component scanning and provide semantic meaning
C They enforce specific design patterns
D They are purely optional and provide no functional benefit
Correct Answer:  B. They mark classes for component scanning and provide semantic meaning
EXPLANATION

Stereotype annotations (@Service, @Repository, @Controller) enable classpath component scanning and convey the architectural role of the class.

Take Test
What is the purpose of using @Bean method parameters in Spring @Configuration classes?
A To inject dependencies into the bean being created
B To pass configuration values from properties files
C To define bean relationships through method parameters
D Both A and C are correct
Correct Answer:  D. Both A and C are correct
EXPLANATION

Method parameters in @Bean methods allow Spring to inject dependent beans and establish bean relationships, enabling dependency injection at bean creation time.

Take Test
In Spring Framework 6.0, what is the role of the @EnableAutoConfiguration annotation in Spring Boot?
A It enables automatic bean registration based on classpath contents
B It disables all manual bean configuration
C It is replaced by @SpringBootApplication in modern versions
D Both A and C are correct
Correct Answer:  D. Both A and C are correct
EXPLANATION

@EnableAutoConfiguration (or @SpringBootApplication which includes it) auto-configures Spring beans based on jar dependencies on the classpath.

Take Test
What does the @Scope annotation with value 'prototype' mean in Spring Framework?
A A new bean instance is created for each request
B The bean is shared across the entire application
C The bean is created once and reused
D The bean is never instantiated
Correct Answer:  A. A new bean instance is created for each request
EXPLANATION

Prototype scope creates a new bean instance each time it's requested, unlike singleton which creates only one instance for the entire application.

Take Test
Advertisement
Consider a Spring application where you need different bean implementations based on the deployment environment. Which approach is most suitable?
A Use @Profile annotation on bean definitions
B Use @Conditional annotation with custom conditions
C Use @ConditionalOnProperty for property-based selection
D All of the above are valid approaches
Correct Answer:  D. All of the above are valid approaches
EXPLANATION

@Profile is for environment-based activation, @Conditional for custom logic, and @ConditionalOnProperty for property-driven bean registration. Choice depends on specific requirements.

Take Test
What is the significance of the @Qualifier annotation when used alongside @Autowired in Spring?
A It improves performance of bean injection
B It specifies which bean to inject when multiple candidates exist
C It marks a bean as high quality
D It reduces memory consumption
Correct Answer:  B. It specifies which bean to inject when multiple candidates exist
EXPLANATION

@Qualifier resolves ambiguity in autowiring by specifying the exact bean name or custom qualifier to inject when multiple beans of the same type exist.

Take Test
In a complex Spring application, when would you prefer to use ObjectFactory over direct bean injection?
A To get a new instance of the bean each time it's accessed
B To defer bean creation until it's actually needed
C To break circular dependencies between beans
D All of the above scenarios
Correct Answer:  D. All of the above scenarios
EXPLANATION

ObjectFactory enables lazy initialization, provides new instances for prototype beans, and can help resolve circular dependencies through deferred access.

Take Test
What is the primary difference between BeanFactory and ApplicationContext in Spring?
A ApplicationContext is a subinterface of BeanFactory with additional features
B BeanFactory is more feature-rich than ApplicationContext
C They are completely different unrelated interfaces
D ApplicationContext only works with XML configuration
Correct Answer:  A. ApplicationContext is a subinterface of BeanFactory with additional features
EXPLANATION

ApplicationContext extends BeanFactory and adds features like event publishing, message resource handling, and easier AOP integration.

Take Test
Which annotation is used to provide external configuration properties to Spring beans in the 2024 Spring Boot version?
A @PropertySource
B @ConfigurationProperties
C @Value
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

@PropertySource loads properties from files, @ConfigurationProperties binds properties to bean properties, and @Value injects individual values. All are used for configuration.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips