0
0
Angularframework~20 mins

Why observables matter in Angular - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Observable Mastery in Angular
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use observables for HTTP requests in Angular?

Angular uses observables for HTTP requests instead of promises. Why is this approach beneficial?

AObservables run HTTP requests synchronously, promises run them asynchronously.
BObservables automatically retry failed HTTP requests without extra code, promises do not.
CObservables allow multiple values over time and can be cancelled, unlike promises which resolve once.
DObservables convert HTTP responses to JSON automatically, promises require manual parsing.
Attempts:
2 left
💡 Hint

Think about how data streams and cancellation work in Angular.

component_behavior
intermediate
2:00remaining
What happens when subscribing to an observable in Angular?

Consider an Angular component subscribing to an observable from a service. What is the main behavior when the observable emits new data?

AThe observable caches the data and replays it only when the component is destroyed.
BThe component blocks UI rendering until the observable completes.
CThe component automatically unsubscribes after the first data emission to prevent memory leaks.
DThe component's subscription callback runs each time new data is emitted, updating the view accordingly.
Attempts:
2 left
💡 Hint

Think about how Angular updates the UI reactively.

📝 Syntax
advanced
2:00remaining
Identify the correct observable pipe usage in Angular

Which option correctly uses the pipe method with map and filter operators on an observable?

Angular
import { map, filter } from 'rxjs/operators';

const source$ = someObservable.pipe( /* ??? */ );
Afilter(x => x > 10), map(x => x * 2)
Bmap(x => x * 2), filter(x => x > 10)
Cmap(x => x * 2 && filter(x => x > 10))
Dfilter(x => x > 10 || map(x => x * 2))
Attempts:
2 left
💡 Hint

Remember the order of operators matters in a pipe.

🔧 Debug
advanced
2:00remaining
Why does this Angular observable subscription cause a memory leak?

Look at this Angular component code snippet:

ngOnInit() {
  this.dataService.getData().subscribe(data => {
    this.items = data;
  });
}

Why might this cause a memory leak?

Angular
ngOnInit() {
  this.dataService.getData().subscribe(data => {
    this.items = data;
  });
}
AThe subscription is never unsubscribed, so it stays active even after the component is destroyed.
BThe observable is synchronous, causing the UI to freeze.
CThe dataService.getData() method returns a promise, not an observable.
DThe items variable is not declared, causing a runtime error.
Attempts:
2 left
💡 Hint

Think about Angular component lifecycle and subscriptions.

state_output
expert
3:00remaining
What is the final value of count after this Angular observable sequence?

Given this Angular component code:

count = 0;

ngOnInit() {
  const source$ = of(1, 2, 3).pipe(
    tap(x => this.count += x),
    filter(x => x % 2 === 1),
    map(x => x * 10)
  );

  source$.subscribe();
}

What is the value of count after ngOnInit runs?

Angular
import { of } from 'rxjs';
import { tap, filter, map } from 'rxjs/operators';

count = 0;

ngOnInit() {
  const source$ = of(1, 2, 3).pipe(
    tap(x => this.count += x),
    filter(x => x % 2 === 1),
    map(x => x * 10)
  );

  source$.subscribe();
}
A30
B6
C4
D0
Attempts:
2 left
💡 Hint

Remember tap runs before filter and map.