Java Programming
Java OOP, collections, multithreading
212 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 212 questions
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
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
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
In Spring framework, what is the purpose of FactoryBean interface?
A To create complex bean instances with custom initialization logic
B To define factory methods for creating beans
C To implement the Factory design pattern
D To manage bean lifecycle events
Correct Answer:  A. To create complex bean instances with custom initialization logic
EXPLANATION

FactoryBean allows creating beans with sophisticated initialization logic through getObject() method, useful for complex object creation.

Take Test
Advertisement
Consider a Spring application where method execution time tracking is required across multiple methods. Which approach is most appropriate?
A Using Spring AOP with custom aspect to track execution time
B Adding timing logic manually in each method
C Using @Timed annotation from Spring Actuator
D Implementing a custom servlet filter
Correct Answer:  A. Using Spring AOP with custom aspect to track execution time
EXPLANATION

Spring AOP with a custom aspect provides centralized, reusable cross-cutting concern implementation without code duplication in individual methods.

Take Test
An enterprise application requires different bean implementations based on runtime conditions. Which Spring feature is most suitable?
A @Conditional annotation with custom Condition implementations
B @Profile annotation for environment-specific beans
C Factory pattern with factory beans
D All of the above can be used depending on the scenario
Correct Answer:  D. All of the above can be used depending on the scenario
EXPLANATION

@Conditional provides fine-grained control, @Profile handles environment-specific beans, and factory beans offer explicit creation logic. Choice depends on use case requirements.

Take Test
How does Spring handle circular dependency between two beans?
A Automatically resolves it using setter injection for one of the beans
B Throws BeanCurrentlyInCreationException at startup
C Silently ignores one of the dependencies
D Uses a proxy pattern to defer initialization of one bean
Correct Answer:  A. Automatically resolves it using setter injection for one of the beans
EXPLANATION

Spring can resolve circular dependencies when at least one injection is done via setter method rather than constructor, allowing lazy initialization.

Take Test
A microservices architecture uses Spring with circular dependency between ServiceA and ServiceB. What is the best solution?
A Increase heap memory to handle circular references
B Refactor to introduce a third service or use setter injection with @Lazy
C Use @Autowired on both constructor and setter
D Switch to manual bean management
Correct Answer:  B. Refactor to introduce a third service or use setter injection with @Lazy
EXPLANATION

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.

Take Test
In Spring Framework 2024-25, which feature allows lazy initialization of beans?
A @Lazy annotation on @Bean or @Component
B @DeferredInit annotation
C Setting lazy-init='true' only in XML
D Spring automatically detects and lazily initializes all beans
Correct Answer:  A. @Lazy annotation on @Bean or @Component
EXPLANATION

@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.

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