Skip to content
QuiznExam

Lifecycle Hooks in Angular

Test your knowledge of Angular lifecycle hooks, including ngOnInit, ngOnChanges, ngDoCheck, ngAfterContentInit, ngAfterContentChecked, ngAfterViewInit, ngAfterViewChecked, and ngOnDestroy.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What are lifecycle hooks in Angular?

    • A. Methods that allow a component to respond to important moments in its lifecycle
    • B. Functions used only for HTTP requests
    • C. Methods used only to create routes
    • D. CSS functions for styling components
    Show answer

    A. Methods that allow a component to respond to important moments in its lifecycle

    Angular lifecycle hooks let components and directives run code at specific stages of their creation, change detection, rendering, and destruction.

  2. 2. Which lifecycle hook is commonly used for initialization logic after Angular has initialized a component's input properties?

    • A. ngOnInit
    • B. ngOnDestroy
    • C. ngAfterViewChecked
    • D. ngDoCheck
    Show answer

    A. ngOnInit

    ngOnInit runs once after Angular has initialized the component's input properties.

  3. 3. Which lifecycle hook is called when one or more data-bound input properties change?

    • A. ngOnChanges
    • B. ngOnInit
    • C. ngAfterViewInit
    • D. ngOnDestroy
    Show answer

    A. ngOnChanges

    ngOnChanges runs when Angular detects changes to data-bound input properties and receives a SimpleChanges object describing those changes.

  4. 4. What does the ngOnChanges lifecycle hook receive as an argument?

    • A. SimpleChanges
    • B. ChangeDetector
    • C. ComponentRef
    • D. ViewChanges
    Show answer

    A. SimpleChanges

    ngOnChanges receives a SimpleChanges object containing information about the current and previous values of changed input properties.

  5. 5. Which lifecycle hook can be used to implement custom change detection logic?

    • A. ngDoCheck
    • B. ngOnInit
    • C. ngAfterContentInit
    • D. ngOnDestroy
    Show answer

    A. ngDoCheck

    ngDoCheck runs during Angular's change-detection process and can be used when custom change detection logic is required.

  6. 6. Which lifecycle hook runs once after Angular has initialized content projected into a component?

    • A. ngAfterContentInit
    • B. ngAfterViewInit
    • C. ngOnInit
    • D. ngAfterViewChecked
    Show answer

    A. ngAfterContentInit

    ngAfterContentInit runs once after Angular has completed initialization of the component's projected content.

  7. 7. Which lifecycle hook runs after Angular checks the content projected into a component?

    • A. ngAfterContentChecked
    • B. ngAfterContentInit
    • C. ngAfterViewInit
    • D. ngOnChanges
    Show answer

    A. ngAfterContentChecked

    ngAfterContentChecked runs after Angular checks the content projected into the component.

  8. 8. Which lifecycle hook runs once after Angular initializes a component's view and child views?

    • A. ngAfterViewInit
    • B. ngAfterContentInit
    • C. ngOnInit
    • D. ngDoCheck
    Show answer

    A. ngAfterViewInit

    ngAfterViewInit runs once after Angular has fully initialized the component's view and its child views.

  9. 9. Which lifecycle hook runs after Angular checks a component's view and child views?

    • A. ngAfterViewChecked
    • B. ngAfterViewInit
    • C. ngAfterContentInit
    • D. ngOnInit
    Show answer

    A. ngAfterViewChecked

    ngAfterViewChecked runs after Angular has completed a check of the component's view and child views.

  10. 10. Which lifecycle hook is used for cleanup before a component is destroyed?

    • A. ngOnDestroy
    • B. ngAfterDestroy
    • C. ngCleanup
    • D. ngDispose
    Show answer

    A. ngOnDestroy

    ngOnDestroy runs immediately before Angular destroys a component or directive and is commonly used for cleanup.

  11. 11. Which task is commonly appropriate to perform in ngOnDestroy?

    • A. Cleaning up subscriptions or event listeners
    • B. Initializing component inputs
    • C. Creating the component's template
    • D. Defining route parameters
    Show answer

    A. Cleaning up subscriptions or event listeners

    ngOnDestroy is commonly used to release resources such as subscriptions, timers, or event listeners before a component is destroyed.

  12. 12. How many times does ngOnInit normally run for a component instance?

    • A. Once
    • B. Every time change detection runs
    • C. Every time the component receives an event
    • D. Only when the component is destroyed
    Show answer

    A. Once

    ngOnInit runs once after Angular has initialized the component's input properties.

  13. 13. Which lifecycle hook is especially useful when you need access to view-related queries after the view has been initialized?

    • A. ngAfterViewInit
    • B. ngOnChanges
    • C. ngDoCheck
    • D. ngOnDestroy
    Show answer

    A. ngAfterViewInit

    ngAfterViewInit runs after the component's view and child views have been initialized, making it useful for view-related initialization.

  14. 14. Which lifecycle hook can run many times during the lifetime of a component?

    • A. ngAfterViewChecked
    • B. ngOnInit
    • C. ngAfterViewInit
    • D. ngAfterContentInit
    Show answer

    A. ngAfterViewChecked

    ngAfterViewChecked can run repeatedly whenever Angular performs checks on the component's view.

  15. 15. Which sequence best represents the major lifecycle stages of an Angular component?

    • A. Initialization, change detection, view/content checks, and destruction
    • B. Compilation, database creation, routing, and deployment
    • C. Routing, HTTP request, CSS compilation, and deletion
    • D. Template creation, database migration, authentication, and logout
    Show answer

    A. Initialization, change detection, view/content checks, and destruction

    Angular components move through stages that include initialization, change detection and view/content checks, followed eventually by destruction.