MCQs on Asynchronous JavaScript and Promises | JavaScript Intermediate

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

  1. 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
  2. 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
  3. Which method can you use to handle asynchronous operations in JavaScript?
    • A) forEach()
    • B) setTimeout()
    • C) alert()
    • D) console.log()
  4. 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
  5. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. How can you avoid “callback hell” in JavaScript?
    • A) Use nested callbacks
    • B) Use synchronous code
    • C) Use promises or async/await
    • D) Use setTimeout() to delay execution

Understanding Promises (resolve, reject, .then, .catch)

  1. A Promise in JavaScript represents:
    • A) A function that will run in the future
    • B) An operation that will complete eventually and return a value
    • C) An operation that can be performed synchronously only
    • D) A method to pause execution
  2. What does the resolve() function do in a promise?
    • A) It creates a new promise
    • B) It fulfills the promise and returns a value
    • C) It rejects the promise
    • D) It sets the promise to a pending state
  3. What happens when a promise is rejected?
    • A) The promise enters a resolved state
    • B) The promise is fulfilled with a value
    • C) The promise enters the rejected state and can be caught with .catch()
    • D) The promise is ignored
  4. Which method is used to handle a successful promise result?
    • A) .catch()
    • B) .then()
    • C) .finally()
    • D) .error()
  5. What does the .catch() method do in JavaScript promises?
    • A) It handles the rejection of the promise
    • B) It handles the resolution of the promise
    • C) It sets the promise state to fulfilled
    • D) It creates a new promise

Promise Chaining

  1. What is promise chaining?
    • A) Linking multiple promises together to handle sequential asynchronous operations
    • B) Creating promises inside an array
    • C) Using multiple .catch() methods
    • D) Nesting promises within other promises
  2. How do you chain promises in JavaScript?
    • A) By using multiple .then() methods
    • B) By using .catch() methods only
    • C) By calling the promise constructor again
    • D) By using async and await only
  3. What will happen if one of the promises in a chain is rejected?
    • A) The subsequent .then() methods will execute
    • B) The chain will stop and the rejection will be handled by .catch()
    • C) The rejected promise will automatically resolve
    • D) The rejection will be ignored
  4. Which of the following is a valid way to chain promises in JavaScript?
    • A) promise.then(result => { console.log(result); }).catch(error => { console.log(error); })
    • B) promise.then(console.log()).catch(console.error())
    • C) promise.chain(result => { console.log(result); })
    • D) promise.catch(console.log())
  5. What happens when you chain .catch() with .then() in a promise?
    • A) .catch() will handle both resolve and reject states
    • B) .catch() will handle only the reject state, and .then() will handle the resolve state
    • C) .catch() will prevent the promise from being resolved
    • D) .catch() will automatically resolve the promise

Bonus Questions

  1. What is a benefit of using promises over callbacks in JavaScript?
    • A) Promises make the code less readable
    • B) Promises allow handling multiple asynchronous operations simultaneously
    • C) Promises make it easier to handle errors in asynchronous code
    • D) Promises block the main thread during execution
  2. What is the state of a promise when it is created but not yet resolved or rejected?
    • A) Fulfilled
    • B) Pending
    • C) Rejected
    • D) Executing
  3. How do you create a promise in JavaScript?
    • A) new Promise(function(resolve, reject) {})
    • B) function Promise() {}
    • C) createPromise()
    • D) new AsyncPromise()
  4. What does Promise.all() do in JavaScript?
    • A) It executes multiple promises in sequence
    • B) It waits for all promises to resolve or reject
    • C) It rejects if one promise is rejected
    • D) It creates an array of promises
  5. What does Promise.race() do in JavaScript?
    • A) It waits for all promises to resolve
    • B) It resolves as soon as the first promise resolves or rejects
    • C) It runs promises in parallel
    • D) It only works with synchronous operations
  6. Which statement about promises is true?
    • A) A promise can be in multiple states at once
    • B) A promise can only be resolved or rejected once
    • C) Promises do not support error handling
    • D) Promises block the main thread
  7. Which of the following will cause a promise to be rejected?
    • A) Returning a non-promise value
    • B) Returning a resolved promise
    • C) Throwing an error inside the executor function
    • D) Resolving the promise
  8. What is the purpose of the .finally() method in promises?
    • A) It executes only if the promise is fulfilled
    • B) It executes after the promise has either been resolved or rejected
    • C) It cancels the promise
    • D) It handles the rejection of a promise
  9. What does the async keyword indicate in JavaScript?
    • A) The function is asynchronous and returns a promise
    • B) The function will run synchronously
    • C) The function has no return value
    • D) The function is executed immediately
  10. What does await do in asynchronous functions?
    • A) It pauses the function execution until the promise is resolved
    • B) It makes the function execute synchronously
    • C) It is used to create new promises
    • D) It executes the function immediately

Answers

QNoAnswer (Option with the text)
1C) Synchronous JavaScript executes sequentially
2B) It allows the program to perform other tasks while waiting for a response
3B) setTimeout()
4C) It blocks the execution of subsequent code until it completes
5A) It executes all synchronous tasks first, then moves to asynchronous tasks
6A) A function that executes after another function finishes
7A) setTimeout(() => console.log('Hello'), 1000)
8C) By passing the function name as an argument to another function
9B) Callback functions can lead to “callback hell” or deeply nested code
10C) Use promises or async/await
11B) An operation that will complete eventually and return a value
12B) It fulfills the promise and returns a value
13C) The promise enters the rejected state and can be caught with .catch()
14B) .then()
15A) It handles the rejection of the promise
16A) Linking multiple promises together to handle sequential asynchronous operations
17A) By using multiple .then() methods
18B) The chain will stop and the rejection will be handled by .catch()
19A) promise.then(result => { console.log(result); }).catch(error => { console.log(error); })
20B) .catch() will handle only the reject state, and .then() will handle the resolve state
21C) Promises make it easier to handle errors in asynchronous code
22B) Pending
23A) new Promise(function(resolve, reject) {})
24B) It waits for all promises to resolve or reject
25B) It resolves as soon as the first promise resolves or rejects
26B) A promise can only be resolved or rejected once
27C) Throwing an error inside the executor function
28B) It executes after the promise has either been resolved or rejected
29A) The function is asynchronous and returns a promise
30A) It pauses the function execution until the promise is resolved

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top