Bird
Raised Fist0
Angularframework~20 mins

Observable vs Promise mental model in Angular - Practice Questions

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

Practice

(1/5)
1. Which statement best describes the difference between an Observable and a Promise in Angular?
easy
A. Observable is synchronous; Promise is asynchronous.
B. Promise handles a single future value; Observable can handle multiple values over time.
C. Promise can be cancelled; Observable cannot be cancelled.
D. Observable handles a single future value; Promise can handle multiple values over time.

Solution

  1. Step 1: Understand the nature of Promise

    A Promise represents a single future value or error that will be resolved once.
  2. Step 2: Understand the nature of Observable

    An Observable can emit multiple values over time and can be stopped or cancelled.
  3. Final Answer:

    Promise handles a single future value; Observable can handle multiple values over time. -> Option B
  4. Quick Check:

    Promise = single value, Observable = multiple values [OK]
Hint: Promise = one value, Observable = many values over time [OK]
Common Mistakes:
  • Thinking Promise can emit multiple values
  • Believing Observable cannot be cancelled
  • Confusing synchronous and asynchronous behavior
2. Which of the following is the correct way to create a Promise that resolves with the value 42 in Angular?
easy
A. const p = new Promise((resolve, reject) => reject(42));
B. const p = new Promise(resolve => reject(42));
C. const p = new Promise((resolve, reject) => resolve(42));
D. const p = new Promise(() => 42);

Solution

  1. Step 1: Check Promise constructor syntax

    The Promise constructor takes a function with two parameters: resolve and reject.
  2. Step 2: Verify correct usage to resolve with 42

    Calling resolve(42) inside the function correctly resolves the Promise with 42.
  3. Final Answer:

    const p = new Promise((resolve, reject) => resolve(42)); -> Option C
  4. Quick Check:

    Promise constructor needs resolve and reject parameters [OK]
Hint: Promise constructor needs (resolve, reject) parameters [OK]
Common Mistakes:
  • Omitting reject parameter in Promise constructor
  • Calling reject instead of resolve to return value
  • Returning value directly without resolve
3. Consider this Angular code snippet:
const obs = new Observable(subscriber => {
  subscriber.next(1);
  subscriber.next(2);
  subscriber.complete();
});

obs.subscribe(value => console.log(value));

What will be printed to the console?
medium
A. 1 2
B. 1
C. 2
D. No output

Solution

  1. Step 1: Analyze Observable emissions

    The Observable emits two values: 1 and 2, then completes.
  2. Step 2: Understand subscription behavior

    The subscriber logs each emitted value, so both 1 and 2 are printed.
  3. Final Answer:

    1 2 -> Option A
  4. Quick Check:

    Observable emits multiple values, all logged [OK]
Hint: Observable emits all next() values until complete [OK]
Common Mistakes:
  • Assuming only first value is logged
  • Thinking complete() emits a value
  • Confusing Observable with Promise single value
4. What is wrong with this Promise usage in Angular?
const p = new Promise((resolve, reject) => {
  setTimeout(() => resolve('done'), 1000);
});
p.then(result => console.log(result));
p.catch(error => console.error(error));
p.cancel();
medium
A. Promises do not have a cancel() method, so calling it causes an error.
B. The Promise should use reject() instead of resolve() inside setTimeout.
C. The then() method should come after catch(), not before.
D. The Promise constructor is missing the reject parameter.

Solution

  1. Step 1: Check Promise API for cancellation

    Promises do not support cancellation; there is no cancel() method.
  2. Step 2: Identify error cause

    Calling p.cancel() will cause a runtime error because cancel() is undefined.
  3. Final Answer:

    Promises do not have a cancel() method, so calling it causes an error. -> Option A
  4. Quick Check:

    Promise cancellation is not supported [OK]
Hint: Promises cannot be cancelled; no cancel() method exists [OK]
Common Mistakes:
  • Thinking Promise supports cancel() like Observable
  • Confusing resolve and reject usage
  • Believing then() must come after catch()
5. You want to fetch user data that updates every 5 seconds and be able to stop the updates when the user navigates away. Which approach fits best in Angular?
hard
A. Use an Observable but do not unsubscribe; it stops automatically.
B. Use a Promise that resolves with user data every 5 seconds.
C. Use a Promise and call cancel() to stop fetching data.
D. Use an Observable that emits new user data every 5 seconds and unsubscribe on navigation.

Solution

  1. Step 1: Identify need for multiple values over time

    Fetching user data every 5 seconds means multiple values over time, which suits an Observable.
  2. Step 2: Understand cancellation requirement

    Observables can be unsubscribed to stop receiving updates, fitting the need to stop on navigation.
  3. Final Answer:

    Use an Observable that emits new user data every 5 seconds and unsubscribe on navigation. -> Option D
  4. Quick Check:

    Multiple updates + cancel = Observable with unsubscribe [OK]
Hint: Multiple updates + cancel = Observable with unsubscribe [OK]
Common Mistakes:
  • Using Promise for multiple repeated values
  • Assuming Promise supports cancel()
  • Not unsubscribing Observable causing memory leaks
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