Bird
0
0

You need to execute script.sh with arguments ['arg1', 'arg2'] using execFile and separately capture both standard output and error output. Which code snippet correctly achieves this?

hard📝 Application Q8 of 15
Node.js - Child Processes
You need to execute script.sh with arguments ['arg1', 'arg2'] using execFile and separately capture both standard output and error output. Which code snippet correctly achieves this?
AexecFile('./script.sh', ['arg1', 'arg2'], (error, stdout, stderr) => { if (error) { console.error('Error:', error); } console.log('Output:', stdout); console.error('Error Output:', stderr); });
BexecFile('./script.sh', ['arg1', 'arg2'], (error, output) => { console.log('Output:', output); });
CexecFile('./script.sh', (error, stdout, stderr) => { console.log(stdout, stderr); });
DexecFile('./script.sh', ['arg1', 'arg2'], (error) => { if (error) throw error; });
Step-by-Step Solution
Solution:
  1. Step 1: Use correct execFile signature

    Pass the executable path, arguments array, and a callback with (error, stdout, stderr).
  2. Step 2: Handle outputs separately

    Inside the callback, log or process stdout and stderr separately to capture standard and error outputs.
  3. Final Answer:

    Option A correctly captures both outputs separately. -> Option A
  4. Quick Check:

    Callback receives error, stdout, stderr separately [OK]
Quick Trick: Callback provides stdout and stderr separately [OK]
Common Mistakes:
  • Ignoring stderr output
  • Using incorrect callback parameters
  • Not passing arguments array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes