Streams & Buffers Questions with Answers

16 practice questions on Streams & Buffers (Node.js & Backend APIs) with answers and step-by-step solutions. Try 10 samples below, then practise all 16 free in the AptiNest app.

Sample questions

Question 1Easy

Which of these is NOT one of the four basic Node.js stream types?

  1. Buffered
  2. Readable
  3. Duplex
  4. Transform

Answer: A. Buffered

Explanation

The four types are Readable, Writable, Duplex (both directions) and Transform (a Duplex that modifies data passing through).

Step-by-step solution

  1. Node.js streams have four basic types: Readable, Writable, Duplex, and Transform.
  2. Transform is a type of Duplex stream that modifies data as it passes through.
  3. Writable streams allow writing data to a destination.
  4. Readable streams allow reading data from a source.
  5. Buffered is not one of the four basic Node.js stream types.

Why the other options are wrong

  • B. Readable is one of the four basic types, so it is not the one that does not belong.
  • C. Duplex streams are one of the four basic types, so this option is part of the set.
  • D. Transform is a kind of duplex stream and is one of the four basic types, not an outsider.

Tip: Remember: Buffered is not a standard Node.js stream type.

Question 2Easy

What is the output?

console.log(Buffer.from('hi').toString('hex'));
  1. hi
  2. 104105
  3. 0x68 0x69
  4. 6869

Answer: D. 6869

Explanation

"h" is byte 0x68 and "i" is byte 0x69; the hex encoding writes two hex digits per byte.

Step-by-step solution

  1. Buffer converts 'hi' to bytes 0x68 and 0x69.
  2. toString('hex') converts each byte to two hex digits: 68 and 69.
  3. Concatenating the hex digits gives 6869.
  4. Final output is 6869.

Why the other options are wrong

  • A. hi is the text, but toString('hex') turns the bytes into hex digits, so it is not printed as text.
  • B. 104 and 105 are decimal byte values, but hex output shows 68 and 69 instead.
  • C. The 0x prefix is not added by toString('hex'), which prints bare hex digits without a prefix.

Tip: Remember: hex encoding uses two digits per byte.

Question 3Easy

What is the output?

const b = Buffer.from('abc');
console.log(b[0]);
  1. 97
  2. a
  3. "a"
  4. 0

Answer: A. 97

Explanation

Indexing a Buffer returns the byte value as a number; "a" is 97 in ASCII/UTF-8.

Step-by-step solution

  1. Buffer.from('abc') holds bytes 97, 98, 99.
  2. b[0] is 97.

Why the other options are wrong

  • B. It's a number.
  • C. Not a string.
  • D. First byte isn't 0.

Tip: a = 97.

Question 4Easy

Why stream a 2 GB file to an HTTP response instead of loading it with fs.readFile()?

  1. Streams always transfer data over the network faster
  2. readFile cannot read binary files
  3. Streaming encrypts the data automatically
  4. Memory use stays small because data flows in chunks

Answer: D. Memory use stays small because data flows in chunks

Explanation

readFile holds the whole file in memory; a stream sends chunk by chunk with roughly constant memory.

Step-by-step solution

  1. Reading a 2 GB file with fs.readFile() loads the entire file into memory.
  2. Memory use spikes to 2 GB, which can crash systems with limited RAM.
  3. Streaming sends data in chunks, keeping memory usage low and stable.
  4. Each chunk is processed as it arrives, reducing overall memory footprint.
  5. This makes streaming more efficient for large files like 2 GB videos or logs.
  6. Final answer: Memory use stays small because data flows in chunks.

Why the other options are wrong

  • A. Streams do not make the network faster; they only change how memory is used for the data.
  • B. readFile can read binary files; the problem is the memory needed for a 2 GB file.
  • C. Streaming does not encrypt anything; it sends chunks, and encryption needs TLS or similar.

Tip: Use streams for big files to avoid high memory use.

Question 5Medium

Why is stream.pipeline() usually preferred over chaining .pipe() calls?

  1. It runs each stream on a separate thread
  2. It keeps the whole stream contents in memory so they can be replayed
  3. It is the only way to connect a Transform stream
  4. It forwards errors and destroys all streams on failure

Answer: D. It forwards errors and destroys all streams on failure

Explanation

.pipe() does not propagate errors, so a failure can leak file descriptors. pipeline() reports one error and cleans up every stream.

Step-by-step solution

  1. stream.pipeline() forwards errors to the user, making failures easier to handle.
  2. Chaining .pipe() does not propagate errors, leading to silent failures and leaked resources.
  3. pipeline() destroys all streams on failure, preventing resource leaks.
  4. This ensures clean up and avoids issues with file descriptors or memory.
  5. Thus, pipeline() is better for error handling and cleanup.
  6. Final answer: It forwards errors and destroys all streams on failure.

Why the other options are wrong

  • A. The streams do not each get a thread; pipeline runs them in the normal event-loop flow.
  • B. pipeline does not keep all the data in memory; it moves chunks through as they arrive.
  • C. Transform streams can be connected with pipe() too, so pipeline is not the only way.

Tip: Use pipeline() for better error handling and resource cleanup in streams.

Question 6Medium

