0
0
Node.jsframework~20 mins

Master and worker processes in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cluster Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Node.js cluster master process code output?
Consider this Node.js code using the cluster module. What will be printed to the console when you run it?
Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  console.log('Master process is running');
  const worker = cluster.fork();
  worker.on('online', () => {
    console.log('Worker is online');
  });
} else {
  console.log('Worker process started');
}
AMaster process is running\nWorker is online\nWorker process started
BWorker process started\nMaster process is running\nWorker is online
CMaster process is running\nWorker process started\nWorker is online
DWorker is online\nMaster process is running\nWorker process started
Attempts:
2 left
💡 Hint
Remember that the master process runs first and forks the worker. The worker prints its message after it starts.
📝 Syntax
intermediate
1:30remaining
Which option correctly creates a worker process using Node.js cluster?
Select the code snippet that correctly forks a worker process using the cluster module in Node.js.
Aconst worker = cluster.spawn();
Bconst worker = cluster.fork();
Cconst worker = cluster.createWorker();
Dconst worker = cluster.newWorker();
Attempts:
2 left
💡 Hint
Check the official cluster module API for the method to create a worker.
🔧 Debug
advanced
2:30remaining
Why does this cluster worker never start?
Given this code, the worker process never logs 'Worker started'. What is the cause?
Node.js
import cluster from 'cluster';

if (cluster.isPrimary) {
  cluster.fork();
} else if (cluster.isWorker) {
  console.log('Worker started');
}
AThe worker code runs only if 'cluster.isWorker' is true, but the correct property is 'cluster.isWorker'.
BThe property 'cluster.isWorker' does not exist; it should be 'cluster.isWorkerProcess'.
CThe worker code is unreachable because 'cluster.isPrimary' is always true.
DThe condition should be 'if (cluster.isWorker)' instead of 'else if'.
Attempts:
2 left
💡 Hint
Check the cluster module properties for identifying master and worker processes.
state_output
advanced
2:00remaining
What is the value of 'workerCount' after this code runs?
This code counts active workers. What is the value of 'workerCount' after execution?
Node.js
import cluster from 'cluster';

let workerCount = 0;

if (cluster.isPrimary) {
  cluster.fork();
  cluster.fork();
  workerCount = Object.keys(cluster.workers).length;
} else {
  // worker code
}
A2
B0
C1
Dundefined
Attempts:
2 left
💡 Hint
cluster.workers is an object with keys for each worker process.
🧠 Conceptual
expert
3:00remaining
Which statement about Node.js cluster master and worker processes is true?
Select the correct statement about how master and worker processes communicate in Node.js cluster.
AWorkers automatically inherit all open network connections from the master without explicit code.
BMaster and workers communicate only through shared memory variables.
CWorkers can send messages to the master using process.send(), and master listens with worker.on('message').
DMaster and workers share the same event loop and memory space.
Attempts:
2 left
💡 Hint
Think about how separate processes exchange data in Node.js cluster.