0
0
Node.jsframework~20 mins

Writing data with Writable streams in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Writable 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 when writing data to a Writable stream?
Consider the following Node.js code using a Writable stream. What will be printed to the console?
Node.js
import { Writable } from 'stream';

const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
});

writable.write('Hello');
writable.write('World');
writable.end();
A
Hello
World
BHelloWorld
Cundefinedundefined
DError: write method missing callback
Attempts:
2 left
💡 Hint
Remember that each write call triggers the write method separately.
📝 Syntax
intermediate
2:00remaining
Which option correctly implements a Writable stream that converts input to uppercase?
Select the option that correctly creates a Writable stream which converts all written data to uppercase before logging it.
A
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString().toUpperCase());
    callback();
  }
});
B
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk, encoding) {
    console.log(chunk.toString().toUpperCase());
  }
});
C
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk) {
    console.log(chunk.toUpperCase());
    callback();
  }
});
D
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toUpperCase());
  }
});
Attempts:
2 left
💡 Hint
The write method must accept three parameters and call callback when done.
🔧 Debug
advanced
2:00remaining
Why does this Writable stream code cause a runtime error?
Examine the code below. Why does it throw an error when writing data?
Node.js
import { Writable } from 'stream';

const writable = new Writable({
  write(chunk, encoding, callback) {
    const data = chunk.toUpperCase();
    console.log(data);
    callback();
  }
});

writable.write('test');
AThe write method must be async to handle string data properly.
Bchunk is a Buffer, so chunk.toUpperCase() is not a function, causing a TypeError.
CMissing callback call causes the stream to hang and throw an error.
DThe Writable constructor requires a 'read' method, which is missing.
Attempts:
2 left
💡 Hint
Check the type of chunk and what methods it supports.
state_output
advanced
2:00remaining
What is the final value of 'data' after writing to this Writable stream?
Given the following code, what will be the value of the variable 'data' after all writes complete?
Node.js
import { Writable } from 'stream';

let data = '';
const writable = new Writable({
  write(chunk, encoding, callback) {
    data += chunk.toString();
    callback();
  }
});

writable.write('Node');
writable.write('JS');
writable.end();
A"undefined"
B"Node JS"
C"Node\nJS"
D"NodeJS"
Attempts:
2 left
💡 Hint
Consider how the chunks are concatenated without extra characters.
🧠 Conceptual
expert
3:00remaining
Which option best explains backpressure handling in Writable streams?
In Node.js Writable streams, what does the return value of the write() method indicate and how should it be handled?
Awrite() returns true when the internal buffer is full; the producer should immediately write more data to avoid blocking.
Bwrite() returns undefined; backpressure is managed by the operating system, not the stream.
Cwrite() returns false when the internal buffer is full; the producer should wait for the 'drain' event before writing more data.
Dwrite() always returns true; backpressure is handled automatically without developer intervention.
Attempts:
2 left
💡 Hint
Think about how streams signal when they can accept more data.