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
Recall & Review
beginner
What is a Buffer in Node.js?
A Buffer is a special object in Node.js used to store raw binary data. It allows you to work with streams of bytes directly, like reading files or network data.
Click to reveal answer
beginner
How do you create a Buffer from a string in Node.js?
Use Buffer.from('your string') to create a Buffer containing the bytes of the string.
Click to reveal answer
intermediate
Which method reads an unsigned 8-bit integer from a Buffer at a specific offset?
The method buffer.readUInt8(offset) reads an unsigned 8-bit integer from the Buffer at the given offset.
Click to reveal answer
intermediate
How do you write a 16-bit unsigned integer to a Buffer at a specific offset?
Use buffer.writeUInt16LE(value, offset) to write a 16-bit unsigned integer in little-endian format at the offset.
Click to reveal answer
advanced
Why is it important to specify endianness when reading or writing multi-byte values in a Buffer?
Endianness determines byte order. Specifying it ensures data is interpreted correctly across different systems, preventing errors in reading or writing multi-byte numbers.
Click to reveal answer
Which method creates a Buffer from a string in Node.js?
Anew Buffer('text')
BBuffer.from('text')
CBuffer.create('text')
DBuffer.string('text')
✗ Incorrect
Buffer.from('text') is the modern and safe way to create a Buffer from a string.
What does buffer.readUInt8(0) return?
AAn unsigned 8-bit integer from the start of the buffer
BA signed 8-bit integer from the start of the buffer
CA string from the buffer
DThe length of the buffer
✗ Incorrect
readUInt8 reads an unsigned 8-bit integer at the specified offset.
Which method writes a 32-bit unsigned integer in little-endian format to a Buffer?
Abuffer.writeUInt32LE(value, offset)
Bbuffer.writeUInt32BE(value, offset)
Cbuffer.writeInt32LE(value, offset)
Dbuffer.writeInt32BE(value, offset)
✗ Incorrect
writeUInt32LE writes a 32-bit unsigned integer in little-endian byte order.
What is the default encoding when converting a Buffer to a string using buffer.toString()?
Ahex
Bascii
Cbase64
Dutf8
✗ Incorrect
utf8 is the default encoding for buffer.toString() in Node.js.
Why should you be careful with the offset when reading or writing Buffer data?
AOffset controls the Buffer size
BBecause offset changes the encoding
CTo avoid reading or writing outside the Buffer bounds causing errors
DOffset is only used for strings
✗ Incorrect
Using an invalid offset can cause runtime errors or corrupt data.
Explain how to read and write numeric data using Node.js Buffers. Include how to handle byte order.
Think about how bytes are stored and accessed in memory.
You got /4 concepts.
Describe how to convert a string to a Buffer and back to a string in Node.js.
Consider how text is stored as bytes.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of a Buffer in Node.js?
easy
A. To manage HTTP requests automatically
B. To store JavaScript objects in memory
C. To hold raw binary data for reading and writing
D. To format strings for display in the console
Solution
Step 1: Understand what Buffer stores
A Buffer is designed to hold raw binary data, not objects or formatted strings.
Step 2: Identify Buffer's main use
Buffers allow reading and writing bytes directly, useful for binary data handling.
Final Answer:
To hold raw binary data for reading and writing -> Option C
Quick Check:
Buffer = raw binary data [OK]
Hint: Buffers store raw bytes, not objects or formatted text [OK]
Common Mistakes:
Thinking Buffer stores JavaScript objects
Confusing Buffer with string formatting tools
Assuming Buffer manages HTTP requests
2. Which of the following is the correct way to create a Buffer of size 5 bytes filled with zeros?
easy
A. Buffer.new(5)
B. Buffer.alloc(5)
C. new Buffer(5, 0)
D. Buffer.create(5)
Solution
Step 1: Recall Buffer creation methods
In modern Node.js, Buffer.alloc(size) creates a zero-filled buffer.
Step 2: Check other options
Buffer.new and Buffer.create do not exist; new Buffer() is deprecated and unsafe.
Final Answer:
Buffer.alloc(5) -> Option B
Quick Check:
Use Buffer.alloc for safe zero-filled buffers [OK]
Hint: Use Buffer.alloc(size) for zero-filled buffers [OK]
Common Mistakes:
Using deprecated new Buffer() constructor
Trying non-existent Buffer.new or Buffer.create methods