Dependency Injection — Concept Check
Questions on Dependency Injection. Spring's DI container wires your application together. Understanding @Component stereotypes, @Bean factory methods, @Qualifier disambiguation, and scope is essential for every Spring developer.
Questions in this quiz
8 questions on Dependency Injection — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer notices that their Spring application is throwing SQLExceptions instead of DataAccessExceptions. What might be the cause?
- AThe @Controller annotation is used instead of @Repository.
- BThe @Repository annotation is missing on the data access layer.
- CThe @Service annotation is used instead of @Repository.
- DThe @Component annotation is used instead of @Service.
2.In a Spring application, two beans implement the PaymentService interface. Without any additional configuration, which bean will be injected by default?
- AThe bean with the shortest name.
- BThe bean defined first in the configuration.
- CThe bean annotated with @Qualifier.
- DThe bean annotated with @Primary.
3.A developer uses @Component on a class but notices it doesn't behave like a @Repository. Why might this be?
- A@Component does not enable persistence exception translation.
- B@Component is only for web layer components.
- C@Component is not a stereotype annotation.
- D@Component requires additional configuration to work.
4.A Spring application has a bean with prototype scope. What happens when it is injected multiple times?
- AA new instance is created each time it is injected.
- BThe same instance is shared across all injections.
- CIt behaves like a singleton in a web context.
- DIt is only instantiated once per HTTP request.
5.A developer wants to register a third-party class as a Spring bean. Which approach should they use?
- AUse @Service to register it as a business logic component.
- BAnnotate the class with @Component.
- CAdd it directly to the Spring context without annotations.
- DDefine it in a @Configuration class with a @Bean method.
6.A developer configures a bean with @ConditionalOnProperty but it doesn't load. What could be the reason?
- AThe bean is not a singleton.
- BThe required property is not set in the environment.
- CThe bean is missing a @Component annotation.
- DThe property is set to 'false' in the environment.
7.What is the default scope of a Spring bean if not specified?
- ASession
- BSingleton
- CRequest
- DPrototype
8.A developer uses @Qualifier to inject a specific bean but encounters a NoSuchBeanDefinitionException. What might be the issue?
- AThe bean is defined with a different scope.
- BThe @Primary annotation is missing on the bean.
- CThe bean is not defined in the same package.
- DThe specified bean name does not match any existing bean.