Bird
0
0

What is wrong with the following code snippet?

medium📝 Debug Q14 of 15
Node.js - Worker Threads
What is wrong with the following code snippet?
const sab = new SharedArrayBuffer(8);
const uint8 = new Uint8Array(sab);
uint8[0] = 255;
Atomics.store(uint8, 0, 256);
console.log(uint8[0]);
AAtomics.store cannot be used with Uint8Array
B256 is out of range for Uint8Array element
CSharedArrayBuffer size is too small
DUint8Array cannot be created from SharedArrayBuffer
Step-by-Step Solution
Solution:
  1. Step 1: Check Uint8Array element range

    Uint8Array elements can only hold values from 0 to 255. The value 256 is out of this range.
  2. Step 2: Understand effect of storing 256

    Storing 256 wraps around to 0 because 256 mod 256 = 0, so uint8[0] becomes 0, not 256.
  3. Final Answer:

    256 is out of range for Uint8Array element -> Option B
  4. Quick Check:

    Uint8 max value 255, 256 wraps to 0 [OK]
Quick Trick: Uint8Array values must be 0-255; higher values wrap [OK]
Common Mistakes:
  • Assuming Atomics.store rejects Uint8Array
  • Ignoring value wrapping behavior
  • Thinking SharedArrayBuffer size is insufficient

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes