0
0
Javascriptprogramming~20 mins

Synchronous vs asynchronous execution in Javascript - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this synchronous code?
Consider the following JavaScript code snippet. What will be printed to the console?
Javascript
console.log('Start');
console.log('Middle');
console.log('End');
AEnd\nMiddle\nStart
BMiddle\nStart\nEnd
CStart\nEnd\nMiddle
DStart\nMiddle\nEnd
Attempts:
2 left
💡 Hint
Synchronous code runs line by line in order.
Predict Output
intermediate
2:00remaining
What is the output of this asynchronous code with setTimeout?
Look at this JavaScript code using setTimeout. What will be printed to the console and in what order?
Javascript
console.log('First');
setTimeout(() => console.log('Second'), 0);
console.log('Third');
AFirst\nSecond\nThird
BFirst\nThird\nSecond
CSecond\nFirst\nThird
DThird\nFirst\nSecond
Attempts:
2 left
💡 Hint
setTimeout with 0 delay runs after the current call stack is empty.
Predict Output
advanced
2:00remaining
What is the output of this Promise-based asynchronous code?
Analyze this JavaScript code using Promises. What will be printed to the console and in what order?
Javascript
console.log('A');
Promise.resolve().then(() => console.log('B'));
console.log('C');
AA\nC\nB
BA\nB\nC
CB\nA\nC
DC\nA\nB
Attempts:
2 left
💡 Hint
Promise.then callbacks run after the current synchronous code but before setTimeout callbacks.
Predict Output
advanced
2:00remaining
What error does this asynchronous code cause?
What error will this JavaScript code produce when run?
Javascript
setTimeout(() => {
  console.log(x);
}, 10);

let x = 5;
ATypeError: x is not a function
Bundefined
C5
DReferenceError: Cannot access 'x' before initialization
Attempts:
2 left
💡 Hint
Variables declared with let are hoisted but not initialized until their declaration line.
🧠 Conceptual
expert
2:00remaining
Which option best describes the JavaScript event loop behavior?
Choose the correct statement about how JavaScript handles synchronous and asynchronous code execution with the event loop.
ASynchronous code runs first, then microtasks, then macrotasks in order.
BMacrotasks run before synchronous code and microtasks.
CMicrotasks run only after all macrotasks are completed.
DAsynchronous code runs before any synchronous code.
Attempts:
2 left
💡 Hint
Remember the order: synchronous code, then microtasks, then macrotasks.