Skip to content
Sharp

Dependency Injection interview questions

Constructor vs Field Injection

26 questions with answers and explanations · 25 flashcards

Questions

  1. Why might you prefer constructor injection over field injection when unit testing?Multiple choiceMedium
  2. In Spring Boot, how can you ensure that a bean is eagerly initialized at startup?Multiple choiceHard
  3. What advantage does constructor injection offer over field injection concerning encapsulation?Multiple choiceMedium
  4. In Spring Boot, how does lazy initialization of beans affect constructor injection?Multiple choiceHard
  5. How does Spring Boot handle dependency injection when multiple beans match the type being injected?Multiple choiceMedium
  6. Which scope of a bean in Spring Boot is the default in Java?Multiple choiceEasy
  7. Why do teams use the @Autowired annotation in Spring Boot in production code?Multiple choiceEasy
  8. Why is constructor injection preferred over field injection in terms of immutability?Multiple choiceMedium
  9. Why is field injection a design problem?Multiple choiceMedium
  10. In Spring Boot, what is the effect of marking a bean as @Primary?Multiple choiceMedium
  11. How does using 'final' with constructor-injected dependencies in Spring Boot help during development?Multiple choiceMedium
  12. In the context of Spring Boot, what is a potential downside of self-invocation in transaction management?Multiple choiceHard
  13. What role does the Spring IoC container play in dependency injection?Multiple choiceMedium
  14. In Spring Boot, what role does the ApplicationContext play in dependency injection?Multiple choiceMedium
  15. How would you explain a key advantage of using Spring Boot over traditional Java applications for dependency injection in an interview?Multiple choiceEasy
  16. What issue might arise from using self-invocation in a transactional method within a Spring Boot application?Multiple choiceHard
  17. Why can using field injection with private fields in a Spring Boot application become a problem in practice?Multiple choiceEasy
  18. Why is constructor injection preferred for mandatory dependencies?Multiple choiceMedium
  19. How would you explain a key benefit of using constructor injection with 'final' fields in Spring Boot in an interview?Multiple choiceEasy
  20. Why is constructor injection generally preferred over field injection in Spring Boot applications?Multiple choiceEasy
  21. What issue might arise with field injection using @Autowired on private fields in a Spring Boot application?Multiple choiceMedium
  22. What impact does using constructor injection have on unit testing in Spring Boot applications?Multiple choiceHard
  23. Which of the following is a benefit of using 'final' with constructor-injected dependencies?Multiple choiceMedium
  24. Why is using @Autowired on constructor parameters recommended in Spring Boot?Multiple choiceEasy
  25. In Spring Boot, what is a potential problem when using field injection with the @Autowired annotation?Multiple choiceMedium
  26. How does Spring Boot handle bean instantiation order when using constructor injection?Multiple choiceHard