JavaScript MCQs – Asynchronous JavaScript

21.) What happens if a promise is neither resolved nor rejected?

A) It is deleted automatically.
B) It remains in a pending state.
C) It throws an error.
D) It resolves by default.

Answer: Option B

Explanation: A promise that is neither resolved nor rejected remains in the pending state indefinitely.

22.) Which of these is NOT a promise-based API?

A) fetch()
B) setTimeout()
C) Promise.all()
D) Promise.any()

Answer: Option B

Explanation: setTimeout() uses callback functions instead of promises.

23.) Which of the following is an example of a callback function?

setTimeout(function() { 
       console.log("Done!");
 }, 1000);
A) setTimeout
B) console.log
C) function()
D) Both B and C

Answer: Option C

Explanation: A callback is a function passed as an argument to another function, which is executed later.

24.) What is the output of the following code?

console.log("A");  
setTimeout(() => console.log("B"), 0);  
console.log("C");
A) A B C
B) A C B
C) B A C
D) C B A

Answer: Option B

Explanation: setTimeout adds the callback to the task queue, so it executes after synchronous code (A and C).

25.) What will console.log output for the following?

const promise = new Promise((resolve, reject) => {  
  reject("Error!");  
});  
promise.then(console.log).catch(console.log);
A) undefined
B) Error!
C) Throws an error
D) Nothing

Answer: Option B

Explanation: The catch method handles the rejection and logs “Error!”.

26.) What will the output be for the following code?

async function test() {  
  return "Hello";  
}  
console.log(test());
A) “Hello”
B) undefined
C) Promise { <pending> }
D) Error

Answer: Option C

Explanation: An async function always returns a promise. The promise in this case is in the pending state.

Leave a Reply

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