@Configuration & @Bean — Concept Check
Questions on @Configuration & @Bean. @Configuration marks a class as a source of bean definitions; @Bean methods create and configure objects that are managed by the Spring container.
Questions in this quiz
8 questions on @Configuration & @Bean — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a Spring configuration class with @Configuration annotation. What happens if you call a @Bean method from another @Bean method in the same class?
- AThe same singleton bean instance is returned.
- BThe method call is ignored by Spring.
- CA new instance of the bean is created each time.
- DAn exception is thrown due to circular dependency.
2.In a Spring application, you notice that each injection point receives a new instance of a bean. What might be the cause?
- AThe bean method is not annotated with @Bean.
- BThe bean is defined with @Scope("prototype").
- CThe bean is missing a @Configuration annotation.
- DThe bean is defined with @Scope("singleton").
3.A developer claims that using @Component instead of @Configuration will still ensure singleton beans. What is the issue with this statement?
- A@Component requires explicit bean names.
- B@Component cannot be used to define beans.
- C@Component automatically sets the scope to prototype.
- D@Component does not ensure singleton semantics for inter-bean calls.
4.What is the default scope of a bean defined with @Bean annotation?
- ARequest
- BSession
- CSingleton
- DPrototype
5.In a Spring Boot application, a bean is not registered when expected. The bean has @ConditionalOnClass annotation. What might be the reason?
- AThe bean method has no parameters.
- BThe bean method is not annotated with @Bean.
- CThe bean is defined in a @Component class.
- DThe required class is not on the classpath.
6.A developer uses @Bean(name="customBean") to define a bean. How can this affect the bean's registration?
- AThe bean is registered as a prototype by default.
- BThe bean is registered with a default name.
- CThe bean is registered with the name 'customBean'.
- DThe bean is not registered due to naming conflict.
7.Why might a Spring application use @ConditionalOnProperty for a bean definition?
- ATo register the bean without any dependencies.
- BTo automatically set the bean's scope to prototype.
- CTo register the bean only if a specific property is set.
- DTo ensure the bean is always a singleton.
8.If a bean is defined with @ConditionalOnMissingBean, when will it be registered?
- AOnly if explicitly injected.
- BAlways, regardless of other beans.
- COnly if another bean of the same type is registered.
- DOnly if no other bean of the same type is registered.