B. message property should be initialized in constructor
C. Missing import of OnInit interface
D. No error, code is correct
Solution
Step 1: Check for required imports
The component implements OnInit but does not import it from '@angular/core'. This causes a compile error.
Step 2: Verify method and property usage
The method name ngOnInit is correct, and initializing message in ngOnInit is valid.
Final Answer:
Missing import of OnInit interface -> Option C
Quick Check:
Implementing OnInit requires importing it [OK]
Hint: Always import lifecycle interfaces from '@angular/core' [OK]
Common Mistakes:
Forgetting to import OnInit
Renaming ngOnInit incorrectly
Thinking properties must initialize in constructor
5. You want to fetch user data from a server right after your Angular component is created. Where should you place the fetch call to ensure it runs once after initialization?
hard
A. Inside the constructor
B. Inside the ngOnInit method
C. Inside the ngOnDestroy method
D. Inside a button click event handler
Solution
Step 1: Understand lifecycle timing
The constructor runs before Angular sets up the component fully, so it's not ideal for async tasks like fetching data.
Step 2: Use ngOnInit for initialization tasks
ngOnInit runs after component creation and is the recommended place to start fetching data once.
Step 3: Exclude other options
ngOnDestroy runs when component is destroyed, and button click handlers run on user action, not automatically.
Final Answer:
Inside the ngOnInit method -> Option B
Quick Check:
Fetch data in ngOnInit for one-time setup [OK]
Hint: Fetch data in ngOnInit, not constructor [OK]
Common Mistakes:
Fetching data in constructor causing timing issues
Using ngOnDestroy which runs too late
Waiting for user click instead of auto-fetch
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.