0
0
Node.jsframework~20 mins

Stream types (Readable, Writable, Transform, Duplex) in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stream Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Readable stream output?
Consider this Node.js Readable stream code. What will be printed to the console?
Node.js
import { Readable } from 'stream';

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

readable.on('data', chunk => console.log(chunk.toString()));
AHello\nWorld
BHelloWorld
CHello\nWorld\n
DHello\nHelloWorld
Attempts:
2 left
💡 Hint
Remember that 'data' event emits chunks as they are pushed.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a Writable stream that logs data?
Which code snippet correctly creates a Writable stream that logs each chunk it receives?
A
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk) {
    console.log(chunk.toString());
  }
});
B
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk, encoding) {
    console.log(chunk.toString());
    callback();
  }
});
C
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
});
D
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk);
  }
});
Attempts:
2 left
💡 Hint
The write method must accept three arguments and call callback when done.
state_output
advanced
2:00remaining
What is the output of this Transform stream?
Given this Transform stream that uppercases input, what will be the final output printed?
Node.js
import { Transform } from 'stream';

const upperCaseTransform = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    callback();
  }
});

upperCaseTransform.on('data', chunk => console.log(chunk.toString()));
upperCaseTransform.write('hello ');
upperCaseTransform.write('world');
upperCaseTransform.end();
Ahello world
BHELLO WORLD
CHELLO\nWORLD
DHELLO \nWORLD
Attempts:
2 left
💡 Hint
Each write triggers a 'data' event with the transformed chunk.
🔧 Debug
advanced
2:00remaining
Why does this Duplex stream cause an error?
This Duplex stream code throws an error. What is the cause?
Node.js
import { Duplex } from 'stream';

const duplex = new Duplex({
  read(size) {
    this.push('data');
    this.push(null);
  },
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
});
AThe read method pushes null too early, causing an error.
BThe write method does not call callback(), causing the stream to hang.
CThe Duplex constructor requires a 'final' method, which is missing.
DThe write method should not log data, causing a runtime error.
Attempts:
2 left
💡 Hint
Writable streams must call callback() in write method.
🧠 Conceptual
expert
2:00remaining
Which stream type allows simultaneous reading and writing with independent data flows?
In Node.js streams, which type supports both reading and writing independently, allowing data to flow in both directions at the same time?
ADuplex stream
BWritable stream
CTransform stream
DReadable stream
Attempts:
2 left
💡 Hint
Think about streams that combine reading and writing capabilities.