0
0
Angularframework~20 mins

Subscribing to observables in Angular - Practice Problems & Coding Challenges

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
What will be the console output after subscribing?
Consider this Angular component code snippet that subscribes to an observable emitting numbers 1 to 3. What will be printed in the console?
Angular
import { Component } from '@angular/core';
import { of } from 'rxjs';

@Component({
  selector: 'app-number',
  template: `<p>Check console output</p>`
})
export class NumberComponent {
  constructor() {
    const numbers$ = of(1, 2, 3);
    numbers$.subscribe(value => console.log(value));
  }
}
A[1, 2, 3]
Bundefined
CError: Observable not subscribed
D1\n2\n3
Attempts:
2 left
💡 Hint
Remember that subscribing to an observable triggers its emissions one by one.
lifecycle
intermediate
1:30remaining
When should you unsubscribe from an observable in Angular?
In Angular components, when is the best time to unsubscribe from an observable to avoid memory leaks?
AIn the ngOnDestroy lifecycle hook
BImmediately after subscribing
CIn the constructor
DNever unsubscribe; Angular handles it automatically
Attempts:
2 left
💡 Hint
Think about when the component is removed from the page.
📝 Syntax
advanced
2:00remaining
Identify the error in this subscription code
What error will this Angular code produce when subscribing to an observable?
Angular
import { Component } from '@angular/core';
import { interval } from 'rxjs';

@Component({
  selector: 'app-timer',
  template: `<p>Timer running</p>`
})
export class TimerComponent {
  ngOnInit() {
    const timer$ = interval(1000);
    timer$.subscribe();
  }
}
ARuntime error: Observable not started
BTypeError: subscribe callback missing
CNo error, but subscription does nothing
DSyntaxError: missing callback function
Attempts:
2 left
💡 Hint
Is a callback function required for subscribe?
🔧 Debug
advanced
2:00remaining
Why does this subscription not log values?
This Angular code subscribes to an observable but nothing is logged. What is the cause?
Angular
import { Component } from '@angular/core';
import { of } from 'rxjs';

@Component({
  selector: 'app-test',
  template: `<p>Test</p>`
})
export class TestComponent {
  constructor() {
    const data$ = of(10, 20, 30);
    data$.subscribe();
  }
}
ANo callback function passed to subscribe, so no logs
BObservable emits no values
CSubscription is not started without next callback
DConsole.log is missing in the code
Attempts:
2 left
💡 Hint
Check what subscribe needs to do something with emitted values.
🧠 Conceptual
expert
2:30remaining
What happens if you subscribe multiple times to a cold observable?
In Angular, if you subscribe multiple times to a cold observable like 'of(1,2,3)', what is the behavior?
ASubscriptions share the same execution and emit simultaneously
BEach subscription triggers the observable sequence independently
COnly the first subscription triggers emissions; others get cached values
DSubscribing multiple times causes an error
Attempts:
2 left
💡 Hint
Think about how cold observables behave on each subscription.