Bird
Raised Fist0
Node.jsframework~20 mins

Creating buffers in Node.js - Practice Exercises

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does Buffer.alloc(10) do in Node.js?
easy
A. Creates a buffer of 10 bytes filled with zeros
B. Creates a buffer of 10 bytes filled with random data
C. Creates a buffer from a string of length 10
D. Creates an empty array of length 10

Solution

  1. Step 1: Understand Buffer.alloc usage

    Buffer.alloc(size) creates a buffer of the given size filled with zeros.
  2. Step 2: Analyze the argument 10

    The argument 10 means the buffer will have 10 bytes, all initialized to zero.
  3. Final Answer:

    Creates a buffer of 10 bytes filled with zeros -> Option A
  4. Quick Check:

    Buffer.alloc(10) = zero-filled buffer [OK]
Hint: Buffer.alloc creates zero-filled buffer of given size [OK]
Common Mistakes:
  • Thinking Buffer.alloc fills with random data
  • Confusing Buffer.alloc with Buffer.from
  • Assuming it creates an empty array
2. Which of the following is the correct syntax to create a buffer from the string 'hello'?
easy
A. Buffer.alloc('hello')
B. Buffer.from('hello')
C. new Buffer('hello')
D. Buffer.create('hello')

Solution

  1. Step 1: Recall correct Buffer creation methods

    In modern Node.js, Buffer.from(string) creates a buffer from a string.
  2. Step 2: Check options

    Buffer.alloc expects a size number, new Buffer is deprecated, and Buffer.create does not exist.
  3. Final Answer:

    Buffer.from('hello') -> Option B
  4. Quick Check:

    Buffer.from(string) creates buffer from string [OK]
Hint: Use Buffer.from(string) to create buffer from text [OK]
Common Mistakes:
  • Using Buffer.alloc with string argument
  • Using deprecated new Buffer() constructor
  • Assuming Buffer.create exists
3. What will be the output of the following code?
const buf = Buffer.from('abc');
console.log(buf.length);
medium
A. Error
B. 6
C. undefined
D. 3

Solution

  1. Step 1: Create buffer from string 'abc'

    Buffer.from('abc') creates a buffer with bytes representing 'a', 'b', 'c'.
  2. Step 2: Check buffer length

    The length property returns the number of bytes, which is 3 for 'abc'.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Buffer length of 'abc' = 3 [OK]
Hint: Buffer length equals number of bytes in string [OK]
Common Mistakes:
  • Assuming length is number of characters times 2
  • Expecting undefined or error
  • Confusing length with string length property
4. Identify the error in this code snippet:
const buf = Buffer.alloc('5');
console.log(buf.length);
medium
A. Buffer.alloc does not have length property
B. Buffer.alloc cannot be used without 'new'
C. Buffer.alloc expects a number, not a string
D. No error, code runs fine

Solution

  1. Step 1: Check Buffer.alloc argument type

    Buffer.alloc expects a number for size, but '5' is a string.
  2. Step 2: Understand type coercion in Buffer.alloc

    Passing a string causes a TypeError because size must be a number.
  3. Final Answer:

    Buffer.alloc expects a number, not a string -> Option C
  4. Quick Check:

    Buffer.alloc('5') causes type error [OK]
Hint: Pass number, not string, to Buffer.alloc size [OK]
Common Mistakes:
  • Using string instead of number for size
  • Thinking Buffer.alloc needs 'new' keyword
  • Assuming length property is missing
5. You want to create a buffer from an array of bytes [72, 101, 108, 108, 111] representing 'Hello'. Which code correctly creates this buffer and converts it back to a string?
hard
A. const buf = Buffer.from([72,101,108,108,111]); console.log(buf.toString());
B. const buf = Buffer.alloc([72,101,108,108,111]); console.log(buf.toString());
C. const buf = Buffer.from('72,101,108,108,111'); console.log(buf.toString());
D. const buf = Buffer.alloc(5); buf.write([72,101,108,108,111]); console.log(buf.toString());

Solution

  1. Step 1: Create buffer from array of bytes

    Buffer.from(array) creates a buffer from an array of byte values correctly.
  2. Step 2: Convert buffer back to string

    buf.toString() converts the buffer bytes to the string 'Hello'.
  3. Step 3: Analyze other options

    Using Buffer.alloc([72,101,108,108,111]) is wrong because alloc expects a number. Using Buffer.from('72,101,108,108,111') creates a buffer from the string of numbers, not bytes. Using buf.write([72,101,108,108,111]) is invalid as write doesn't accept arrays directly.
  4. Final Answer:

    const buf = Buffer.from([72,101,108,108,111]); console.log(buf.toString()); -> Option A
  5. Quick Check:

    Buffer.from(array) + toString() = 'Hello' [OK]
Hint: Use Buffer.from(array) to create buffer from bytes [OK]
Common Mistakes:
  • Using Buffer.alloc with array argument
  • Passing string of numbers instead of array
  • Trying to write array directly into buffer