0
0
Node.jsframework~20 mins

Buffer concatenation in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Buffer Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output of this Buffer concatenation code?
Consider the following Node.js code that concatenates two buffers. What will be the output when logged as a string?
Node.js
const buf1 = Buffer.from('Hello, ');
const buf2 = Buffer.from('World!');
const result = Buffer.concat([buf1, buf2]);
console.log(result.toString());
A"Hello, World!"
B"Hello World!"
C"Hello,World!"
D"HelloWorld!"
Attempts:
2 left
💡 Hint
Remember that Buffer.concat joins buffers exactly as they are without adding extra characters.
Predict Output
intermediate
1:00remaining
What is the length of the concatenated Buffer?
Given these buffers, what is the length of the resulting buffer after concatenation?
Node.js
const buf1 = Buffer.from('abc');
const buf2 = Buffer.from('defg');
const result = Buffer.concat([buf1, buf2]);
console.log(result.length);
A4
B6
C8
D7
Attempts:
2 left
💡 Hint
Length counts all bytes in both buffers combined.
🔧 Debug
advanced
1:30remaining
Why does this Buffer.concat call throw an error?
Examine the code below. Why does it throw a TypeError?
Node.js
const buf1 = Buffer.from('test');
const buf2 = 'not a buffer';
const result = Buffer.concat([buf1, buf2]);
ABecause buf2 is a string, not a Buffer instance
BBecause buf1 is not initialized correctly
CBecause Buffer.concat requires at least three buffers
DBecause Buffer.concat only accepts a single buffer argument
Attempts:
2 left
💡 Hint
Buffer.concat expects an array of Buffer objects only.
component_behavior
advanced
1:30remaining
What happens if you specify a total length smaller than the sum of buffers in Buffer.concat?
Consider this code snippet. What will be the output of the concatenated buffer as a string?
Node.js
const buf1 = Buffer.from('12345');
const buf2 = Buffer.from('67890');
const result = Buffer.concat([buf1, buf2], 8);
console.log(result.toString());
A"1234567"
B"12345678"
C"1234567890"
D"123456789"
Attempts:
2 left
💡 Hint
The total length limits the size of the resulting buffer.
📝 Syntax
expert
2:00remaining
Which option correctly concatenates buffers and converts to uppercase string?
Choose the code snippet that concatenates two buffers and outputs the uppercase string of the result.
A
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');
const result = Buffer.concat([buf1, buf2]).toUpperCase();
console.log(result.toString());
B
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');
const result = Buffer.concat(buf1, buf2);
console.log(result.toString().toUpperCase());
C
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');
const result = Buffer.concat([buf1, buf2]);
console.log(result.toString().toUpperCase());
D
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');
const result = Buffer.concat([buf1, buf2]);
console.log(result.toUpperCase());
Attempts:
2 left
💡 Hint
Remember Buffer.concat takes an array of buffers and toUpperCase() is a string method.