Angular Signals, Lifecycle, and State: Essentials Quiz Quiz

Test your knowledge of Angular Signals, component lifecycle, change detection, state management, and performance best practices. Perfect for candidates preparing for Angular interviews or developers seeking to reinforce key concepts in modern Angular development.

  1. Angular Signals Concept

    What is an Angular Signal as introduced in Angular v16+?

    1. A directive for styling elements dynamically
    2. A new reactive primitive that enables fine-grained reactivity without observables
    3. A class for routing between modules
    4. A function for handling HTTP requests

    Explanation: Angular Signals are a new reactive primitive in Angular v16+ that allow components to react to state changes automatically, without the need for observables. They help track dependencies and update values efficiently. The other options refer to unrelated Angular features: HTTP handling, styling directives, or routing classes, none of which define Signals.

  2. Signals State Update

    Which statement describes how Signals update state in Angular components?

    1. They cannot be used with template bindings
    2. They only re-render after a full page reload
    3. They require manual subscription and unsubscription
    4. They automatically track dependencies and re-evaluate when values change

    Explanation: Signals provide automatic dependency tracking and state updates, ensuring components react immediately when values change. Manual subscriptions are NOT required, unlike classic observables. A full page reload is not needed for updates, and Signals can be bound in templates, making the other options incorrect.

  3. Component Lifecycle Start

    Which lifecycle hook is called first during the Angular component initialization process?

    1. ngOnDestroy
    2. ngAfterViewInit
    3. ngOnChanges
    4. ngOnInit

    Explanation: ngOnChanges is invoked first whenever Angular sets or changes input properties before ngOnInit is called. ngOnInit comes next, after inputs are set. ngAfterViewInit is later in the sequence, after the view initialization. ngOnDestroy runs when the component is destroyed.

  4. Purpose of ngOnInit

    What is the main purpose of the ngOnInit lifecycle hook in an Angular component?

    1. To initialize component logic after input properties are set
    2. To clean up subscriptions before component removal
    3. To respond immediately to input changes
    4. To capture view children after rendering

    Explanation: ngOnInit is meant for placing initialization logic that depends on the final input values. ngOnDestroy is for cleanup, ngOnChanges responds to input changes, and ngAfterViewInit is for operations after the view's children are rendered, which makes them incorrect here.

  5. Signals vs. Observables

    How do Angular Signals mainly differ from traditional Observables for component state?

    1. Signals can only be used with HTTP requests
    2. Signals require no manual subscription management
    3. Signals are not reactive primitives
    4. Signals rely on the async pipe exclusively

    Explanation: Unlike observables, Angular Signals update the UI automatically and require no manual subscribe or unsubscribe logic from the developer. The async pipe is used with observables, not exclusively with signals. Signals are not limited to HTTP requests and are designed precisely as reactive primitives, which the other responses contradict.

  6. ngAfterViewInit Usage

    When would you typically use ngAfterViewInit in an Angular component?

    1. To access or interact with child view elements after the component's view is initialized
    2. To set initial input property values
    3. To fetch data before rendering the template
    4. To execute code before any bindings are set

    Explanation: ngAfterViewInit is called after Angular fully initializes the component’s view, making it the best place for interacting with view children. Setting input properties and fetching data are better suited for earlier hooks, while running code before bindings are set does not apply to this hook.

  7. Change Detection Trigger

    Which scenario automatically triggers Angular's change detection mechanism?

    1. When a component is destroyed
    2. When the browser is idle
    3. After every CSS style computation
    4. When a signal or bound property changes value

    Explanation: Angular runs change detection when the application's state changes, such as when a signal’s value is set. Component destruction does not itself trigger change detection, and idle browser moments or CSS calculations are unrelated triggers.

  8. Local State Management

    What is a key benefit of using Signals for local state management inside components?

    1. They simplify local reactivity without external state management libraries
    2. They eliminate the need for input properties
    3. They enforce global application state synchronization
    4. They require manual dependency injection

    Explanation: Signals offer an easy and built-in way to manage component-local state reactively, removing the need for extra libraries. They do not enforce global state, do not replace input properties, and do not require explicit dependency injection.

  9. Lifecycle: Cleanup

    In Angular, which lifecycle hook is responsible for performing cleanup operations like unsubscribing from observables?

    1. ngOnInit
    2. ngAfterContentInit
    3. ngOnDestroy
    4. ngDoCheck

    Explanation: ngOnDestroy is designed to handle cleanup tasks, such as unbinding event listeners or unsubscribing from observables. ngAfterContentInit and ngOnInit are for setup, while ngDoCheck is for custom change detection logic.

  10. Performance Optimization

    Which approach can improve performance in large Angular applications?

    1. Triggering change detection manually after every function
    2. Always using inline styles for templates
    3. Using OnPush change detection strategy
    4. Avoiding all lifecycle hooks

    Explanation: The OnPush strategy helps limit when Angular performs change detection checks, improving performance especially in big projects. Avoiding lifecycle hooks is poor practice, inline styles do not impact detection optimization, and manually triggering checks everywhere is inefficient.

  11. State Selection Example

    Given a signal for a counter value, how can you derive a computed value such as double that counter?

    1. By setting the counter to zero every change
    2. By using a computed function that references the signal
    3. By creating a service with an HTTP call
    4. By declaring a new signal without referencing the counter

    Explanation: A computed function can read the signal’s value and derive another, like double the counter, automatically updating when the source changes. HTTP services or unrelated signals do not capture the dependency, and resetting a counter on each change is not computation.

  12. Signal State Update Syntax

    Which syntax is used to update the value of an Angular signal called 'count'?

    1. count.set(5)
    2. setCount(5)
    3. count.update(5)
    4. count = 5

    Explanation: The correct syntax to update a signal's value is count.set(value). Direct assignment or update method are not supported by signals, and setCount is not a convention used in Angular's signal API.

  13. ngOnChanges Trigger

    When is the ngOnChanges lifecycle hook called in an Angular component?

    1. After the ngAfterViewInit hook
    2. When the user clicks a button in the view
    3. On every browser repaint
    4. When input properties are changed by the parent component

    Explanation: ngOnChanges is triggered whenever the parent component updates input-bound properties. User actions or browser repaints do not directly invoke this hook, and it occurs before ngAfterViewInit.

  14. Change Detection Efficiency

    What can help Angular efficiently detect changes and avoid unnecessary checks?

    1. Repeating the same computation in every lifecycle hook
    2. Tracking dependencies accurately with signals and computed properties
    3. Avoiding all change detection mechanisms
    4. Triggering detection manually everywhere

    Explanation: Properly tracking dependencies lets Angular know when to run checks, reducing unnecessary work. Avoiding change detection or triggering it too frequently is bad practice, and repeating computations is wasteful and inefficient.

  15. Component Destruction

    Why is it important to implement ngOnDestroy in Angular components?

    1. To render the component template
    2. To perform cleanup like unsubscribing from observables and freeing resources
    3. To handle HTTP error responses
    4. To set up input property defaults

    Explanation: ngOnDestroy helps prevent memory leaks by allowing proper cleanup when a component is removed. It is unrelated to initial rendering, error handling, or input property setup, which are addressed in different parts of the component lifecycle.

  16. Parent-Child Communication

    Which technique is commonly used for passing data from a parent to a child component in Angular?

    1. Binding an input property in the child component
    2. Exporting the module from the parent
    3. Modifying the child component's private variables directly
    4. Calling a method in the child from the parent template

    Explanation: Data is commonly passed from parent to child by binding an input property, which allows Angular to update the child when parent data changes. Directly modifying private variables is not recommended and breaks encapsulation, while calling child methods or exporting the module are not standard or safe for property binding.