Bird
0
0

What will the following code print if the command node -v runs successfully?

medium📝 component behavior Q5 of 15
Node.js - Child Processes
What will the following code print if the command node -v runs successfully?
const { exec } = require('child_process');
exec('node -v', (error, stdout, stderr) => {
  if (error) {
    console.log('Error occurred');
  } else if (stderr) {
    console.log('Standard error output');
  } else {
    console.log(stdout.trim());
  }
});
AStandard error output
BError occurred
CThe Node.js version string, e.g., v20.0.0
DNothing prints
Step-by-Step Solution
Solution:
  1. Step 1: Understand command output

    node -v prints the Node.js version to stdout.
  2. Step 2: Check callback logic

    If no error or stderr, the trimmed stdout (version string) is printed.
  3. Final Answer:

    The Node.js version string, e.g., v20.0.0 -> Option C
  4. Quick Check:

    exec node -v prints version string [OK]
Quick Trick: Successful exec prints stdout trimmed, usually command output [OK]
Common Mistakes:
  • Confusing stderr with stdout
  • Expecting error when command is valid
  • Not trimming stdout before printing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes