0
0
Angularframework~20 mins

Creating observables in Angular - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Observable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Observable subscription behavior in Angular
Consider this Angular component code that creates an observable and subscribes to it. What will be logged to the console when the component initializes?
Angular
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-test',
  template: `<p>Check console output</p>`
})
export class TestComponent implements OnInit {
  data$: Observable<number> = new Observable(observer => {
    observer.next(1);
    observer.next(2);
    observer.complete();
  });

  ngOnInit() {
    this.data$.subscribe(value => console.log(value));
  }
}
A1
B2
C1 2
DNo output, observable never emits
Attempts:
2 left
💡 Hint
Think about how many times observer.next is called before complete.
📝 Syntax
intermediate
2:00remaining
Correct syntax to create an observable emitting a single value
Which option correctly creates an observable that emits the string 'hello' once and then completes?
AObservable.of('hello')
Bnew Observable(observer => { observer.next('hello'); observer.complete(); })
CObservable.create(observer => { observer.next('hello'); })
Dnew Observable(observer => { observer.emit('hello'); observer.complete(); })
Attempts:
2 left
💡 Hint
Check the method names and how observables are created in RxJS.
🔧 Debug
advanced
2:00remaining
Why does this observable never emit values?
Given this observable code, why does the subscription never log any values?
Angular
const obs$ = new Observable(observer => {
  setTimeout(() => {
    observer.next('data');
    observer.complete();
  }, 1000);
});

obs$.subscribe(value => console.log(value));
AThe observable is cold and subscription triggers the emission after 1 second, so it logs 'data' after 1 second.
BThe observable never emits because setTimeout is asynchronous and observer.next is not called synchronously.
CThe subscription is missing, so no values are emitted.
DThe observable completes before emitting any value.
Attempts:
2 left
💡 Hint
Think about how asynchronous code inside observables works.
state_output
advanced
2:00remaining
State of subscription after multiple emissions
What is the state of the subscription after this observable emits three values and completes?
Angular
const obs$ = new Observable(observer => {
  observer.next(10);
  observer.next(20);
  observer.next(30);
  observer.complete();
});

let received = [];
obs$.subscribe(value => received.push(value));
Areceived is empty and subscription is closed
Breceived contains [10, 20] and subscription is still open
Creceived contains [30] and subscription is closed
Dreceived contains [10, 20, 30] and subscription is closed
Attempts:
2 left
💡 Hint
Consider what happens when observer.complete is called.
🧠 Conceptual
expert
2:00remaining
Difference between cold and hot observables in Angular
Which statement correctly describes the difference between cold and hot observables in Angular?
ACold observables start emitting values only when subscribed; hot observables emit values regardless of subscriptions.
BCold observables emit values regardless of subscriptions; hot observables start emitting only when subscribed.
CCold observables cannot be multicasted; hot observables cannot be unsubscribed.
DCold observables are synchronous; hot observables are always asynchronous.
Attempts:
2 left
💡 Hint
Think about when the data starts flowing in each observable type.