Bird
Raised Fist0
Node.jsframework~20 mins

Streams vs loading entire file in memory in Node.js - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Stream Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use streams instead of loading entire files?
Which of the following best explains why streams are preferred over loading an entire file into memory in Node.js?
AStreams allow processing data piece by piece, reducing memory usage for large files.
BStreams automatically compress files to save disk space.
CLoading entire files is faster and uses less CPU than streams.
DStreams require the entire file to be loaded before processing starts.
Attempts:
2 left
💡 Hint
Think about memory use when files are very large.
component_behavior
intermediate
2:00remaining
What happens when reading a large file with streams?
Given this Node.js code snippet using streams, what will be the output behavior?
Node.js
const fs = require('fs');
const stream = fs.createReadStream('largefile.txt');
stream.on('data', chunk => {
  console.log('Received chunk of size:', chunk.length);
});
stream.on('end', () => {
  console.log('Finished reading file');
});
AReads the entire file at once and logs one chunk size.
BLogs 'Finished reading file' immediately, then logs chunk sizes.
CThrows an error because 'data' event is not supported on streams.
DLogs multiple 'Received chunk of size:' messages as data arrives, then logs 'Finished reading file'.
Attempts:
2 left
💡 Hint
Streams emit 'data' events multiple times as chunks arrive.
📝 Syntax
advanced
2:00remaining
Identify the error in this stream reading code
What error will this Node.js code produce when trying to read a file using streams?
Node.js
const fs = require('fs');
const stream = fs.createReadStream('file.txt');
stream.on('data', (chunk) => {
  console.log(chunk.toString());
});
stream.on('finish', () => {
  console.log('Done reading');
});
ANo error; logs file content and 'Done reading'.
BSyntaxError due to missing semicolon.
CError: 'finish' event does not exist on readable streams.
DTypeError because chunk is undefined.
Attempts:
2 left
💡 Hint
Check if 'finish' is the right event for reading streams.
state_output
advanced
2:00remaining
Memory usage difference between streams and full file read
If you read a 1GB file using fs.readFile versus fs.createReadStream, what is the expected memory usage behavior?
ABoth use the same memory because Node.js caches files automatically.
Bfs.readFile uses much more memory because it loads the entire file; streams use less memory by processing chunks.
CStreams use more memory because they keep multiple chunks in memory at once.
Dfs.readFile uses less memory because it reads faster.
Attempts:
2 left
💡 Hint
Think about how much data is held in memory at once.
🔧 Debug
expert
2:00remaining
Why does this stream pipeline hang without output?
Consider this Node.js code that reads a file and writes to another file using streams. Why does it hang without finishing?
Node.js
const fs = require('fs');
const readStream = fs.createReadStream('input.txt');
const writeStream = fs.createWriteStream('output.txt');
readStream.pipe(writeStream);
// No event handlers attached
AThe code is correct; it should finish but no logs show because no events are handled.
BThe writeStream is not closed, so the process waits indefinitely.
CThe readStream is not paused, causing a deadlock.
DThe pipe method is missing a callback to start the flow.
Attempts:
2 left
💡 Hint
Think about what happens if no logs or events are used.

Practice

(1/5)
1. What is the main advantage of using streams in Node.js instead of loading an entire file into memory?
easy
A. Streams load the entire file faster than reading all at once.
B. Streams require less code to read files than other methods.
C. Streams automatically compress files during reading.
D. Streams process data in small chunks, saving memory.

Solution

  1. Step 1: Understand how streams work

    Streams read data piece by piece, not all at once, which uses less memory.
  2. Step 2: Compare with loading entire file

    Loading entire file reads all data into memory, which can be heavy for big files.
  3. Final Answer:

    Streams process data in small chunks, saving memory. -> Option D
  4. Quick Check:

    Streams = small chunks, less memory [OK]
Hint: Streams handle data bit by bit, saving memory [OK]
Common Mistakes:
  • Thinking streams load files faster always
  • Believing streams compress data automatically
  • Assuming streams require less code always
