Bird
0
0

What will be the output order of the following code?

medium📝 component behavior Q13 of 15
Node.js - Timers and Scheduling
What will be the output order of the following code?
console.log('start');
process.nextTick(() => console.log('nextTick'));
setImmediate(() => console.log('setImmediate'));
console.log('end');
Astart\nsetImmediate\nnextTick\nend
Bstart\nend\nnextTick\nsetImmediate
Cstart\nnextTick\nend\nsetImmediate
DnextTick\nstart\nend\nsetImmediate
Step-by-Step Solution
Solution:
  1. Step 1: Identify synchronous and asynchronous calls

    console.log('start') and console.log('end') run immediately in order. process.nextTick runs after current operation but before I/O. setImmediate runs after I/O.
  2. Step 2: Determine execution order

    Output order is: 'start' (sync), 'end' (sync), 'nextTick' (nextTick queue), then 'setImmediate' (next event loop cycle).
  3. Final Answer:

    start\nend\nnextTick\nsetImmediate -> Option B
  4. Quick Check:

    nextTick before setImmediate, sync logs first [OK]
Quick Trick: Sync logs first, nextTick before setImmediate [OK]
Common Mistakes:
  • Assuming setImmediate runs before nextTick
  • Mixing sync and async output order
  • Ignoring that nextTick runs before I/O

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes