MCQs on Async/Await and Advanced Promises | JavaScript Advanced

In this chapter, we dive into the powerful features of JavaScript—Async/Await and advanced promises. We explore how to simplify asynchronous code, handle errors effectively, and manage parallel and sequential promises using Promise.all and Promise.race.

MCQs on Async/Await and Advanced Promises

Introduction to Async and Await

  1. What is the primary benefit of using async and await in JavaScript?
    • A) Makes asynchronous code run synchronously
    • B) Simplifies asynchronous code and makes it easier to read
    • C) Reduces memory consumption
    • D) Only works with synchronous functions
  2. What keyword is used to define an asynchronous function in JavaScript?
    • A) async
    • B) await
    • C) promise
    • D) asyncFunc
  3. What does await do inside an asynchronous function?
    • A) Pauses the execution of the entire program
    • B) Waits for the promise to resolve and returns the result
    • C) Immediately returns the promise object
    • D) Cancels the promise if it takes too long
  4. What type of value can an async function return?
    • A) Only a promise
    • B) Any value, wrapped in a promise
    • C) Only a string
    • D) Undefined
  5. Which of the following will throw an error when used with await?
    • A) A resolved promise
    • B) A pending promise
    • C) A non-promise value
    • D) An undefined value

Error Handling with try…catch in Async Functions

  1. How do you handle errors in an asynchronous function?
    • A) Using try…catch block
    • B) Using promises directly
    • C) By catching them with .catch()
    • D) By using a callback function
  2. Which of the following is correct when using try...catch in async functions?
    • A) catch block can only handle sync errors
    • B) try block catches both sync and async errors
    • C) try...catch only works in synchronous functions
    • D) You don’t need a catch block
  3. What happens when an error occurs inside an async function but there is no catch block?
    • A) The error is automatically logged to the console
    • B) The program terminates immediately
    • C) The promise is rejected with the error
    • D) The promise is ignored and resolved with null
  4. In which scenario should you use try...catch inside an async function?
    • A) To handle errors synchronously
    • B) To handle errors from awaited promises
    • C) To handle JavaScript syntax errors
    • D) To debug your code
  5. What is the behavior when an exception is thrown in an async function without a try...catch?
    • A) The promise will resolve to undefined
    • B) The promise will resolve to the exception message
    • C) The promise will be rejected with the exception
    • D) The exception is ignored and the code continues

Promise.all, Promise.race

  1. What is the purpose of Promise.all in JavaScript?
    • A) To execute multiple promises sequentially
    • B) To wait for all promises to resolve and return the results
    • C) To handle errors in a promise
    • D) To cancel a promise
  2. Which of the following is true when using Promise.all?
    • A) It returns a promise that resolves when any of the promises resolve
    • B) It rejects immediately if any of the promises reject
    • C) It waits for the first promise to reject
    • D) It only resolves when the last promise resolves
  3. What does Promise.race do?
    • A) Resolves as soon as one of the promises resolves or rejects
    • B) Resolves after all promises have resolved
    • C) Always rejects if any promise rejects
    • D) Resolves only if all promises resolve
  4. When using Promise.all, what happens if one of the promises rejects?
    • A) It rejects all other promises immediately
    • B) It resolves with the remaining promises
    • C) It continues to wait for other promises to resolve
    • D) It ignores the rejection
  5. In Promise.race, which promise’s result is returned?
    • A) The last promise to resolve
    • B) The first promise to resolve or reject
    • C) The promise with the lowest priority
    • D) All promises’ results are returned
  6. Which of the following is not supported by Promise.all?
    • A) Resolving multiple promises
    • B) Returning results as an array
    • C) Rejecting when one promise fails
    • D) Returning a promise with a single value
  7. How can Promise.race be useful in handling timeouts?
    • A) It waits for a specific promise to reject first
    • B) It resolves the first promise to finish, including timeouts
    • C) It waits for a timeout to occur before resolving any promise
    • D) It cancels the other promises once one is completed
  8. If you want to execute multiple promises in parallel but need to resolve when all are completed, which method should you use?
    • A) Promise.all
    • B) Promise.race
    • C) Promise.any
    • D) setTimeout
  9. What does Promise.all return if any of the promises reject?
    • A) The rejection reason
    • B) The resolved value from all promises
    • C) An array of the results
    • D) A resolved promise with null values
  10. How would you use Promise.all to run asynchronous functions in parallel?
    • A) By passing an array of promises to Promise.all
    • B) By passing a single promise object to Promise.all
    • C) By chaining promises together
    • D) By using await with each promise sequentially

