Asynchronous JavaScript is a key concept for handling tasks without blocking the main thread. It includes callbacks, promises, and chaining to manage asynchronous operations efficiently. Test your knowledge with these 30 MCQs covering Synchronous vs Asynchronous JavaScript, Callbacks, Promises, and Promise Chaining.
Synchronous vs. Asynchronous JavaScript
What is the primary difference between synchronous and asynchronous JavaScript?
A) Synchronous JavaScript executes in parallel
B) Asynchronous JavaScript executes sequentially
C) Synchronous JavaScript executes sequentially
D) Asynchronous JavaScript blocks the main thread
Which of the following statements is true about asynchronous JavaScript?
A) It blocks the main thread
B) It allows the program to perform other tasks while waiting for a response
C) It executes functions one after the other
D) It is faster than synchronous JavaScript in all cases
Which method can you use to handle asynchronous operations in JavaScript?
A) forEach()
B) setTimeout()
C) alert()
D) console.log()
What happens when a synchronous function runs in JavaScript?
A) It runs in parallel with other code
B) It waits for other tasks to complete
C) It blocks the execution of subsequent code until it completes
D) It executes the callback function immediately
How does JavaScript handle asynchronous operations in the event loop?
A) It executes all synchronous tasks first, then moves to asynchronous tasks
B) It executes asynchronous tasks before synchronous tasks
C) It ignores asynchronous tasks
D) It executes both at the same time
Introduction to Callbacks
A callback function in JavaScript is:
A) A function that executes after another function finishes
B) A function that runs asynchronously in parallel
C) A function that is executed first
D) A function used to return data
Which of the following is an example of a callback in JavaScript?
A) setTimeout(() => console.log('Hello'), 1000)
B) function add(a, b) { return a + b; }
C) function multiply(a, b) { return a * b; }
D) document.getElementById('myElement').innerText
How do you pass a callback function in JavaScript?
A) By returning the function directly
B) By invoking the function immediately
C) By passing the function name as an argument to another function
D) By using a promise
What is a potential issue with using callbacks in JavaScript?
A) Callback functions are always executed in sequence
B) Callback functions can lead to “callback hell” or deeply nested code
C) Callback functions are less efficient than promises
D) Callback functions are only useful for asynchronous operations