0
0
Angularframework~20 mins

Observable vs Promise mental model in Angular - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Observable vs Promise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Observable vs Promise: Emission Behavior
Which statement best describes the difference in how Observables and Promises emit values over time?
AObservables emit multiple values over time, while Promises emit only one value once.
BPromises emit multiple values over time, while Observables emit only one value once.
CBoth Observables and Promises emit multiple values over time.
DBoth Observables and Promises emit only one value once.
Attempts:
2 left
💡 Hint
Think about how many times you can get data from each after subscribing or calling.
component_behavior
intermediate
2:00remaining
Angular Component: Observable Subscription Behavior
In an Angular component, if you subscribe to an Observable that emits values every second, what happens if you subscribe twice without unsubscribing?
Angular
this.myObservable.subscribe(val => console.log('First:', val));
this.myObservable.subscribe(val => console.log('Second:', val));
ASubscribing twice causes a runtime error.
BOnly the first subscription receives values; the second does not receive any.
CThe Observable emits values only once, so only one log appears every second.
DBoth subscriptions receive the same values independently, so logs show 'First:' and 'Second:' every second.
Attempts:
2 left
💡 Hint
Think about how Observables handle multiple subscribers.
lifecycle
advanced
2:00remaining
Promise vs Observable: Cancellation and Cleanup
Which statement correctly describes how cancellation or cleanup works for Promises and Observables in Angular?
AObservables can be cancelled by unsubscribing, but Promises cannot be cancelled once started.
BNeither Promises nor Observables support cancellation.
CBoth Promises and Observables can be cancelled by calling unsubscribe().
DPromises can be cancelled by calling unsubscribe(), but Observables cannot be cancelled.
Attempts:
2 left
💡 Hint
Consider how you stop receiving data from each after starting.
📝 Syntax
advanced
2:00remaining
Correct Syntax for Creating an Observable
Which code snippet correctly creates an Observable that emits the values 1, 2, and 3, then completes?
Aconst obs = Observable.of(1, 2, 3);
Bconst obs = new Observable(observer => { observer.next(1); observer.next(2); observer.next(3); observer.complete(); });
Cconst obs = new Promise(resolve => { resolve([1, 2, 3]); });
Dconst obs = Observable.create(observer => { observer.emit(1); observer.emit(2); observer.emit(3); observer.done(); });
Attempts:
2 left
💡 Hint
Check the method names and syntax for Observables.
🔧 Debug
expert
2:30remaining
Debugging Observable Subscription Memory Leak
An Angular component subscribes to an Observable in ngOnInit but does not unsubscribe in ngOnDestroy. What is the most likely consequence?
AThe Observable throws an error when the component is destroyed.
BNo issue occurs; Angular automatically unsubscribes Observables on component destroy.
CThe subscription continues after component destruction, causing a memory leak and unexpected behavior.
DThe subscription is paused automatically and resumes if the component is recreated.
Attempts:
2 left
💡 Hint
Think about what happens if you keep listening to data after leaving a page.