Which change detection strategy checks the component only when its input properties change?

  • ChangeWhenInputChanges
  • CheckOnce
  • Default
  • OnPush
The change detection strategy that checks the component only when its input properties change is "ChangeWhenInputChanges." This strategy is commonly referred to as "OnPush." When using this strategy, Angular will recheck a component only when its input properties change, improving performance by reducing unnecessary checks on components that haven't received new input data.

The lifecycle hook ngAfterViewInit is specifically related to a component's child ________.

  • Directive
  • Element
  • Module
  • Template
The lifecycle hook ngAfterViewInit is specifically related to a component's child template. This hook is called after Angular has fully initialized a component's view and any child views it contains. It allows you to interact with the template and its elements once they are ready for manipulation.

Which Route Guard allows you to check whether a user can activate a particular route?

  • CanActivate
  • CanActivateChild
  • CanDeactivate
  • CanLoad
The 'CanActivate' Route Guard is used to check whether a user can activate a particular route. It's commonly used for protecting routes based on authentication or other conditions. This guard determines if a user has permission to access a specific route and is typically used to secure routes in Angular applications. The other guards have different roles, such as 'CanActivateChild' for protecting child routes and 'CanDeactivate' for confirming navigation away from a component.

You are working on an autocomplete feature. As a user types into a search box, you want to delay the API call until the user has stopped typing for a specific duration. Which RxJS operator would you utilize?

  • debounceTime
  • mergeMap
  • switchMap
  • throttleTime
To achieve this behavior, you should use the debounceTime operator in RxJS. It allows you to wait for a specific duration of inactivity before emitting the latest value, which is ideal for scenarios like autocomplete.

You want to test a component that has a dependency on another service, but you don't want to test the service's actual behavior. How would you approach this in Angular tests?

  • Create a mock service that provides controlled responses
  • Mock the component instead of the service
  • Use HttpClientTestingModule to mock HTTP service
  • Use the actual service for realistic testing
To isolate the component's behavior for testing, you should create a mock service that provides controlled responses. This allows you to focus solely on testing the component's interactions with the service without invoking the actual service logic.

In which scenario is it NOT recommended to use ChangeDetectionStrategy.OnPush?

  • When a component has complex view logic with many nested components
  • When a component is stateful and has user interactions triggering updates
  • When a component relies on frequent data updates from external sources
  • When optimizing for performance is a top priority
ChangeDetectionStrategy.OnPush is not recommended when a component relies on frequent data updates from external sources because it can lead to manual triggering of change detection, reducing the benefits of this strategy. It's more suitable for components with infrequent updates or performance optimization.

How can you ensure that a particular component is checked only once by Angular's change detection mechanism?

  • Using the ChangeDetectionStrategy.OnPush strategy.
  • Wrapping the component in an async pipe.
  • Manually triggering change detection for that component.
  • Adding the component to the AppModule declarations array.
You can ensure that a particular component is checked only once by Angular's change detection mechanism by using the ChangeDetectionStrategy.OnPush strategy. This strategy tells Angular to only run change detection on a component when its input properties change or when you manually trigger it. The other options do not provide a way to control change detection behavior in the same way.

In what scenario might you need to use both ComponentFactoryResolver and ViewContainerRef together?

  • When creating a dynamic form with user-generated fields.
  • When applying CSS styles to a component.
  • When handling HTTP requests in Angular.
  • When using Angular animations in a component.
You might need to use both ComponentFactoryResolver and ViewContainerRef together when creating a dynamic form with user-generated fields. ComponentFactoryResolver is used to dynamically create components for each form field, and ViewContainerRef is used to manage the location where these components are inserted within the DOM. This combination allows you to dynamically generate and manage form fields based on user input or requirements. The other options do not typically require both ComponentFactoryResolver and ViewContainerRef together.

For which purpose might you consider using dynamic components in Angular?

  • Defining application routes
  • Lazy loading of modules
  • Optimizing database queries by default
  • Styling components dynamically
Dynamic components in Angular are often used for lazy loading of modules. This allows you to load parts of your application on-demand, improving initial loading times and overall performance. While Angular uses routes to define application navigation, dynamic components are not used for this specific purpose. They are also not primarily used for styling components dynamically or optimizing database queries.

If a service is provided both at the module level and the component level, which instance will the component receive?

  • The component will receive both instances.
  • The component will receive the component-level instance.
  • The component will receive the module-level instance.
  • The component will throw an error.
In Angular (or similar frameworks), when a service is provided both at the module and component levels, the component will receive the component-level instance. This is known as "shadowing." The component-level instance takes precedence over the module-level one. The scenario where a component receives both instances is not valid, and it will result in an error.