0
0
Node.jsframework~20 mins

Buffer allocation and encoding in Node.js - Practice Problems & Coding Challenges

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 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());
A"aaaaa"
B"aaaa"
C"\u0000\u0000\u0000\u0000\u0000"
D"a\u0000\u0000\u0000\u0000"
Attempts:
2 left
💡 Hint
Buffer.alloc(size, fill) fills the buffer with the given value.
Predict Output
intermediate
2: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);
A6
B4
C5
D10
Attempts:
2 left
💡 Hint
Each ASCII character is 1 byte in UTF-8.
Predict Output
advanced
2: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);
ATypeError: Buffer size must be a number
BRangeError: The value of "size" is out of range. It must be >= 0 and <= 2147483647. Received -1
CSyntaxError: Unexpected token '-'
DNo error, creates empty buffer
Attempts:
2 left
💡 Hint
Buffer size must be a non-negative integer.
Predict Output
advanced
2: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'));
A"caf?"
B"café"
C"cafÃ"
D"café"
Attempts:
2 left
💡 Hint
UTF-8 bytes interpreted as Latin1 characters can produce unexpected characters.
component_behavior
expert
3: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);
A5
B6
C11
D10
Attempts:
2 left
💡 Hint
Buffer.slice(start, end) includes bytes from start up to but not including end.