Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following best describes the lifecycle of a Spring bean?
easy
A. Creation, initialization, use, and destruction
B. Compilation, execution, and garbage collection
C. Declaration, inheritance, and polymorphism
D. Loading, linking, and running

Solution

  1. Step 1: Understand Spring bean lifecycle phases

    Spring beans go through creation, initialization, usage, and destruction phases during their lifecycle.
  2. Step 2: Compare options with lifecycle phases

    Only Creation, initialization, use, and destruction correctly lists these phases. Other options describe unrelated programming concepts.
  3. Final Answer:

    Creation, initialization, use, and destruction -> Option A
  4. Quick Check:

    Bean lifecycle = Creation, init, use, destroy [OK]
Hint: Remember bean lifecycle phases: create, init, use, destroy [OK]
Common Mistakes:
  • Confusing bean lifecycle with Java program lifecycle
  • Mixing bean lifecycle with object-oriented concepts
  • Thinking lifecycle only involves creation and destruction
2. Which annotation is used in Spring to run a method after a bean's properties have been set?
easy
A. @PostConstruct
B. @PreDestroy
C. @Autowired
D. @Bean

Solution

  1. Step 1: Identify annotation for post-initialization

    @PostConstruct is the annotation used to mark a method to run after bean properties are set.
  2. Step 2: Eliminate other annotations

    @PreDestroy runs before destruction, @Autowired injects dependencies, and @Bean defines a bean method.
  3. Final Answer:

    @PostConstruct -> Option A
  4. Quick Check:

    @PostConstruct runs after properties set [OK]
Hint: PostConstruct runs after init, PreDestroy before destroy [OK]
Common Mistakes:
  • Confusing @PreDestroy with @PostConstruct
  • Using @Autowired for lifecycle methods
  • Thinking @Bean runs after initialization
3. Given this Spring bean code snippet, what will be printed when the bean is initialized?
@Component
public class MyBean {
  @PostConstruct
  public void init() {
    System.out.println("Bean initialized");
  }
}
medium
A. Bean destroyed
B. No output
C. Bean initialized
D. Compilation error

Solution

  1. Step 1: Understand @PostConstruct behavior

    The method annotated with @PostConstruct runs after the bean is created and properties set, printing "Bean initialized".
  2. Step 2: Check output possibilities

    Since the method prints a message on init, output will be "Bean initialized". No errors or destruction messages occur here.
  3. Final Answer:

    Bean initialized -> Option C
  4. Quick Check:

    @PostConstruct prints "Bean initialized" [OK]
Hint: PostConstruct method runs once after bean creation [OK]
Common Mistakes:
  • Expecting output on bean destruction instead
  • Thinking no output occurs without explicit call
  • Confusing syntax causing compilation errors
4. What is wrong with this Spring bean code if the cleanup() method is never called?
@Component
public class MyBean {
  public void cleanup() {
    System.out.println("Cleaning up");
  }
}
medium
A. cleanup method should be static
B. Missing @PreDestroy annotation on cleanup method
C. Missing @PostConstruct annotation on cleanup method
D. cleanup method must return a value

Solution

  1. Step 1: Identify lifecycle annotation for destruction

    The cleanup method should be annotated with @PreDestroy to run before bean destruction.
  2. Step 2: Check other options for correctness

    @PostConstruct is for initialization, static or return value are not required for lifecycle methods.
  3. Final Answer:

    Missing @PreDestroy annotation on cleanup method -> Option B
  4. Quick Check:

    @PreDestroy needed to run cleanup before destroy [OK]
Hint: Use @PreDestroy to run code before bean destruction [OK]
Common Mistakes:
  • Using @PostConstruct instead of @PreDestroy for cleanup
  • Making cleanup static unnecessarily
  • Expecting cleanup to run without annotation
5. You want to ensure a Spring bean runs a method both after initialization and before destruction. Which combination correctly achieves this?
hard
A. Implement InitializingBean interface only
B. Annotate the same method with both @PostConstruct and @PreDestroy
C. Use @Autowired on init method and @PreDestroy on destroy method
D. Annotate one method with @PostConstruct and another with @PreDestroy

Solution

  1. Step 1: Understand lifecycle annotations usage

    @PostConstruct runs after initialization, @PreDestroy runs before destruction; they must be on separate methods.
  2. Step 2: Evaluate other options

    Annotating the same method with both annotations is invalid. @Autowired is for injection, not lifecycle. Implementing InitializingBean interface only handles post-initialization but not destruction.
  3. Final Answer:

    Annotate one method with @PostConstruct and another with @PreDestroy -> Option D
  4. Quick Check:

    Separate methods with @PostConstruct and @PreDestroy [OK]
Hint: Use separate methods for init (@PostConstruct) and destroy (@PreDestroy) [OK]
Common Mistakes:
  • Trying to use one method for both init and destroy
  • Confusing @Autowired with lifecycle annotations
  • Using InitializingBean interface only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard