Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q14 of 15
Angular - RxJS and Observables Fundamentals
What is wrong with this code snippet?
const subject = new Subject();
subject.next('hello');
subject.subscribe(value => console.log(value));
AThe subscription will not receive 'hello' because Subject does not replay past values.
BThe code will throw a syntax error because subscribe is called after next.
CThe Subject should be replaced with BehaviorSubject to avoid errors.
DThe next() method cannot be called before any subscribers exist.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Subject behavior

    Subject sends values only to current subscribers. Since next('hello') is called before subscribe, no subscriber receives it.
  2. Step 2: Check for errors

    There is no syntax or runtime error. The code runs but subscriber misses the first value.
  3. Final Answer:

    The subscription will not receive 'hello' because Subject does not replay past values. -> Option A
  4. Quick Check:

    Subject sends only to current subscribers [OK]
Quick Trick: Subject does not replay values to late subscribers [OK]
Common Mistakes:
MISTAKES
  • Expecting subscriber to get values emitted before subscription
  • Thinking next() before subscribe causes error
  • Confusing Subject with BehaviorSubject or ReplaySubject

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes