Challenge - 5 Problems
Exec Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of exec with simple command
What will be the output of this Node.js code using exec to run a shell command?
Node.js
import { exec } from 'child_process'; exec('echo Hello World', (error, stdout, stderr) => { if (error) { console.error(`Error: ${error.message}`); return; } if (stderr) { console.error(`Stderr: ${stderr}`); return; } console.log(`Output: ${stdout.trim()}`); });
Attempts:
2 left
💡 Hint
Remember that exec runs the command and returns its output in stdout.
✗ Incorrect
The exec function runs the shell command 'echo Hello World'. The output of this command is 'Hello World' which is captured in stdout. The code trims and prints it with 'Output: '.
❓ Predict Output
intermediate1:30remaining
Handling errors in exec callback
What will this code print if the command does not exist?
Node.js
import { exec } from 'child_process'; exec('nonexistentcommand', (error, stdout, stderr) => { if (error) { console.log(`Error: ${error.message}`); return; } console.log(`Output: ${stdout}`); });
Attempts:
2 left
💡 Hint
If the command is not found, exec returns an error with a message about failure.
✗ Incorrect
The exec function tries to run 'nonexistentcommand' which does not exist, so it returns an error. The error message includes 'Command failed: nonexistentcommand'.
❓ component_behavior
advanced2:00remaining
Behavior of exec with large output
What happens if you run exec with a command that produces a very large output exceeding the default buffer size?
Node.js
import { exec } from 'child_process'; exec('yes | head -n 1000000', (error, stdout, stderr) => { if (error) { console.log(`Error: ${error.message}`); return; } console.log(`Output length: ${stdout.length}`); });
Attempts:
2 left
💡 Hint
exec has a default maxBuffer size for stdout and stderr.
✗ Incorrect
The command produces a large output that exceeds exec's default maxBuffer size (1MB). This causes exec to return an error with message 'stdout maxBuffer exceeded'.
📝 Syntax
advanced1:30remaining
Correct syntax for exec import and usage
Which option shows the correct modern syntax to import and use exec from 'child_process' in Node.js ES modules?
Attempts:
2 left
💡 Hint
Node.js ES modules use import syntax with curly braces for named exports.
✗ Incorrect
Option D correctly imports exec as a named export and uses the callback style. Option D uses CommonJS require which is not ES modules. Option D tries default import which is invalid. Option D treats exec as returning a promise which it does not.
🔧 Debug
expert2:00remaining
Why does this exec code not print output?
Consider this code snippet:
import { exec } from 'child_process';
const result = exec('echo test');
console.log(result.stdout);
Why does this code not print 'test'?
Node.js
import { exec } from 'child_process'; const result = exec('echo test'); console.log(result.stdout);
Attempts:
2 left
💡 Hint
Check what exec returns and how output is accessed.
✗ Incorrect
exec returns a ChildProcess object immediately. The output is provided asynchronously via a callback. Accessing result.stdout directly does not give the command output.