Bird
0
0

Given this Spring bean code snippet, what will be printed when the bean is initialized?

medium📝 component behavior Q13 of 15
Spring Boot - Inversion of Control and Dependency Injection
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");
  }
}
ABean destroyed
BNo output
CBean initialized
DCompilation error
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes