0
0
Node.jsframework~20 mins

Reading data with Readable streams in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Readable Stream Master
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 Node.js Readable stream code?
Consider the following code that reads data from a Readable stream. What will be logged to the console?
Node.js
import { Readable } from 'stream';

const readable = Readable.from(['Hello', ' ', 'World']);

readable.on('data', chunk => {
  console.log(chunk.toString());
});
AHello\n \nWorld\n
BHello World (all in one chunk)
CHello World\n
DHello World
Attempts:
2 left
💡 Hint
Think about how the 'data' event emits chunks separately.
📝 Syntax
intermediate
1:30remaining
Which option correctly creates a Readable stream from an array of strings?
You want to create a Readable stream from an array of strings ['a', 'b', 'c']. Which code snippet is correct?
Aconst readable = Readable(['a', 'b', 'c']);
Bconst readable = new Readable(['a', 'b', 'c']);
Cconst readable = new Readable.from(['a', 'b', 'c']);
Dconst readable = Readable.from(['a', 'b', 'c']);
Attempts:
2 left
💡 Hint
Check the correct way to use Readable.from static method.
state_output
advanced
2:00remaining
What is the value of 'dataCollected' after this code runs?
This code reads data from a Readable stream and collects it into a string. What is the final value of 'dataCollected'?
Node.js
import { Readable } from 'stream';

const readable = Readable.from(['Node', 'JS']);

let dataCollected = '';

readable.on('data', chunk => {
  dataCollected += chunk.toString();
});

readable.on('end', () => {
  // end event
});
A"NodeJS"
B"Node JS"
C"Node\nJS"
D"Node"
Attempts:
2 left
💡 Hint
Remember how the chunks are emitted and concatenated.
🔧 Debug
advanced
2:30remaining
Why does this Readable stream code cause a runtime error?
Examine the code below. Why does it throw an error when run?
Node.js
import { Readable } from 'stream';

const readable = new Readable({
  read() {
    this.push('data');
    this.push(null);
  }
});

readable.on('data', chunk => {
  console.log(chunk.toString());
});

readable.on('end', () => {
  console.log('Stream ended');
});
ACalling readable.read() manually causes error because data is pushed asynchronously.
BNo error occurs; the code runs and logs 'data' and 'Stream ended'.
CThe stream emits data twice causing a conflict.
DThe read() method pushes data but calling readable.read() is incorrect usage causing error.
Attempts:
2 left
💡 Hint
Consider how the Readable stream's read() method is used internally.
🧠 Conceptual
expert
3:00remaining
Which option best explains how backpressure is handled in Node.js Readable streams?
Backpressure helps control data flow in streams. Which explanation correctly describes how Node.js Readable streams handle backpressure?
ABackpressure is not handled by Readable streams; it must be managed manually by the developer.
BThe stream drops data chunks if the consumer is slow to avoid memory overflow.
CThe stream buffers data internally and pauses emitting 'data' events when the consumer is slow, resuming when ready.
DThe stream automatically speeds up the producer to match the consumer's speed.
Attempts:
2 left
💡 Hint
Think about how streams avoid overwhelming the consumer.