0
0
Spring Bootframework~20 mins

Bean lifecycle overview in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Bean Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the correct order of Spring Bean lifecycle phases?
Arrange the following Spring Bean lifecycle phases in the correct order from bean creation to destruction.
A1,2,3,4
B2,1,3,4
C1,3,2,4
D1,2,4,3
Attempts:
2 left
💡 Hint
Think about what happens first: creating the bean, then setting its dependencies, then running any setup code, and finally cleaning up.
component_behavior
intermediate
2:00remaining
What happens if a Spring Bean implements InitializingBean?
Consider a Spring Bean that implements the InitializingBean interface. What method is called and when during the bean lifecycle?
AThe constructor is called after the bean is fully initialized.
BThe destroy() method is called before the bean is destroyed.
CThe init() method is called before dependency injection.
DThe afterPropertiesSet() method is called after dependency injection is complete.
Attempts:
2 left
💡 Hint
Look for the method that runs after all properties are set.
lifecycle
advanced
2:00remaining
What error occurs if a Spring Bean's @PostConstruct method throws an exception?
If a method annotated with @PostConstruct in a Spring Bean throws a RuntimeException, what is the effect on the application context startup?
AThe exception is ignored and the bean is created normally.
BThe application context fails to start and throws a BeanCreationException.
CThe bean is created but marked as lazy and initialized later.
DThe exception is logged but the application continues without the bean.
Attempts:
2 left
💡 Hint
Think about what happens if initialization code fails during startup.
state_output
advanced
2:00remaining
What is the output of this Spring Bean lifecycle log?
Given a Spring Bean with methods logging messages in constructor, @PostConstruct, and @PreDestroy, what is the order of log messages when the application starts and then shuts down?
Spring Boot
public class MyBean {
  public MyBean() { System.out.println("Constructor called"); }
  @PostConstruct
  public void init() { System.out.println("PostConstruct called"); }
  @PreDestroy
  public void cleanup() { System.out.println("PreDestroy called"); }
}
AConstructor called\nPostConstruct called\nPreDestroy called
BPostConstruct called\nConstructor called\nPreDestroy called
CConstructor called\nPreDestroy called\nPostConstruct called
DPreDestroy called\nConstructor called\nPostConstruct called
Attempts:
2 left
💡 Hint
Remember the bean is created first, then initialized, then destroyed.
🔧 Debug
expert
2:00remaining
Why does this Spring Bean's @PreDestroy method not run on shutdown?
A Spring Bean has a method annotated with @PreDestroy, but when the application stops, the method is never called. What is the most likely cause?
Spring Boot
@Component
public class MyBean {
  @PreDestroy
  public void cleanup() {
    System.out.println("Cleaning up");
  }
}
AThe @PreDestroy annotation is misspelled and ignored by Spring.
BThe cleanup method is private and cannot be called by Spring.
CThe bean is not managed by Spring because it was created with new instead of injected.
DThe application context is not closed properly, so destroy callbacks are skipped.
Attempts:
2 left
💡 Hint
Think about how Spring knows which beans to manage and call lifecycle methods on.