Challenge - 5 Problems
Node.js Fork Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Node.js fork example?
Consider this code that uses
fork from the child_process module. What will be printed to the console?Node.js
import { fork } from 'child_process'; const child = fork('./child.js'); child.on('message', (msg) => { console.log('Parent received:', msg); }); child.send('Hello child'); // child.js content: // process.on('message', (msg) => { // process.send(msg + ' world'); // });
Attempts:
2 left
💡 Hint
Remember that
fork creates a child process that can communicate via messages.✗ Incorrect
The parent sends 'Hello child' to the child process. The child appends ' world' and sends it back. The parent logs the full message.
📝 Syntax
intermediate1:30remaining
Which option correctly imports and uses fork in Node.js ES modules?
You want to create a child process using
fork in a Node.js ES module. Which code snippet is correct?Attempts:
2 left
💡 Hint
ES modules use
import syntax and fork requires a script path.✗ Incorrect
Option D correctly imports
fork and calls it with a script path. Option D uses CommonJS syntax, invalid in ES modules. Option D imports default which does not exist. Option D calls fork without a script path, causing an error.🔧 Debug
advanced2:30remaining
Why does this forked child process not receive messages?
Given this parent and child code, why does the child never log the message sent by the parent?
Node.js
/* parent.js */ import { fork } from 'child_process'; const child = fork('./child.js'); child.send('ping'); /* child.js */ console.log('Child started'); process.on('message', (msg) => { console.log('Child received:', msg); });
Attempts:
2 left
💡 Hint
Check if the child process stays alive to receive messages.
✗ Incorrect
If the child process does not keep running (e.g., no event loop tasks), it exits before processing messages. The parent sends 'ping', but the child exits immediately after logging 'Child started'.
❓ state_output
advanced2:00remaining
What is the value of variable 'count' after this forked process communication?
In this example, the parent and child share a variable 'count'. What is the final value of 'count' in the parent after the child sends messages?
Node.js
import { fork } from 'child_process'; let count = 0; const child = fork('./child.js'); child.on('message', (msg) => { if (msg === 'increment') count++; }); child.send('start'); // child.js // process.on('message', (msg) => { // if (msg === 'start') { // process.send('increment'); // process.send('increment'); // } // });
Attempts:
2 left
💡 Hint
The child sends two 'increment' messages, each incrementing count by 1.
✗ Incorrect
The parent sends 'start' to the child process. The child responds by sending two 'increment' messages. The parent increments count once for each 'increment' message, resulting in count = 2.
🧠 Conceptual
expert3:00remaining
Which statement about Node.js fork and IPC is true?
Select the correct statement about the behavior of
fork and inter-process communication (IPC) in Node.js.Attempts:
2 left
💡 Hint
Think about how processes communicate and memory isolation.
✗ Incorrect
Forked child processes run in separate memory spaces. Communication happens via asynchronous message passing using serialization. The fork method can run any JS file by path. File descriptors are not automatically inherited unless specified.