What does objectMode: true change about a stream?

  1. The stream reads JSON files only
  2. Data is compressed between stages
  3. The stream can be read by several independent consumers at once
  4. Chunks may be any JS value, not just Buffers or strings

Answer: D. Chunks may be any JS value, not just Buffers or strings

Explanation

By default streams carry bytes. In object mode each chunk is one JavaScript value, and highWaterMark counts objects instead of bytes.

Step-by-step solution

  1. By default streams carry only Buffers or strings.
  2. ObjectMode: true lets each chunk be any JavaScript value, such as an object or a number.
  3. The correct answer is option D: Chunks may be any JS value.

Why the other options are wrong

  • A. ObjectMode is not about file format; JSON files are not special to streams.
  • B. ObjectMode does not compress data; compression needs a separate zlib stream.
  • C. Several readers at once is not what objectMode does; that is a different feature.

Tip: Think of objectMode as allowing any JavaScript value per chunk instead of raw bytes or text.

Question 7Medium

writable.write(chunk) returns false. What should well-behaved code do?

  1. Retry the same chunk immediately
  2. Stop writing until the "drain" event fires
  3. Close the stream; the write failed
  4. Ignore it; false simply means the chunk was written successfully

Answer: B. Stop writing until the "drain" event fires

Explanation

false means the internal buffer passed highWaterMark (backpressure). Waiting for "drain" keeps memory bounded.

Step-by-step solution

  1. writable.write(chunk) returns false when the buffer is full.
  2. False means the chunk was not accepted immediately; it's queued.
  3. Waiting for 'drain' event ensures memory doesn't overflow.
  4. Code should pause writing until 'drain' event fires to avoid backpressure.
  5. This prevents excessive memory usage and maintains performance.
  6. Final answer: Stop writing until the 'drain' event fires.

Why the other options are wrong

  • A. Retrying the same chunk at once only adds more data to the full buffer; wait for drain.
  • C. Write failure is reported by the error event, not by a false return from write().
  • D. False does not mean the chunk was written; it means the buffer is full and writes should pause.

Tip: 'Drain' event is key to managing backpressure in writable streams.

Question 8Medium

What is the output?

console.log('€'.length, Buffer.byteLength('€'));
  1. 1 1
  2. 3 3
  3. 1 3
  4. 1 2

Answer: C. 1 3

Explanation

String length counts UTF-16 code units (1 for "€"), while Buffer.byteLength counts UTF-8 bytes; "€" needs 3 bytes.

Step-by-step solution

  1. '€'.length is 1 because it's a single UTF-16 code unit.
  2. Buffer.byteLength('€') is 3 because '€' uses 3 UTF-8 bytes.
  3. So the output is 1 3.

Why the other options are wrong

  • A. '€' is one UTF-16 code unit, but its UTF-8 byte length is 3, so 1 1 is wrong.
  • B. Length counts code units, not bytes, so the first value is 1, not 3.
  • D. Euro sign takes three bytes in UTF-8, not two, so byteLength is not 2.

Tip: Remember: string length ≠ byte length in Node.js.

Question 9Hard

What is the output?

const a = Buffer.from('abcd');
const b = a.subarray(1, 3);
b[0] = 0x7a;
console.log(a.toString());
  1. abcd
  2. azcd
  3. zbcd
  4. azbd

Answer: B. azcd

Explanation

subarray() returns a view over the same memory, not a copy. Writing 0x7a ("z") to b[0] changes a[1].

Step-by-step solution

  1. Buffer 'a' contains bytes [97, 98, 99, 100].
  2. subarray(1, 3) creates view of bytes [98, 99].
  3. b[0] = 0x7a changes a[1] to 122 (z).
  4. a.toString() converts bytes [97, 122, 99, 100] to 'azcd'.
  5. Final answer: azcd.

Why the other options are wrong

  • A. subarray shares memory with a, so writing to b changes a too and a does not stay abcd.
  • C. The write lands at index 1 of a, not index 0, since the view starts at offset one of a.
  • D. Index 2 of a is not touched, because b[0] is the byte at index 1 of a, not index 2.

Tip: subarray() creates a reference, not a copy.

Question 10Hard

A Transform stream is placed between a file read stream and gzip using .pipe(). The Transform emits an error. What happens with plain .pipe()?

  1. Other streams are not cleaned up and may leak
  2. The error goes to the final stream's handler
  3. The read stream restarts from the beginning
  4. Node retries the failed chunk automatically

Answer: A. Other streams are not cleaned up and may leak

Explanation

.pipe() does not forward errors or destroy the other streams, so you need a handler on every stream. stream.pipeline() solves both problems.

Step-by-step solution

  1. The Transform stream emits an error without propagating it.
  2. Plain .pipe() does not handle errors or destroy connected streams.
  3. Error is not passed to downstream streams, leading to potential leaks.
  4. Other streams remain active and may cause resource leaks.
  5. Final answer: Other streams are not cleaned up and may leak.

Why the other options are wrong

  • B. With pipe, an error on the Transform is not forwarded to the final stream's handler.
  • C. The read stream does not restart; pipe does not rewind a source after an error on another stream.
  • D. Node does not retry a failed chunk on its own; the error must be handled by the code.

Tip: Use stream.pipeline() for automatic error handling and cleanup.

Practise all 16 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Node.js & Backend APIs topics · All topics