0
0
Spring Bootframework~20 mins

@PostConstruct and @PreDestroy in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
When is a method annotated with @PostConstruct called?
In a Spring Boot application, what is the correct time when a method annotated with @PostConstruct is executed?
ARight after the bean is fully initialized and dependencies are injected, before the bean is ready to use.
BOnly when the application context is closed.
CAfter the bean is destroyed and removed from the context.
DBefore the bean is created and dependencies are injected.
Attempts:
2 left
💡 Hint
Think about when you want to run setup code after all dependencies are ready.
component_behavior
intermediate
2:00remaining
What happens when a method is annotated with @PreDestroy?
In a Spring Boot bean, when is a method annotated with @PreDestroy executed?
AAfter the bean is fully initialized and ready to use.
BWhen the bean is created and dependencies are injected.
CWhen the application starts.
DRight before the bean is destroyed and removed from the Spring context.
Attempts:
2 left
💡 Hint
Think about cleanup tasks before the bean disappears.
📝 Syntax
advanced
2:30remaining
Identify the correct usage of @PostConstruct in a Spring Boot bean
Which of the following code snippets correctly uses @PostConstruct in a Spring Boot component?
A
public class MyBean {
  @PostConstruct
  public void init(String param) {
    System.out.println(param);
  }
}
B
public class MyBean {
  @PostConstruct
  private int init() {
    return 1;
  }
}
C
public class MyBean {
  @PostConstruct
  public void init() {
    System.out.println("Bean initialized");
  }
}
D
public class MyBean {
  @PostConstruct
  public static void init() {
    System.out.println("Static init");
  }
}
Attempts:
2 left
💡 Hint
Remember the method must be void, no parameters, and non-static.
🔧 Debug
advanced
2:30remaining
Why does the @PreDestroy method not run in this Spring Boot application?
Consider this Spring Boot bean: public class MyService { @PreDestroy public void cleanup() { System.out.println("Cleaning up resources"); } } Why might the cleanup method never be called when the application stops?
AThe bean is not managed by Spring because it lacks a stereotype annotation like @Component.
B@PreDestroy methods only run on singleton beans, and this bean is prototype scoped.
CThe application context is not closed properly, so @PreDestroy is skipped.
DThe method is public and void, so it should run without issues.
Attempts:
2 left
💡 Hint
Check if Spring knows about this bean to manage its lifecycle.
🧠 Conceptual
expert
3:00remaining
What is the effect of multiple @PostConstruct methods in a single Spring bean?
If a Spring bean class has two methods both annotated with @PostConstruct, what will happen when the bean is initialized?
AOnly the first declared @PostConstruct method will be called; the second is ignored.
BBoth methods will be called in an undefined order during bean initialization.
CSpring will throw a runtime exception due to multiple @PostConstruct methods.
DNeither method will be called because of the conflict.
Attempts:
2 left
💡 Hint
Think about how Java handles multiple lifecycle callbacks in one class.