Bird
0
0

What will be logged to the console after executing this code?

medium📝 component behavior Q4 of 15
Node.js - Worker Threads
What will be logged to the console after executing this code?
const sab = new SharedArrayBuffer(8);
const uint16 = new Uint16Array(sab);
uint16[1] = 100;
console.log(uint16[1]);
A0
B100
Cundefined
DThrows an error
Step-by-Step Solution
Solution:
  1. Step 1: Buffer Size and TypedArray

    The buffer is 8 bytes, and Uint16Array uses 2 bytes per element, so it can hold 4 elements.
  2. Step 2: Assign and Access

    Setting uint16[1] = 100 stores 100 at the second element.
  3. Step 3: Logging

    Logging uint16[1] outputs 100.
  4. Final Answer:

    100 -> Option B
  5. Quick Check:

    TypedArray indexing matches stored value [OK]
Quick Trick: TypedArray elements reflect assigned values [OK]
Common Mistakes:
  • Confusing byte size with element count
  • Expecting zero or undefined instead of assigned value
  • Assuming error due to SharedArrayBuffer

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes