Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the exec function in Node.js?
The exec function runs shell commands from a Node.js script, letting you execute system commands and get their output.
Click to reveal answer
beginner
Which Node.js module provides the exec function?
The child_process module provides the exec function to run shell commands.
Click to reveal answer
intermediate
What are the three main arguments of exec(command, options, callback)?
1. command: The shell command string to run. 2. options: Optional settings like environment variables. 3. callback: Function called with error, stdout, and stderr after command finishes.
Click to reveal answer
intermediate
How do you handle errors when using exec?
Check if the error argument in the callback is set. If yes, the command failed or had issues. You can then handle or log the error.
Click to reveal answer
beginner
What is a simple example of using exec to list files in the current directory?
The callback receives error first, then stdout. Option C uses correct argument order.
What is a common use case for exec in Node.js?
AManipulating arrays
BRunning system shell commands and getting their output
CCreating HTTP servers
DReading files from disk
✗ Incorrect
exec is used to run shell commands and capture their output.
Explain how to use exec to run a shell command and handle its output and errors.
Think about the three arguments in the callback and how to safely use them.
You got /5 concepts.
Describe a simple example where exec is used to list files in a directory and print them.
Remember the common 'ls' command and how to handle callback arguments.
You got /4 concepts.
Practice
(1/5)
1. What does the exec function in Node.js primarily do?
easy
A. Runs shell commands from within a Node.js program
B. Creates a new HTTP server
C. Reads files from the filesystem
D. Starts a database connection
Solution
Step 1: Understand the purpose of exec
The exec function is designed to run shell commands from Node.js code.
Step 2: Compare with other options
Creating servers, reading files, or database connections are unrelated to exec.
Final Answer:
Runs shell commands from within a Node.js program -> Option A
Quick Check:
exec runs shell commands = B [OK]
Hint: Remember exec runs shell commands, not servers or files [OK]
Common Mistakes:
Confusing exec with file reading functions
Thinking exec creates servers
Assuming exec manages databases
2. Which is the correct way to import and use exec from the child_process module in Node.js?
easy
A. import exec from 'child_process';
B. const exec = require('child_process').exec;
C. const exec = require('exec');
D. import { exec } from 'child_process';
Solution
Step 1: Recall Node.js import syntax for exec
In Node.js CommonJS, exec is imported as const exec = require('child_process').exec;.
Step 2: Check other options for correctness
import exec from 'child_process'; uses ES module default import incorrectly; const exec = require('exec'); tries to require a non-existent module; import { exec } from 'child_process'; is ES module named import but Node.js needs special config.
Final Answer:
const exec = require('child_process').exec; -> Option B
Quick Check:
CommonJS import for exec = A [OK]
Hint: Use require('child_process').exec for standard Node.js [OK]
Common Mistakes:
Using import without Node.js ES module setup
Requiring wrong module name
Confusing default and named imports
3. What will the following Node.js code output if the current directory contains a file named test.txt?
C. Missing error return causes stdout to print even on error
D. exec is not imported correctly
Solution
Step 1: Check error handling logic
The code logs error but does not return or stop, so stdout logs even if error occurs.
Step 2: Understand impact of missing return
This can cause confusing output mixing error and stdout, which is not ideal.
Final Answer:
Missing error return causes stdout to print even on error -> Option C
Quick Check:
Missing error return causes stdout to print even on error = D [OK]
Hint: Return after error to avoid printing stdout [OK]
Common Mistakes:
Not returning after error check
Assuming exec import is wrong
Thinking command syntax is incorrect
5. You want to run a shell command that lists all files but only print those containing the word 'log'. Which exec usage correctly achieves this in Node.js?
hard
A. exec('ls > grep log', (err, stdout) => { if (!err) console.log(stdout); });
B. exec('ls && grep log', (err, stdout) => { if (!err) console.log(stdout); });
C. exec('ls | find log', (err, stdout) => { if (!err) console.log(stdout); });
D. exec('ls | grep log', (err, stdout) => { if (!err) console.log(stdout); });
Solution
Step 1: Understand shell command chaining
The pipe | sends output of ls to grep log to filter lines containing 'log'.
Step 2: Evaluate each option's command
ls | grep log correctly pipes output to grep for filtering; ls > grep log uses redirection to create a file; ls && grep log runs sequentially without piping; ls | find log pipes to filesystem search tool unsuitable for text filtering.
Final Answer:
exec('ls | grep log', (err, stdout) => { if (!err) console.log(stdout); }); -> Option D
Quick Check:
Pipe output to grep for filtering = C [OK]
Hint: Use pipe (|) to filter command output with grep [OK]