Parallel and Sequential Execution of Promises

  1. Which of the following will execute promises sequentially in JavaScript?
    • A) Using Promise.all
    • B) Using Promise.race
    • C) Using await inside a loop
    • D) Using setTimeout
  2. How do you execute promises in parallel and wait for all of them to complete?
    • A) Use Promise.all
    • B) Use Promise.race
    • C) Use Promise.resolve()
    • D) Use forEach
  3. Which of the following will run promises sequentially, one after the other?
    • A) Promise.all
    • B) Promise.race
    • C) A for loop with await
    • D) Promise.any
  4. What is the advantage of running promises sequentially rather than in parallel?
    • A) Better for handling time-sensitive tasks
    • B) Allows handling each promise one by one in a specific order
    • C) Faster execution time
    • D) More efficient memory usage
  5. When using async/await inside a loop, how can you run promises sequentially?
    • A) By using Promise.all inside the loop
    • B) By using await inside the loop
    • C) By chaining promises together
    • D) By using setTimeout with promises
  6. What happens when you use await inside a loop for sequential execution?
    • A) All promises will execute in parallel
    • B) Each promise will wait for the previous one to resolve
    • C) The program will exit immediately
    • D) The promises will be ignored
  7. What is the advantage of running promises in parallel?
    • A) Reduces execution time by executing all promises simultaneously
    • B) Makes sure promises execute in a specific order
    • C) Reduces memory usage
    • D) Guarantees error-free execution
  8. Which of the following is a key difference between parallel and sequential execution of promises?
    • A) Parallel execution speeds up the process
    • B) Sequential execution waits for the completion of each promise
    • C) Parallel execution uses more memory
    • D) Sequential execution does not involve promises
  9. How can you handle errors when running promises in parallel using Promise.all?
    • A) By using .catch() on individual promises
    • B) By using try...catch inside each promise
    • C) By wrapping Promise.all with try...catch
    • D) By using setTimeout
  10. What will happen if a rejected promise is in a parallel execution using Promise.all?
    • A) It will be ignored and the other promises will continue
    • B) The entire Promise.all will reject immediately
    • C) The other promises will be delayed
    • D) It will result in an empty array of results

Answer Key

QnoAnswer (Option with Text)
1B) Simplifies asynchronous code and makes it easier to read
2A) async
3B) Waits for the promise to resolve and returns the result
4B) Any value, wrapped in a promise
5C) A non-promise value
6A) Using try…catch block
7B) try block catches both sync and async errors
8C) The promise is rejected with the error
9B) To handle errors from awaited promises
10C) The promise will be rejected with the exception
11B) To wait for all promises to resolve and return the results
12B) It rejects immediately if any of the promises reject
13A) Resolves as soon as one of the promises resolves or rejects
14A) It rejects all other promises immediately
15B) The first promise to resolve or reject
16D) Returning a promise with a single value
17B) It resolves the first promise to finish, including timeouts
18A) Promise.all
19A) The rejection reason
20A) By passing an array of promises to Promise.all
21C) Using await inside a loop
22A) Use Promise.all
23C) A for loop with await
24B) Allows handling each promise one by one in a specific order
25B) By using await inside the loop
26B) Each promise will wait for the previous one to resolve
27A) Reduces execution time by executing all promises simultaneously
28B) Sequential execution waits for the completion of each promise
29C) By wrapping Promise.all with try...catch
30B) The entire Promise.all will reject immediately

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