Quiz: NodeJS IO Mastery
Test your knowledge of files, streams & buffers
Ready to dive into the world of NodeJS IO? 🌊
This quiz will test your understanding of Node’s IO operations, from basic file system operations to advanced streaming concepts. We’ll cover buffers, encoding, and best practices for handling data efficiently.
Let’s see how well you know your streams from your buffers! 🚀
Warmup: Buffers
What does this code do?
Buffer.alloc(size)
creates a new Buffer of specified size filled with zeros.
The output will be: <Buffer 00 00 00 00 00>
If you want to create a Buffer with random data, use Buffer.allocUnsafe(5)
.
Warmup: Buffers
What will this print?
The numbers in the array represent ASCII codes:
- 65: ‘A’
toString()
converts these bytes to their string representation using UTF-8 encoding by default.
Warmup: File System
What’s the output order?
Since readFile
is asynchronous, the code continues executing while the file is being read.
Therefore, “Done” will be printed before the file content.
To wait for the file to be read first, you could use the Promise-based version:
File System Basics
What does this code return?
fs.readFileSync()
returns a Buffer by default when no encoding is specified. If you want a string, you need to either:
- Specify an encoding:
fs.readFileSync('test.txt', 'utf8')
- Convert the Buffer:
content.toString()
Streams
Which set of events are commonly used with Readable streams?
Readable streams emit several important events:
- ‘data’: When data is available to be read
- ‘end’: When there is no more data to read
- ‘error’: When an error occurs
- ‘finish’: When all data has been flushed to the underlying system
Streams
What does this code do?
pipe()
connects a readable stream to a writable stream, automatically managing backpressure and copying data in chunks without loading the entire file into memory.
This is memory-efficient for large files compared to fs.readFile()
followed by fs.writeFile()
.
File System
What does the recursive option do?
The recursive: true
option creates parent directories if they don’t exist.
Without this option, trying to create ‘./a/b/c’ would throw an error if ‘./a’ or ‘./a/b’ don’t exist.
This is similar to the shell command mkdir -p
.
Streams
What will this output?
Transform streams modify data as it passes through. Here, each chunk is:
- Converted to a string
- Transformed to uppercase
- Passed to stdout
This creates a pipeline that converts all input to uppercase.
File System
How many times will the callback fire when a file is modified?
fs.watch()
often fires twice for a single change because many text editors:
- Save to a temporary file
- Rename it to the target file
For more reliable watching, consider using:
- The
chokidar
package - Debouncing the callback
- Using
fs.watchFile()
(though it’s less efficient)
Buffers
What’s the output?
Buffers are compared by reference, not value. Even though they contain the same data, they are different objects.
To compare Buffer contents, use:
Streams
What’s the main purpose of stream backpressure?
Backpressure is a mechanism that prevents memory overflow by pausing reading when the writing end can’t keep up.
Example of manual backpressure:
pipe()
handles this automatically!
File System
What does this code do?
symlinkSync
creates a symbolic link (like a shortcut) to the target file.
Key differences from hard links:
- Can link to directories
- Can span file systems
- Breaks if target is deleted
To create a hard link instead:
Streams
What modes can Node.js streams operate in?
Streams can operate in:
- Binary mode (default): for buffers and strings
- Object mode: for any JavaScript value
Example of object mode:
File System
What type is the fd
parameter in this callback?
File descriptors are numbers that uniquely identify open files in the operating system.
The first three file descriptors are reserved:
- 0: stdin
- 1: stdout
- 2: stderr
Always remember to close file descriptors:
Encoding
How many bytes will this string take in UTF-8?
In UTF-8:
- ASCII characters (like ‘Hello ’) take 1 byte each
- The earth emoji 🌍 takes 4 bytes
So: 5 (Hello) + 1 (space) + 4 (🌍) = 10 bytes
To see the bytes:
I hope you enjoyed testing your NodeJS IO knowledge! Want more? Check out my Quiz Collection for more challenges!