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 Buffer allocation code?
Consider the following Node.js code snippet. What will be logged to the console?
Node.js
const buf = Buffer.alloc(5, 'a'); console.log(buf.toString());
Attempts:
2 left
💡 Hint
Buffer.alloc(size, fill) fills the buffer with the given value.
✗ Incorrect
Buffer.alloc(5, 'a') creates a buffer of length 5 filled with the byte for 'a'. Converting to string shows 'aaaaa'.
❓ Predict Output
intermediate2:00remaining
What does this Buffer.from() call output?
What will be the output of this code snippet?
Node.js
const buf = Buffer.from('hello', 'utf8'); console.log(buf.length);
Attempts:
2 left
💡 Hint
Each ASCII character is 1 byte in UTF-8.
✗ Incorrect
The string 'hello' has 5 characters, each encoded as 1 byte in UTF-8, so buffer length is 5.
❓ Predict Output
advanced2:00remaining
What error does this Buffer allocation code raise?
What error will this code produce when run in Node.js?
Node.js
const buf = Buffer.alloc(-1);Attempts:
2 left
💡 Hint
Buffer size must be a non-negative integer.
✗ Incorrect
Buffer.alloc() throws a RangeError if the size is negative.
❓ Predict Output
advanced2:00remaining
What is the output of this Buffer encoding conversion?
What will this code print to the console?
Node.js
const buf = Buffer.from('café', 'utf8'); console.log(buf.toString('latin1'));
Attempts:
2 left
💡 Hint
UTF-8 bytes interpreted as Latin1 characters can produce unexpected characters.
✗ Incorrect
The UTF-8 bytes for 'é' are two bytes, which when read as Latin1 produce 'é'.
❓ component_behavior
expert3:00remaining
How many bytes does this Buffer contain after slicing?
Given this code, what is the length of the resulting buffer?
Node.js
const buf = Buffer.from('hello world', 'utf8'); const slice = buf.slice(6, 11); console.log(slice.length);
Attempts:
2 left
💡 Hint
Buffer.slice(start, end) includes bytes from start up to but not including end.
✗ Incorrect
Slice from index 6 to 11 includes 5 bytes: 'world'.