Sample questions
Question 1Easy
Which of these is NOT one of the four basic Node.js stream types?
- Buffered
- Readable
- Duplex
- 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
- Node.js streams have four basic types: Readable, Writable, Duplex, and Transform.
- Transform is a type of Duplex stream that modifies data as it passes through.
- Writable streams allow writing data to a destination.
- Readable streams allow reading data from a source.
- 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'));
- hi
- 104105
- 0x68 0x69
- 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
- Buffer converts 'hi' to bytes 0x68 and 0x69.
- toString('hex') converts each byte to two hex digits: 68 and 69.
- Concatenating the hex digits gives 6869.
- 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]);
- 97
- a
- "a"
- 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
Buffer.from('abc') holds bytes 97, 98, 99.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()?
- Streams always transfer data over the network faster
- readFile cannot read binary files
- Streaming encrypts the data automatically
- 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
- Reading a 2 GB file with
fs.readFile() loads the entire file into memory. - Memory use spikes to 2 GB, which can crash systems with limited RAM.
- Streaming sends data in chunks, keeping memory usage low and stable.
- Each chunk is processed as it arrives, reducing overall memory footprint.
- This makes streaming more efficient for large files like 2 GB videos or logs.
- 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?
- It runs each stream on a separate thread
- It keeps the whole stream contents in memory so they can be replayed
- It is the only way to connect a Transform stream
- 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
- stream.pipeline() forwards errors to the user, making failures easier to handle.
- Chaining .pipe() does not propagate errors, leading to silent failures and leaked resources.
- pipeline() destroys all streams on failure, preventing resource leaks.
- This ensures clean up and avoids issues with file descriptors or memory.
- Thus, pipeline() is better for error handling and cleanup.
- 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?
- The stream reads JSON files only
- Data is compressed between stages
- The stream can be read by several independent consumers at once
- 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
- By default streams carry only Buffers or strings.
- ObjectMode: true lets each chunk be any JavaScript value, such as an object or a number.
- 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?
- Retry the same chunk immediately
- Stop writing until the "drain" event fires
- Close the stream; the write failed
- 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
writable.write(chunk) returns false when the buffer is full.- False means the chunk was not accepted immediately; it's queued.
- Waiting for 'drain' event ensures memory doesn't overflow.
- Code should pause writing until 'drain' event fires to avoid backpressure.
- This prevents excessive memory usage and maintains performance.
- 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
- 3 3
- 1 3
- 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
- '€'.length is 1 because it's a single UTF-16 code unit.
- Buffer.byteLength('€') is 3 because '€' uses 3 UTF-8 bytes.
- 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());
- abcd
- azcd
- zbcd
- 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
- Buffer 'a' contains bytes [97, 98, 99, 100].
- subarray(1, 3) creates view of bytes [98, 99].
- b[0] = 0x7a changes a[1] to 122 (z).
- a.toString() converts bytes [97, 122, 99, 100] to 'azcd'.
- 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()?
- Other streams are not cleaned up and may leak
- The error goes to the final stream's handler
- The read stream restarts from the beginning
- 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
- The Transform stream emits an error without propagating it.
- Plain .pipe() does not handle errors or destroy connected streams.
- Error is not passed to downstream streams, leading to potential leaks.
- Other streams remain active and may cause resource leaks.
- 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 →