0
0
Angularframework~20 mins

BehaviorSubject as simple store in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BehaviorSubject Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Angular component using BehaviorSubject?

Consider this Angular component that uses a BehaviorSubject as a simple store. What will be displayed in the template after the button is clicked once?

Angular
import { Component } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Component({
  selector: 'app-counter',
  template: `
    <p>Count: {{ count$ | async }}</p>
    <button (click)="increment()">Increment</button>
  `
})
export class CounterComponent {
  private countSubject = new BehaviorSubject<number>(0);
  count$ = this.countSubject.asObservable();

  increment() {
    const current = this.countSubject.value;
    this.countSubject.next(current + 1);
  }
}
ACount: NaN
BCount: 1
CCount: undefined
DCount: 0
Attempts:
2 left
💡 Hint

Remember that BehaviorSubject holds the current value and emits it to subscribers immediately.

state_output
intermediate
2:00remaining
What is the value of the BehaviorSubject after these operations?

Given this code snippet, what is the final value emitted by store$?

Angular
import { BehaviorSubject } from 'rxjs';

const store = new BehaviorSubject({ count: 0 });

store.next({ count: store.value.count + 1 });
store.next({ count: store.value.count + 2 });

const store$ = store.asObservable();
A{"count": 3}
B{"count": 0}
C{"count": 2}
D{"count": 1}
Attempts:
2 left
💡 Hint

Each next uses the current value at that moment.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a BehaviorSubject with initial value and exposes it as observable?

Choose the correct code snippet that creates a BehaviorSubject with initial value 10 and exposes it as a read-only observable named value$.

A
private valueSubject = BehaviorSubject(10);
public value$ = valueSubject.asObservable();
B
public valueSubject = new BehaviorSubject(10);
private value$ = this.valueSubject.asObservable();
C
private valueSubject = new BehaviorSubject&lt;number&gt;(10);
public value$ = this.valueSubject.asObservable();
D
const valueSubject = new BehaviorSubject&lt;number&gt;;
const value$ = valueSubject.asObservable();
Attempts:
2 left
💡 Hint

Remember to use new keyword and specify initial value in constructor.

🔧 Debug
advanced
2:00remaining
Why does this BehaviorSubject-based store not update the component view?

In this Angular component, the template does not update when updateName is called. What is the cause?

Angular
import { Component } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Component({
  selector: 'app-name',
  template: `
    <p>Name: {{ name$ | async }}</p>
    <button (click)="updateName('Alice')">Set Alice</button>
  `
})
export class NameComponent {
  private nameSubject = new BehaviorSubject<string>('Bob');
  name$ = this.nameSubject.asObservable();

  updateName(newName: string) {
    this.nameSubject.next(newName);
  }
}
ADirectly assigning to nameSubject.value does not emit a new value; use next() instead.
BThe template is missing the async pipe, so it does not update.
CBehaviorSubject cannot hold string values; it only supports numbers.
DThe component selector is incorrect, so the component is not rendered.
Attempts:
2 left
💡 Hint

Check how to properly update a BehaviorSubject's value.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using BehaviorSubject as a simple store in Angular?

Choose the best explanation for why BehaviorSubject is often used as a simple store in Angular applications.

AIt enforces immutability of the state, preventing accidental changes.
BIt automatically persists state to local storage without extra code.
CIt delays emissions until all subscribers are ready, ensuring synchronized updates.
DIt holds the current state and immediately emits the latest value to new subscribers, enabling reactive UI updates.
Attempts:
2 left
💡 Hint

Think about how BehaviorSubject differs from a regular Subject.