0
0
Angularframework~20 mins

OnPush change detection strategy in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
OnPush Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does OnPush affect component updates?
Consider an Angular component using OnPush change detection. Which action will NOT trigger the component to update its view?
AMutating a property of an @Input() object without changing its reference
BChanging an @Input() property to a new object reference
CEmitting an event from the component
DCalling markForCheck() on the component's ChangeDetectorRef
Attempts:
2 left
💡 Hint
OnPush checks only when input references change or events happen.
state_output
intermediate
2:00remaining
Output of component with OnPush and async pipe
Given this Angular component using OnPush and an async pipe in the template, what will be displayed after 2 seconds?
Angular
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Component({
  selector: 'app-counter',
  template: `<div>{{ counter$ | async }}</div>`,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CounterComponent {
  counter$ = new BehaviorSubject(0);

  constructor() {
    setTimeout(() => {
      this.counter$.next(1);
    }, 2000);
  }
}
A1 immediately on load
B0 initially and stays 0 forever
C0 initially, then updates to 1 after 2 seconds
DComponent throws an error due to OnPush
Attempts:
2 left
💡 Hint
Async pipe triggers change detection even with OnPush.
lifecycle
advanced
2:00remaining
When does ngOnChanges run with OnPush?
In an Angular component with OnPush change detection, when is the ngOnChanges lifecycle hook called?
AEvery time change detection runs, regardless of input changes
BOnly when an @Input() property reference changes
COnly when a user event triggers change detection
DNever, ngOnChanges is disabled with OnPush
Attempts:
2 left
💡 Hint
ngOnChanges depends on input property changes.
📝 Syntax
advanced
2:00remaining
Correct syntax to set OnPush strategy
Which option correctly sets the OnPush change detection strategy in an Angular component?
A@Component({ selector: 'app', template: '', changeDetectionStrategy: ChangeDetectionStrategy.OnPush })
B@Component({ selector: 'app', template: '', changeDetection: 'OnPush' })
C@Component({ selector: 'app', template: '', changeDetection: OnPush })
D@Component({ selector: 'app', template: '', changeDetection: ChangeDetectionStrategy.OnPush })
Attempts:
2 left
💡 Hint
Use the ChangeDetectionStrategy enum with the changeDetection property.
🔧 Debug
expert
3:00remaining
Why does this OnPush component not update?
This Angular component uses OnPush. The parent updates an object property but the child does not update its view. What is the cause?
Angular
Parent template: <app-child [data]="user"></app-child>

Parent component:
user = { name: 'Alice' };

updateName() {
  this.user.name = 'Bob';
}

Child component:
@Component({
  selector: 'app-child',
  template: `{{ data.name }}`,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ChildComponent {
  @Input() data!: { name: string };
}
AThe parent mutates the object property without changing the object reference
BOnPush disables all change detection in child components
CThe child component lacks ngOnChanges lifecycle hook
DThe @Input() property is missing the ! non-null assertion
Attempts:
2 left
💡 Hint
OnPush tracks input references, not deep mutations.