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 helps handle data like files, network packets, or streams efficiently.
Click to reveal answer
beginner
How do you allocate a Buffer of 10 bytes filled with zeros?
Use Buffer.alloc(10). This creates a Buffer of length 10 filled with zeros safely.
Click to reveal answer
intermediate
What is the difference between Buffer.alloc() and Buffer.allocUnsafe()?
Buffer.alloc() creates a zero-filled Buffer for safety. Buffer.allocUnsafe() creates a Buffer faster but with old data inside, so you must overwrite it before use.
Click to reveal answer
beginner
How do you convert a string to a Buffer with UTF-8 encoding?
Use Buffer.from('your string', 'utf8'). This encodes the string into bytes using UTF-8.
Click to reveal answer
intermediate
What encoding options can you use when creating or converting Buffers?
Common encodings include 'utf8', 'ascii', 'base64', 'hex', and 'latin1'. They tell Node.js how to interpret or convert the bytes.
Click to reveal answer
Which method creates a Buffer filled with zeros?
ABuffer.create(size)
BBuffer.allocUnsafe(size)
CBuffer.from(size)
DBuffer.alloc(size)
✗ Incorrect
Buffer.alloc(size) creates a zero-filled Buffer safely.
What does Buffer.allocUnsafe(size) do?
ACreates a Buffer with uninitialized memory (old data)
BCreates a Buffer filled with zeros
CCreates a Buffer from a string
DThrows an error
✗ Incorrect
Buffer.allocUnsafe(size) creates a Buffer quickly but with old data inside, so it is unsafe until overwritten.
How do you convert a string 'hello' to a Buffer with UTF-8 encoding?
ABuffer.alloc('hello')
BBuffer.encode('hello')
CBuffer.from('hello', 'utf8')
DBuffer.toString('hello')
✗ Incorrect
Buffer.from('hello', 'utf8') converts the string to a Buffer using UTF-8 encoding.
Which encoding is NOT commonly used with Buffers?
Ajson
Bbase64
Cutf8
Dhex
✗ Incorrect
'json' is not an encoding for Buffers; common encodings are utf8, base64, hex, ascii, and latin1.
Why should you be careful using Buffer.allocUnsafe()?
AIt fills Buffer with zeros
BIt may contain old sensitive data until overwritten
CIt is slower than Buffer.alloc()
DIt does not create a Buffer
✗ Incorrect
Buffer.allocUnsafe() may contain old data, so you must overwrite it before use to avoid leaks.
Explain how to create a Buffer in Node.js and why encoding matters.
Think about how raw data and strings differ and how encoding helps convert between them.
You got /4 concepts.
Describe the risks and benefits of using Buffer.allocUnsafe() compared to Buffer.alloc().
Consider safety versus performance trade-offs.
You got /3 concepts.
Practice
(1/5)
1. What does Buffer.alloc(5) do in Node.js?
easy
A. Creates a buffer of length 5 filled with zeros
B. Creates a buffer of length 5 filled with random data
C. Creates a buffer from a string of length 5
D. Allocates memory but does not initialize the buffer
Solution
Step 1: Understand Buffer.alloc behavior
Buffer.alloc(size) creates a buffer of the given size and fills it with zeros for safety.
Step 2: Apply to size 5
Calling Buffer.alloc(5) creates a buffer of length 5 filled with zeros.
Final Answer:
Creates a buffer of length 5 filled with zeros -> Option A
Quick Check:
Buffer.alloc(5) = zero-filled buffer [OK]
Hint: Buffer.alloc always zero-fills the buffer size [OK]
Common Mistakes:
Thinking Buffer.alloc fills with random data
Confusing Buffer.alloc with Buffer.from
Assuming buffer is uninitialized
2. Which of the following is the correct syntax to create a buffer from the string 'hello' using UTF-8 encoding?
easy
A. Buffer.alloc('hello', 'utf8')
B. Buffer.create('hello', 'utf8')
C. new Buffer('hello', 'utf8')
D. Buffer.from('hello', 'utf8')
Solution
Step 1: Identify correct Buffer creation method
In modern Node.js, Buffer.from() creates a buffer from a string with encoding.
Step 2: Check syntax correctness
Buffer.from('hello', 'utf8') is correct syntax; Buffer.alloc does not accept string input, and Buffer.create does not exist. new Buffer() is deprecated.
Final Answer:
Buffer.from('hello', 'utf8') -> Option D
Quick Check:
Use Buffer.from for string buffers [OK]
Hint: Use Buffer.from for strings, Buffer.alloc for size [OK]
Buffer.alloc(size, fill) accepts a size and a fill value. The fill can be a string, which repeats to fill the buffer.
Step 2: Understand fill behavior and toString()
Filling 5 bytes with 'abc' repeats 'abcab'. toString() converts buffer back to string.
Final Answer:
No error, prints 'abcab' -> Option C
Quick Check:
Buffer.alloc fills string repeatedly [OK]
Hint: Buffer.alloc fills string repeatedly if fill is string [OK]
Common Mistakes:
Thinking Buffer.alloc fill cannot be string
Assuming toString() is invalid on Buffer
Expecting error due to fill length mismatch
5. You want to create a buffer from the string 'café' and then convert it back to a string. Which encoding should you use to preserve the accented character correctly?
hard
A. 'ascii'
B. 'utf8'
C. 'base64'
D. 'hex'
Solution
Step 1: Understand character encoding for accented characters
ASCII encoding cannot represent accented characters like 'é'. UTF-8 supports all Unicode characters including accented ones.
Step 2: Choose encoding for buffer creation and conversion
Using 'utf8' ensures the accented character is preserved when converting to and from buffer.
Final Answer:
'utf8' -> Option B
Quick Check:
Use UTF-8 for accented characters [OK]
Hint: Use UTF-8 to handle accented characters correctly [OK]
Common Mistakes:
Using ASCII which drops accents
Using base64 or hex which are encoding formats, not text encodings