JavaScript MCQs – Asynchronous JavaScript

11.) What is the purpose of Promise.race()?

A) Resolves with the fastest promise
B) Resolves with all promises’ results
C) Rejects all promises if one fails
D) Pauses until all promises are complete

Answer: Option A

Explanation: Promise.race() resolves or rejects with the result of the first completed promise.

12.) What does the fetch() method do?

A) Sends HTTP requests and returns a promise
B) Creates DOM elements
C) Executes functions asynchronously
D) Fetches variables from memory

Answer: Option A

Explanation: The fetch() method makes HTTP requests and returns a promise that resolves to a response object.

13.) How do you ensure a function runs only after a specific promise is resolved?

A) Use setTimeout.
B) Use .then() or await.
C) Use Promise.reject.
D) Use a synchronous loop.

Answer: Option B

Explanation: The .then() method or await ensures the function runs only after the promise resolves.

14.) How do you handle errors in an async function?

A) Using catch
B) Using try…catch
C) Using finally
D) None of the above

Answer: Option B

Explanation: Errors in an async function can be caught using a try…catch block.

15.) Which of these is NOT asynchronous in JavaScript?

A) setTimeout()
B) fetch()
C) Promise.all()
D) for loop

Answer: Option D

Explanation: A for loop is synchronous and blocks execution until it completes.

16.) What does Promise.resolve() do?

A) Resolves a pending promise with a given value
B) Rejects a promise
C) Creates a pending promise
D) Waits for a promise to resolve

Answer: Option A

Explanation: Promise.resolve() creates a promise that is already resolved with the specified value.

17.) How do you cancel an interval set with setInterval()?

A) stopInterval()
B) clearTimeout()
C) clearInterval()
D) cancelInterval()

Answer: Option C

Explanation: Use clearInterval() with the interval ID to stop a running interval.

18.) Which is NOT true about Promise.finally()?

A) It executes after the promise is settled.
B) It does not modify the resolved value.
C) It runs only on a rejected promise.
D) It can be used for cleanup.

Answer: Option C

Explanation: finally() runs regardless of whether the promise is resolved or rejected.

19.) What is the main drawback of callbacks?

A) Callback hell (nested callbacks)
B) Poor readability
C) Difficult error handling
D) All of the above

Answer: Option D

Explanation: Callbacks can lead to nested structures, hard-to-read code, and tricky error handling, known as callback hell.

20.) How do you convert a callback-based function into a promise?

A) Using async
B) Using Promise.race()
C) Using the Promise constructor
D) Using await

Answer: Option C

Explanation: The Promise constructor can wrap callback functions and convert them into promises.

Leave a Reply

Your email address will not be published. Required fields are marked *