2. Which of the following is the correct way to create a readable stream for a file named data.txt in Node.js?
easy
A. const stream = fs.createReadStream('data.txt');
B. const stream = fs.readFile('data.txt');
C. const stream = fs.openStream('data.txt');
D. const stream = fs.streamFile('data.txt');

Solution

  1. Step 1: Recall Node.js stream syntax

    The correct method to create a readable stream is fs.createReadStream(filename).
  2. Step 2: Check each option

    Only const stream = fs.createReadStream('data.txt'); uses the correct method name and syntax.
  3. Final Answer:

    const stream = fs.createReadStream('data.txt'); -> Option A
  4. Quick Check:

    Use createReadStream() to read files as streams [OK]
Hint: Use fs.createReadStream() to open file streams [OK]
Common Mistakes:
  • Using fs.readFile() which reads whole file, not stream
  • Using non-existent methods like openStream or streamFile
  • Missing quotes around filename
3. Consider this Node.js code snippet:
const fs = require('fs');
let data = '';
const stream = fs.createReadStream('file.txt');
stream.on('data', chunk => { data += chunk; });
stream.on('end', () => { console.log(data.length); });

What will this code output if file.txt is 5000 bytes?
medium
A. It will print undefined
B. It will print 5000
C. It will print 0
D. It will throw an error

Solution

  1. Step 1: Understand stream data event

    The 'data' event adds chunks of the file to the data string as they arrive.
  2. Step 2: Check what happens on 'end'

    When the stream ends, data.length is logged, which equals the total bytes read (5000).
  3. Final Answer:

    It will print 5000 -> Option B
  4. Quick Check:

    Stream chunks combined length = file size [OK]
Hint: Stream 'data' events accumulate full content length [OK]
Common Mistakes:
  • Assuming data is empty before 'end' event
  • Expecting undefined because of async nature
  • Thinking stream throws error without error handler
4. This code tries to read a file using streams but does not print anything:
const fs = require('fs');
const stream = fs.createReadStream('bigfile.txt');
stream.on('data', chunk => { console.log(chunk.toString()); });

What is the likely reason no output appears?
medium
A. Streams require a 'data' event to be removed to work.
B. The 'end' event is missing to start reading.
C. The file path is incorrect or file does not exist.
D. The stream is paused by default and needs resume() call.

Solution

  1. Step 1: Check if file exists

    If the file path is wrong or file missing, stream emits error and no data event triggers.
  2. Step 2: Understand stream default behavior

    Streams start flowing automatically when 'data' event is attached; no need to call resume().
  3. Final Answer:

    The file path is incorrect or file does not exist. -> Option C
  4. Quick Check:

    No output usually means file missing or wrong path [OK]
Hint: Check file path first if streams show no output [OK]
Common Mistakes:
  • Thinking 'end' event triggers reading
  • Believing streams pause by default without resume()
  • Removing 'data' event to fix reading
5. You want to process a very large log file line by line without loading it fully into memory. Which approach best fits this need in Node.js?
hard
A. Use fs.createReadStream() with a line-by-line parser like readline module.
B. Use fs.open() and read fixed-size buffers manually without streams.
C. Use synchronous file reading with fs.readFileSync().
D. Use fs.readFile() to load entire file, then split lines.

Solution

  1. Step 1: Identify memory-efficient reading

    Loading entire file or synchronous reading uses lots of memory and blocks event loop.
  2. Step 2: Combine streams with line parsing

    Using fs.createReadStream() with readline module reads file chunk by chunk and processes lines efficiently.
  3. Final Answer:

    Use fs.createReadStream() with a line-by-line parser like readline module. -> Option A
  4. Quick Check:

    Streams + readline = memory-efficient line processing [OK]
Hint: Combine streams with readline for big file line processing [OK]
Common Mistakes:
  • Loading entire file for big logs causes memory issues
  • Using synchronous methods blocks Node.js event loop
  • Reading fixed buffers manually is complex and error-prone