Bird
Raised Fist0
Angularframework~10 mins

Why observables matter in Angular - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Observable class from RxJS.

Angular
import { [1] } from 'rxjs';
Drag options to blanks, or click blank then click option'
AObservable
BSubject
CSubscription
DObserver
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Observer instead of Observable
Using Subject instead of Observable
2fill in blank
medium

Complete the code to subscribe to an observable and log the emitted value.

Angular
myObservable.subscribe({ next: value => console.[1](value) });
Drag options to blanks, or click blank then click option'
Alog
Binfo
Cwarn
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.error which is for errors
Using console.warn which shows warnings
3fill in blank
hard

Fix the error in the code to correctly create an observable that emits 'Hello'.

Angular
const hello$ = new Observable(observer => { observer.[1]('Hello'); observer.complete(); });
Drag options to blanks, or click blank then click option'
Aerror
Bnext
Csubscribe
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' which is not a method on observer
Using 'subscribe' which is for subscribing, not emitting
4fill in blank
hard

Fill both blanks to create an observable that emits numbers 1 to 3 and then completes.

Angular
const numbers$ = new Observable(observer => { observer.[1](1); observer.[1](2); observer.[1](3); observer.[2](); });
Drag options to blanks, or click blank then click option'
Anext
Berror
Ccomplete
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' instead of 'complete'
Using 'subscribe' instead of 'complete'
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each emitted value to its square if the value is greater than 2.

Angular
const squares = { [1]: [2] for [3] of values if [3] > 2 };
Drag options to blanks, or click blank then click option'
Avalue
Bvalue * value
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently
Not squaring the value correctly

Practice

(1/5)
1. Why are observables important in Angular applications?
easy
A. They help handle asynchronous data streams smoothly.
B. They replace all HTML elements with custom tags.
C. They make Angular apps run faster by compiling code.
D. They automatically style components without CSS.

Solution

  1. Step 1: Understand what observables do

    Observables allow Angular to listen and react to data that changes over time, like user input or server responses.
  2. Step 2: Compare options to observable purpose

    Only "They help handle asynchronous data streams smoothly." correctly describes observables as tools for handling asynchronous data streams smoothly.
  3. Final Answer:

    They help handle asynchronous data streams smoothly. -> Option A
  4. Quick Check:

    Observables = handle async data [OK]
Hint: Observables manage changing data over time [OK]
Common Mistakes:
  • Thinking observables style components
  • Confusing observables with HTML tags
  • Believing observables speed up compilation
2. Which of the following is the correct way to subscribe to an observable in Angular?
easy
A. observable.watch(data => console.log(data));
B. observable.listen(data => console.log(data));
C. observable.on(data => console.log(data));
D. observable.subscribe(data => console.log(data));

Solution

  1. Step 1: Recall Angular observable syntax

    In Angular, the method to receive data from an observable is called subscribe.
  2. Step 2: Check each option's method name

    Only observable.subscribe(data => console.log(data)); uses the correct method subscribe. Others like listen, on, and watch are not valid observable methods.
  3. Final Answer:

    observable.subscribe(data => console.log(data)); -> Option D
  4. Quick Check:

    Subscribe method = correct syntax [OK]
Hint: Use subscribe() to get observable data [OK]
Common Mistakes:
  • Using listen() instead of subscribe()
  • Confusing event handlers with observables
  • Trying to watch() or on() which don't exist
3. Given this Angular code snippet:
const obs = new Observable(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.complete();
subscriber.next(3);
});
obs.subscribe(value => console.log(value));

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

Solution

  1. Step 1: Understand observable emissions

    The observable emits 1, then 2, then calls complete(). After completion, emissions stop, so 3 is ignored.
  2. Step 2: Determine console output

    The subscriber logs values 1 and 2 only. Value 3 is not emitted because the observable is completed before it.
  3. Final Answer:

    1 2 -> Option C
  4. Quick Check:

    Complete stops emissions = 1 2 [OK]
Hint: complete() stops further emissions [OK]
Common Mistakes:
  • Expecting value after complete()
  • Thinking all next() calls run regardless
  • Ignoring complete() effect
4. What is wrong with this Angular observable subscription code?
this.data$ = this.http.get('/api/data');
this.data$.subscribe(data => {
this.data = data;
});
this.data$.subscribe(data => {
console.log(data);
});
medium
A. The subscribe method is misspelled.
B. Multiple subscriptions can cause repeated HTTP calls.
C. Observables cannot be assigned to variables.
D. The HTTP get method does not return an observable.

Solution

  1. Step 1: Analyze multiple subscriptions to the same observable

    Each subscription to an HTTP observable triggers a new HTTP request, causing repeated calls.
  2. Step 2: Identify the problem in the code

    Here, this.data$ is subscribed twice, so two HTTP requests are sent, which is inefficient and often unwanted.
  3. Final Answer:

    Multiple subscriptions can cause repeated HTTP calls. -> Option B
  4. Quick Check:

    Multiple subscriptions = repeated HTTP calls [OK]
Hint: Avoid multiple subscriptions to HTTP observables [OK]
Common Mistakes:
  • Thinking subscribe is misspelled
  • Believing observables can't be variables
  • Assuming HTTP get returns non-observable
5. You want to create a component that updates its display every second with the current time using observables. Which approach correctly uses observables to achieve this in Angular?
hard
A. Use interval(1000).subscribe(time => this.currentTime = new Date()) inside ngOnInit and unsubscribe on destroy.
B. Use setTimeout inside the template to update time every second.
C. Call new Date() once in the constructor and display it.
D. Use a loop inside the template to refresh the time continuously.

Solution

  1. Step 1: Identify observable usage for timed updates

    The interval observable emits values at set intervals, perfect for updating time every second.
  2. Step 2: Proper subscription and cleanup

    Subscribing in ngOnInit and unsubscribing on destroy prevents memory leaks and keeps updates smooth.
  3. Final Answer:

    Use interval(1000).subscribe(time => this.currentTime = new Date()) inside ngOnInit and unsubscribe on destroy. -> Option A
  4. Quick Check:

    interval + subscribe + cleanup = correct pattern [OK]
Hint: Use interval observable with subscribe and unsubscribe [OK]
Common Mistakes:
  • Using setTimeout in template (not reactive)
  • Setting time once in constructor (no updates)
  • Trying to loop in template (not reactive)
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