Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Node.js - Worker Threads
Identify the error in this code snippet:
const sab = new SharedArrayBuffer(8);
const uint8 = new Uint8Array();
uint8.buffer = sab;
ASharedArrayBuffer size must be 16 bytes or more.
BSharedArrayBuffer cannot be assigned to Uint8Array.buffer property.
CUint8Array does not exist in Node.js.
DUint8Array must be created with the buffer as constructor argument.
Step-by-Step Solution
Solution:
  1. Step 1: Check Uint8Array creation

    Uint8Array requires the buffer to be passed during construction, not assigned later.
  2. Step 2: Identify correct usage

    Correct syntax: new Uint8Array(sab); assigning buffer property directly is invalid.
  3. Final Answer:

    Uint8Array must be created with the buffer as constructor argument. -> Option D
  4. Quick Check:

    Correct typed array creation = A [OK]
Quick Trick: Pass buffer to typed array constructor, don't assign buffer property [OK]
Common Mistakes:
  • Assigning buffer property after creation
  • Thinking buffer property is writable
  • Confusing buffer with data property

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes