0
0
Node.jsframework~20 mins

Buffer to string conversion in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Buffer Conversion Master
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 to string conversion?
Consider the following code snippet that converts a buffer to a string. What will be printed to the console?
Node.js
const buf = Buffer.from([72, 101, 108, 108, 111]);
console.log(buf.toString());
A"undefined"
B"[72,101,108,108,111]"
C"Buffer(5)"
D"Hello"
Attempts:
2 left
💡 Hint
Think about what the buffer contains and what toString() does by default.
Predict Output
intermediate
2:00remaining
What does this code output when specifying encoding?
What will this code print to the console?
Node.js
const buf = Buffer.from('68656c6c6f', 'hex');
console.log(buf.toString('utf8'));
A"hello"
B"68656c6c6f"
C"undefined"
D"\u6865\u6c6c\u6f"
Attempts:
2 left
💡 Hint
The buffer is created from hex values representing ASCII characters.
component_behavior
advanced
2:00remaining
What happens if you convert a buffer with invalid UTF-8 bytes to string?
Given this buffer with invalid UTF-8 bytes, what will be the output of toString()?
Node.js
const buf = Buffer.from([0xff, 0xfe, 0xfd]);
console.log(buf.toString('utf8'));
A"\xff\xfe\xfd"
BThrows a SyntaxError
C"���"
D"" (empty string)
Attempts:
2 left
💡 Hint
Invalid UTF-8 bytes are replaced with a special character when converted to string.
📝 Syntax
advanced
2:00remaining
Which option correctly converts a buffer to a base64 string?
Select the code snippet that correctly converts a buffer to a base64 encoded string.
Aconst b64 = buf.toString('base64');
Bconst b64 = buf.encode('base64');
Cconst b64 = Buffer.toString(buf, 'base64');
Dconst b64 = buf.toBase64();
Attempts:
2 left
💡 Hint
Check the Buffer API for the correct method and parameter to convert to base64.
🔧 Debug
expert
3:00remaining
Why does this buffer to string conversion produce unexpected output?
This code is intended to convert a buffer to a UTF-8 string, but the output is garbled. What is the most likely cause?
Node.js
const buf = Buffer.from('c3a9', 'utf8');
console.log(buf.toString());
ABuffer.from() cannot accept strings, only arrays
BThe input string is hex but encoded as UTF-8, causing wrong bytes in buffer
CtoString() requires an encoding parameter to decode properly
DThe buffer is empty, so output is empty string
Attempts:
2 left
💡 Hint
Consider what the input string represents and how Buffer.from interprets it with the given encoding.