0
0
Node.jsframework~20 mins

spawn for streaming processes in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spawn Streaming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Node.js spawn code?
Consider this code snippet using spawn from child_process. What will be printed to the console?
Node.js
import { spawn } from 'child_process';

const ls = spawn('ls', ['-l']);

ls.stdout.on('data', (data) => {
  console.log(`Output chunk: ${data.toString()}`);
});

ls.stderr.on('data', (data) => {
  console.error(`Error chunk: ${data.toString()}`);
});

ls.on('close', (code) => {
  console.log(`Process exited with code ${code}`);
});
AThe console prints the directory listing in chunks, then 'Process exited with code 0'.
BThe console prints nothing because spawn does not emit data events.
CThe console prints an error because 'ls' is not a valid command in Node.js.
DThe console prints 'Process exited with code 1' immediately without output.
Attempts:
2 left
💡 Hint
Think about how spawn streams output and how events are emitted.
📝 Syntax
intermediate
2:00remaining
Which option correctly spawns a process to stream output line-by-line?
You want to spawn a process and handle its output line-by-line as it streams. Which code snippet correctly sets this up?
A
const proc = spawn('ping', ['-c', '4', 'google.com']);
proc.stdout.on('data', (line) => {
  console.log(line);
});
B
const proc = spawn('ping', ['-c', '4', 'google.com']);
proc.stdout.on('line', (line) => {
  console.log(line);
});
C
const proc = spawn('ping', ['-c', '4', 'google.com']);
proc.stdout.on('data', (data) => {
  data.toString().split('\n').forEach(line => console.log(line));
});
D
const proc = spawn('ping', ['-c', '4', 'google.com']);
proc.stdout.on('chunk', (chunk) => {
  console.log(chunk.toString());
});
Attempts:
2 left
💡 Hint
Remember that 'data' events emit buffers, not lines.
🔧 Debug
advanced
2:00remaining
Why does this spawn code never print output?
This code spawns a process but never prints any output. What is the cause?
Node.js
import { spawn } from 'child_process';

const proc = spawn('node', ['-e', "console.log('hello')"]);

proc.stdout.on('data', (data) => {
  console.log(`Output: ${data}`);
});
AThe process exits before the event listener is attached, missing the output.
BThe code is correct and will print 'Output: hello' as expected.
CThe output is sent to stderr, not stdout, so 'data' event on stdout never fires.
DThe process output is buffered and not flushed, so no 'data' event fires.
Attempts:
2 left
💡 Hint
Check if the output is sent to stdout or stderr and if listeners are attached properly.
state_output
advanced
2:00remaining
What is the value of variable 'output' after this spawn code runs?
Given this code, what will be the final value of the 'output' variable after the process closes?
Node.js
import { spawn } from 'child_process';

let output = '';
const proc = spawn('echo', ['Hello World']);

proc.stdout.on('data', (data) => {
  output += data.toString();
});

proc.on('close', () => {
  console.log('Process done');
});
A'Hello World\n'
B'Hello World'
C'' (empty string)
Dundefined
Attempts:
2 left
💡 Hint
Remember what the echo command outputs including line breaks.
🧠 Conceptual
expert
2:00remaining
Which option best explains why spawn is preferred over exec for streaming large outputs?
Why do developers often choose spawn instead of exec when they want to process large output streams from child processes?
Aspawn automatically parses output into JSON, while exec returns raw strings.
Bspawn can only run shell commands, while exec can run any executable.
Cspawn runs processes asynchronously, but exec runs them synchronously blocking the event loop.
Dspawn streams output data in chunks, avoiding buffer size limits, while exec buffers all output in memory before returning.
Attempts:
2 left
💡 Hint
Think about memory usage and how output is handled in both methods.