0
0
Node.jsframework~20 mins

Worker thread vs child process in Node.js - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Parallelism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Difference in memory sharing between worker threads and child processes
In Node.js, what is the main difference in how worker threads and child processes handle memory?
AWorker threads share the same memory space, while child processes have separate memory spaces.
BChild processes share the same memory space, while worker threads have separate memory spaces.
CBoth worker threads and child processes share the same memory space.
DNeither worker threads nor child processes share memory; both use separate memory spaces.
Attempts:
2 left
💡 Hint
Think about how threads and processes work in general operating system terms.
component_behavior
intermediate
2:00remaining
Output behavior of worker thread vs child process
Consider this Node.js code snippet that creates a worker thread and a child process, both printing a message. What will be the order of outputs in the console?
Node.js
const { Worker } = require('worker_threads');
const { fork } = require('child_process');

console.log('Main start');

const worker = new Worker(`
  const { parentPort } = require('worker_threads');
  parentPort.postMessage('Worker done');
`, { eval: true });

const child = fork('./child.js');

worker.on('message', msg => console.log(msg));
child.on('message', msg => console.log(msg));

console.log('Main end');
A
Main start
Main end
Child done
Worker done
B
Main start
Worker done
Main end
Child done
C
Main start
Main end
Worker done
Child done
D
Worker done
Child done
Main start
Main end
Attempts:
2 left
💡 Hint
Remember that worker threads and child processes run asynchronously, but main thread logs synchronously.
🔧 Debug
advanced
2:00remaining
Identify the error in child process communication
This Node.js code tries to send a message from the parent to the child process. What error will occur when running this code?
Node.js
const { fork } = require('child_process');

const child = fork('child.js');

child.send('Hello parent');

// child.js content:
// process.on('message', msg => {
//   console.log('Message from parent:', msg);
// });
AError: The child process cannot send messages without a listener in the parent.
BTypeError: child.send is not a function.
CError: The child process file path is incorrect or missing.
DNo error; the message is sent successfully to the child process.
Attempts:
2 left
💡 Hint
Check if the parent is sending a message to the child process correctly.
📝 Syntax
advanced
2:00remaining
Correct syntax to create a worker thread with inline code
Which option correctly creates a worker thread in Node.js using inline code with the 'eval' option?
Anew Worker(`console.log('Hello from worker');`, { eval: true });
Bnew Worker(`const { parentPort } = require('worker_threads'); parentPort.postMessage('Hi');`, { eval: true });
Cnew Worker('console.log("Hello from worker")', { eval: true });
Dnew Worker('const { parentPort } = require('worker_threads'); parentPort.postMessage('Hi');');
Attempts:
2 left
💡 Hint
Remember that inline worker code must be a string and use eval: true to run.
state_output
expert
2:00remaining
State sharing behavior between worker threads and child processes
Given this scenario: A variable 'count' is incremented inside a worker thread and separately inside a child process. After both finish, what are the final values of 'count' in the main thread?
Node.js
let count = 0;

// Worker thread increments count by 1
// Child process increments count by 1

// After both finish, what is count in main thread?
Acount is 0 because neither worker thread nor child process can change main thread variables directly.
Bcount is 1 because only the worker thread shares memory with main thread.
Ccount is undefined because the variable is not shared at all.
Dcount is 2 because both increments affect the same variable.
Attempts:
2 left
💡 Hint
Think about how memory is shared or isolated between main thread, worker threads, and child processes.