1. What will be the output of the following code?
setTimeout(() => {
console.log('Hello');
}, 2000);
A) Logs 'Hello' immediately
B) Logs 'Hello' after 2 seconds
C) Logs 'Hello' after 1 second
D) Throws an error
Show Explanation
2. Which method is used to handle the resolved value of a Promise?
A) .catch()
B) .finally()
C) .then()
D) .async()
Show Explanation
3. What is returned by an async function in JavaScript?
A) Undefined
B) The value returned inside the function
C) Nothing
D) A Promise
Show Explanation
4. What will be the output of this code?
async function greet() {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('Hello');
}
greet();
A) Logs 'Hello' after 1 second
B) Logs 'Hello' immediately
C) Throws an error
D) Logs 'undefined'
Show Explanation
5. Which method is used to handle errors in a Promise?
A) .then()
B) .catch()
C) .finally()
D) .await()
Show Explanation
6. Why is async/await often preferred over using plain Promises?
A) It is faster
B) It supports more features
C) It makes asynchronous code easier to read
D) It removes the need for error handling
Show Explanation
7. How can errors in async/await code be handled?
A) Using try-catch
B) Using .finally()
C) Using a callback
D) Ignoring them
Show Explanation
8. Which Promise method executes after both resolve and reject cases?
A) .then()
B) .finally()
C) .catch()
D) .await()
Show Explanation
9. What is the purpose of 'resolve' in a Promise constructor function?
A) It ignores errors
C) It completes the Promise successfully
B) It handles errors
D) It throws an error
Show Explanation
10. Where can the 'await' keyword be used?
A) Inside async functions
B) Anywhere in the code
C) Inside regular functions
D) Inside Promises
Show Explanation
11. What is a callback function in JavaScript?
A) A function that returns another function
B) A function used only in Promises
C) A function passed as an argument to be called later
D) A function that runs only once
Show Explanation
12. When is the 'then' method of a Promise called?
A) After the Promise is resolved
B) Before the Promise is resolved
C) Only if there is an error
D) When the Promise is rejected
Show Explanation
13. What does the 'async' keyword do in front of a function?
A) It pauses the function
B) It only allows Promises inside the function
C) It runs the function on a separate thread
D) It allows 'await' to be used within the function
Show Explanation
14. What does 'Promise.all()' do?
A) Resolves when any Promise resolves
B) Resolves when all Promises are resolved or any is rejected
C) Resolves when the last Promise is resolved
D) Waits indefinitely for all Promises
Show Explanation
15. What does 'Promise.race()' do?
A) Resolves only if all Promises resolve
B) Rejects if all Promises reject
C) Returns the first resolved or rejected Promise
D) Ignores any errors
Show Explanation
16. When does the 'finally' method of a Promise execute?
A) After the Promise resolves or rejects
B) Only after the Promise resolves
C) Only after the Promise rejects
D) Only if there is an error
Show Explanation
17. What is a benefit of using 'async/await' in JavaScript?
A) It makes the code run faster
B) It makes asynchronous code look like synchronous code
C) It creates a separate thread
D) It eliminates the need for error handling
Show Explanation
18. What does 'Promise.reject()' do?
A) Ignores any errors
B) Creates a resolved Promise
C) Creates an immediately rejected Promise
D) Creates a Promise that waits indefinitely
Show Explanation
19. How can you handle errors in an 'async/await' function?
A) Use a try-catch block
B) Use only 'catch' method
C) Errors cannot be handled in async functions
D) Use a 'then' block
Show Explanation
20. What does the 'await' keyword do?
A) It ignores Promise rejections
B) It cancels the Promise
C) It returns the Promise immediately
D) It pauses execution until the Promise resolves
Show Explanation