Bird
Raised Fist0
Angularframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main role of Zone.js in Angular?
easy
A. It compiles TypeScript to JavaScript.
B. It manages routing between pages.
C. It styles components with CSS.
D. It automatically detects async tasks and triggers UI updates.

Solution

  1. Step 1: Understand Zone.js purpose

    Zone.js tracks asynchronous operations like timers and events.
  2. Step 2: Connect to Angular UI updates

    When async tasks finish, Zone.js tells Angular to update the screen automatically.
  3. Final Answer:

    It automatically detects async tasks and triggers UI updates. -> Option D
  4. Quick Check:

    Zone.js auto-detects async tasks = A [OK]
Hint: Zone.js watches async tasks to update UI automatically [OK]
Common Mistakes:
  • Confusing Zone.js with routing or styling
  • Thinking Zone.js compiles code
  • Assuming manual UI updates are needed
2. Which syntax correctly shows how Angular uses Zone.js to detect changes?
easy
A. Zone.detectChanges()
B. ChangeDetector.detectZone()
C. NgZone.run(() => { /* update UI */ })
D. Zone.runOutsideAngular(() => { /* update UI */ })

Solution

  1. Step 1: Identify Angular's Zone.js API

    Angular provides NgZone.run() to run code inside Angular's zone for UI updates.
  2. Step 2: Check other options

    Other options are not valid Angular Zone.js methods.
  3. Final Answer:

    <code>NgZone.run(() => { /* update UI */ })</code> -> Option C
  4. Quick Check:

    NgZone.run triggers UI update = B [OK]
Hint: NgZone.run wraps code to trigger Angular updates [OK]
Common Mistakes:
  • Using non-existent Zone.js methods
  • Confusing runOutsideAngular with run
  • Assuming detectChanges is a Zone.js method
3. Given this Angular code snippet, what will happen after 1 second?
constructor(private zone: NgZone) {
  setTimeout(() => {
    this.zone.run(() => {
      this.message = 'Updated!';
    });
  }, 1000);
}
medium
A. The UI does not update because setTimeout is outside Angular zone.
B. The UI updates to show 'Updated!' automatically after 1 second.
C. An error occurs because run() cannot be used inside setTimeout.
D. The message changes but UI needs manual refresh.

Solution

  1. Step 1: Analyze setTimeout with NgZone.run

    The setTimeout callback runs inside zone.run(), so Angular detects changes.
  2. Step 2: Understand UI update behavior

    Angular automatically updates the UI to show the new message after 1 second.
  3. Final Answer:

    The UI updates to show 'Updated!' automatically after 1 second. -> Option B
  4. Quick Check:

    NgZone.run triggers UI update = A [OK]
Hint: NgZone.run inside async triggers UI update automatically [OK]
Common Mistakes:
  • Thinking setTimeout runs outside Angular zone here
  • Believing manual UI refresh is needed
  • Assuming run() causes errors inside setTimeout
4. What is wrong with this Angular code that tries to update UI?
constructor(private zone: NgZone) {
  this.zone.runOutsideAngular(() => {
    setTimeout(() => {
      this.message = 'Changed!';
    }, 500);
  });
}
medium
A. UI will not update because changes happen outside Angular zone.
B. setTimeout is not allowed inside runOutsideAngular.
C. NgZone.runOutsideAngular triggers automatic UI update.
D. This code causes a runtime error.

Solution

  1. Step 1: Understand runOutsideAngular effect

    Code inside runOutsideAngular does not trigger Angular change detection.
  2. Step 2: Analyze setTimeout inside runOutsideAngular

    The message changes but Angular does not detect it, so UI won't update automatically.
  3. Final Answer:

    UI will not update because changes happen outside Angular zone. -> Option A
  4. Quick Check:

    runOutsideAngular skips UI update = C [OK]
Hint: runOutsideAngular blocks automatic UI updates [OK]
Common Mistakes:
  • Assuming setTimeout is disallowed inside runOutsideAngular
  • Thinking UI updates automatically anyway
  • Believing this code throws errors
5. You want to optimize an Angular app by running heavy calculations without triggering UI updates, then update UI once done. Which approach using Zone.js is correct?
hard
A. Run calculations inside zone.runOutsideAngular(), then update UI inside zone.run().
B. Run calculations inside zone.run() and update UI outside Angular zone.
C. Run calculations and UI update both inside zone.run().
D. Run calculations and UI update both outside Angular zone.

Solution

  1. Step 1: Use runOutsideAngular for heavy work

    Heavy calculations inside runOutsideAngular() avoid triggering UI updates repeatedly.
  2. Step 2: Use run to update UI once done

    After calculations, call zone.run() to update UI and trigger change detection.
  3. Final Answer:

    Run calculations inside zone.runOutsideAngular(), then update UI inside zone.run(). -> Option A
  4. Quick Check:

    Heavy work outside, UI update inside = D [OK]
Hint: Heavy work outside zone, UI update inside zone.run [OK]
Common Mistakes:
  • Running heavy work inside Angular zone causing slow UI
  • Updating UI outside Angular zone so no refresh
  • Doing both heavy work and UI update outside zone
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard