0
0
Angularframework~20 mins

Zone.js and automatic detection in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Zone.js Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Zone.js detect changes in Angular?
Angular uses Zone.js to automatically detect changes. What triggers Angular's change detection when using Zone.js?
AAngular runs change detection only once when the app starts and never again.
BOnly manual calls to detectChanges() trigger Angular's change detection.
CAngular detects changes only when the user clicks a button explicitly calling a method.
DAny asynchronous event like setTimeout, promises, or DOM events triggers Angular's change detection automatically.
Attempts:
2 left
💡 Hint

Think about what Zone.js wraps to know when to update the UI.

lifecycle
intermediate
2:00remaining
What happens if you run code outside Angular's zone?
If you run code inside Angular's NgZone.runOutsideAngular(), what is the effect on change detection?
AAngular disables the entire app until you manually re-enter the zone.
BAngular will run change detection twice for every asynchronous event.
CAngular will not run change detection automatically for that code's asynchronous events.
DAngular runs change detection normally without any difference.
Attempts:
2 left
💡 Hint

Consider what 'outside Angular's zone' means for automatic updates.

📝 Syntax
advanced
2:00remaining
Identify the correct way to run code outside Angular's zone
Which code snippet correctly runs a function outside Angular's zone to avoid triggering change detection?
Angular
import { Component, NgZone } from '@angular/core';

@Component({ selector: 'app-root', template: '' })
export class AppComponent {
  constructor(private ngZone: NgZone) {}

  runTask() {
    // Which option below correctly runs outside Angular's zone?
  }
}
Athis.ngZone.runOutsideAngular(() => { setTimeout(() => console.log('Task done'), 1000); });
Bthis.ngZone.run(() => { setTimeout(() => console.log('Task done'), 1000); });
CsetTimeout(() => console.log('Task done'), 1000);
Dthis.ngZone.runOutsideAngular; setTimeout(() => console.log('Task done'), 1000);
Attempts:
2 left
💡 Hint

Look for the method that explicitly runs code outside Angular's zone.

🔧 Debug
advanced
2:00remaining
Why does Angular not update the view after an async event?
You run an HTTP request inside NgZone.runOutsideAngular(), but the view does not update after the response. Why?
ABecause running outside Angular's zone prevents automatic change detection after async events.
BBecause HTTP requests never trigger change detection in Angular.
CBecause Angular only updates views on user input events.
DBecause the component's template is missing a binding for the data.
Attempts:
2 left
💡 Hint

Think about what happens when you run code outside Angular's zone.

🧠 Conceptual
expert
3:00remaining
What is the main purpose of Zone.js in Angular?
Why does Angular use Zone.js under the hood for change detection?
ATo allow Angular to run outside the browser environment like Node.js without changes.
BTo automatically detect when asynchronous tasks complete and trigger change detection without manual calls.
CTo replace JavaScript's event loop with a custom one for better performance.
DTo provide a new syntax for writing asynchronous code in Angular templates.
Attempts:
2 left
💡 Hint

Think about how Angular knows when to update the UI after async work.