Challenge - 5 Problems
Buffer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Node.js buffer code?
Consider the following code snippet that creates a buffer and writes a string into it. What will be logged to the console?
Node.js
const buf = Buffer.alloc(5); buf.write('HelloWorld'); console.log(buf.toString());
Attempts:
2 left
💡 Hint
Buffer.alloc creates a fixed size buffer. Writing beyond its size truncates the string.
✗ Incorrect
Buffer.alloc(5) creates a buffer of length 5. Writing 'HelloWorld' writes only the first 5 characters 'Hello'. The rest is ignored. So buf.toString() outputs 'Hello'.
❓ Predict Output
intermediate2:00remaining
What does this Buffer.from() call produce?
What will be the output of this code snippet?
Node.js
const buf = Buffer.from([65, 66, 67, 68]); console.log(buf.toString());
Attempts:
2 left
💡 Hint
Buffer.from with an array creates a buffer with those byte values.
✗ Incorrect
The array [65,66,67,68] corresponds to ASCII codes for 'A', 'B', 'C', 'D'. Buffer.from creates a buffer with those bytes. toString() converts bytes to string 'ABCD'.
❓ Predict Output
advanced2:00remaining
What error does this buffer code raise?
What error will this code produce when run?
Node.js
const buf = Buffer.allocUnsafe(3); buf.write('NodeJS', 0, 10); console.log(buf.toString());
Attempts:
2 left
💡 Hint
Writing more bytes than buffer size causes a range error.
✗ Incorrect
Buffer.allocUnsafe(3) creates a buffer of length 3. Trying to write 10 bytes starting at 0 exceeds buffer size, causing a RangeError.
❓ component_behavior
advanced2:00remaining
How does Buffer.concat behave with empty buffers?
Given the following code, what will be the output?
Node.js
const buf1 = Buffer.from('Hello'); const buf2 = Buffer.alloc(0); const buf3 = Buffer.from('World'); const result = Buffer.concat([buf1, buf2, buf3]); console.log(result.toString());
Attempts:
2 left
💡 Hint
Buffer.concat ignores empty buffers and joins the rest.
✗ Incorrect
Buffer.concat joins all buffers in order. Empty buffers add no content. So the result is 'Hello' + '' + 'World' = 'HelloWorld'.
📝 Syntax
expert2:00remaining
Which option correctly creates a zero-filled buffer of length 4?
Select the code snippet that creates a buffer of length 4 filled with zeros without any warnings or errors.
Attempts:
2 left
💡 Hint
Buffer.alloc creates zero-filled buffers safely.
✗ Incorrect
Buffer.alloc(4) creates a zero-filled buffer of length 4 safely. Buffer.from(4) is invalid. Buffer.allocUnsafe(4) creates uninitialized buffer, but fill(0) fills it with zeros (valid but less preferred). new Buffer(4) is deprecated and unsafe.