0
0
Node.jsframework~20 mins

Creating buffers in Node.js - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Buffer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
AHello
BError: Buffer overflow
CHello\u0000
DHelloWorld
Attempts:
2 left
💡 Hint
Buffer.alloc creates a fixed size buffer. Writing beyond its size truncates the string.
Predict Output
intermediate
2: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());
AABCD
B[65,66,67,68]
C65666768
DError: Invalid argument
Attempts:
2 left
💡 Hint
Buffer.from with an array creates a buffer with those byte values.
Predict Output
advanced
2: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());
ASyntaxError: Unexpected token
BTypeError: Invalid argument
CNo error, outputs 'NodeJS'
DRangeError: Attempt to write outside buffer bounds
Attempts:
2 left
💡 Hint
Writing more bytes than buffer size causes a range error.
component_behavior
advanced
2: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());
AHello\u0000World
BHelloWorld
CHello
DWorld
Attempts:
2 left
💡 Hint
Buffer.concat ignores empty buffers and joins the rest.
📝 Syntax
expert
2: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.
Aconst buf = new Buffer(4);
Bconst buf = Buffer.from(4);
Cconst buf = Buffer.alloc(4);
Dconst buf = Buffer.allocUnsafe(4).fill(0);
Attempts:
2 left
💡 Hint
Buffer.alloc creates zero-filled buffers safely.