JavaScript MCQs – Asynchronous JavaScript

Asynchronous JavaScript is key to handling time-consuming tasks like API calls, timers, and user interactions without blocking the main thread. Topics like promises, async/await, callbacks, and methods such as fetch() are crucial for modern web development. Understanding these concepts helps write efficient, non-blocking code that is often tested in interviews. Practicing MCQs on these topics is an excellent way to prepare and boost interview confidence.

1.) What is asynchronous JavaScript?

A) Code executed line by line in order
B) Code executed in parallel without blocking
C) Code executed only after page load
D) Code executed without using variables

Answer: Option B

Explanation: Asynchronous JavaScript allows tasks to run without blocking the main thread, enabling parallel execution.

2.) Which method is used to handle promises in JavaScript?

A) catch
B) then
C) finally
D) All of the above

Answer: Option D

Explanation: Promises use then to handle resolved values, catch for errors, and finally for cleanup after execution.

3.) What does setTimeout() do in JavaScript?

A) Executes a function immediately
B) Executes a function after a delay
C) Stops code execution for a set time
D) Executes code repeatedly

Answer: Option B

Explanation: setTimeout() runs a function after the specified delay in milliseconds.

4.) What is the purpose of async in JavaScript?

A) Makes a function asynchronous
B) Converts a promise into a callback
C) Pauses function execution
D) Returns multiple promises

Answer: Option A

Explanation: The async keyword allows functions to return a promise, making asynchronous code easier to write and read.

5.) What does await do in an async function?

A) Creates a promise
B) Resolves a promise
C) Pauses the execution until the promise resolves
D) Rejects a promise

Answer: Option C

Explanation: The await keyword pauses execution until the associated promise is resolved or rejected.

6.) Which function repeatedly executes code after a set interval?

A) setTimeout()
B) setInterval()
C) setRepeater()
D) executeLater()

Answer: Option B

Explanation: setInterval() runs a function repeatedly at specified intervals.

7.) What happens if await is used outside an async function?

A) It works normally
B) It throws an error
C) It resolves the promise automatically
D) It skips the await keyword

Answer: Option B

Explanation: await must be used inside an async function; otherwise, it throws a syntax error.

8.) What is the default state of a JavaScript promise?

A) Resolved
B) Rejected
C) Pending
D) Fulfilled

Answer: Option C

Explanation: Promises start in the pending state before they are resolved or rejected.

9.) How do you create a new promise?

A) new Promise()
B) Promise.create()
C) Promise.start()
D) new Async()

Answer: Option A

Explanation: A new promise is created using the Promise constructor, which accepts a function with resolve and reject as parameters.

10.) What is the purpose of Promise.all()?

A) Resolves multiple promises sequentially
B) Resolves multiple promises concurrently
C) Rejects all promises if one fails
D) Creates a single promise

Answer: Option B

Explanation: Promise.all() resolves all promises concurrently and returns a single promise with an array of results.

Leave a Reply

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