(Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024, Exams of Java Programming

(Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024(Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024(Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024(Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024

Typology: Exams

2022/2023

Available from 07/15/2024

ClemBSC
ClemBSC 🇺🇸

3.8

(32)

1.6K documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
FULL STACK JAVASCRIPT
Async & Await
KNOWLEDGE ASSESSMENT GUIDE
2024
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download (Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024 and more Exams Java Programming in PDF only on Docsity!

FULL STACK JAVASCRIPT

Async & Await

KNOWLEDGE ASSESSMENT GUIDE

  1. Which of the following statements about async functions is true?
    • A) They always return a Promise.
    • B) They block the main thread.
    • C) They cannot be used with await.
    • D) They must always be named.
    • Answer: A) They always return a Promise.
    • Rationale: Async functions always return a Promise, which resolves to the value returned by the function or rejects with an error.
  2. What does the await keyword do in an async function?
    • A) It pauses the execution of the async function and waits for the Promise to resolve.
    • B) It converts a synchronous function into an asynchronous one.
    • C) It rejects the Promise if it takes too long to resolve.
    • D) It runs the function in a separate thread.
    • Answer: A) It pauses the execution of the async function and waits for the Promise to resolve.
    • Rationale: The await keyword pauses the execution of the async function until the Promise is resolved or rejected.
  3. Which of the following is NOT a valid way to handle errors in async/await?
  1. Which of the following methods can be used to run multiple async functions concurrently?
    • A) Promise.all()
    • B) Promise.race()
    • C) Promise.allSettled()
    • D) All of the above
    • Answer: D) All of the above
    • Rationale: All these methods can be used to handle multiple Promises concurrently, each with different behaviors.

Fill-in-the-Blank Questions

  1. The await keyword can only be used inside an __________ function.
    • Answer: async
    • Rationale: The await keyword is designed to work only within async functions to pause execution until the Promise is resolved.
  2. If an async function does not explicitly return a value, it implicitly returns a __________.
    • Answer: Promise
    • Rationale: Async functions always return a Promise, even if no value is explicitly returned.
  1. The Promise.all() method returns a single Promise that resolves when __________ of the promises in the iterable have resolved.
    • Answer: all
    • Rationale: Promise.all() waits for all Promises in the iterable to resolve before resolving itself.
  2. To handle errors in an async function, you can use a __________ block.
    • Answer: try...catch
    • Rationale: try...catch blocks are used to handle errors in async functions.
  3. The Promise.race() method returns a Promise that resolves or rejects as soon as __________ of the promises in the iterable resolves or rejects.
    • Answer: one
    • Rationale: Promise.race() resolves or rejects as soon as one of the Promises in the iterable resolves or rejects.

True/False Questions

  1. Async functions can be used as constructors.
    • Answer: False
    • Rationale: Async functions cannot be used as constructors because they always return a Promise.
  1. What is the purpose of the async keyword in JavaScript?
    • A) To declare a function that returns a Promise.
    • B) To pause the execution of a function.
    • C) To handle synchronous code.
    • D) To create a new thread.
    • Answer: A) To declare a function that returns a Promise.
    • Rationale: The async keyword is used to declare a function that returns a Promise.
  2. Which of the following is true about Promise.allSettled()?
    • A) It resolves when all Promises are resolved.
    • B) It rejects if any Promise is rejected.
    • C) It resolves when all Promises are settled, regardless of their outcome.
    • D) It cancels all Promises if one is rejected.
    • Answer: C) It resolves when all Promises are settled, regardless of their outcome.
    • Rationale: Promise.allSettled() resolves after all Promises have settled, regardless of whether they were resolved or rejected.
  3. What will be the output of the following code?
    async function test() { throw new Error("Test error"); 

test().catch(e => console.log(e.message));

 - A) Test error - B) undefined - C) Error - D) Promise {<rejected>: Error: Test error} - Answer: A) Test error - Rationale: The async function throws an error, which is caught and logged by the `catch` method. 19. Which method can be used to handle the first resolved Promise among multiple Promises? - A) `Promise.all()` - B) `Promise.race()` - C) `Promise.allSettled()` - D) `Promise.any()` - Answer: B) `Promise.race()` - Rationale: `Promise.race()` resolves or rejects as soon as one of the Promises resolves or rejects. 20. What will be the output of the following code? ```javascript Rationale: The async keyword is used to declare that a function is asynchronous in JavaScript. Fill-in-the-Blank: Async functions always return a __. ## Correct Answer: Promise Rationale: Async functions return a Promise, which represents a value that may be available now, or in the future, or never. True/False: The await keyword can only be used inside an asynchronous function. ## Correct Answer: True Rationale: The await keyword can only be used inside functions declared with the async keyword to wait for a Promise to be resolved. Multiple Choice: Which of the following is not a valid use case for async/await in JavaScript? A. Making AJAX requests B. Performing CPU-intensive tasks C. Handling timeouts D. Animating elements on a webpage ## Correct Answer: D. Animating elements on a webpage Rationale: Async/await is not suitable for handling animations as it is primarily used for managing asynchronous operations. Fill-in-the-Blank: The await keyword is used to wait for a __ to be resolved. ## Correct Answer: Promise Rationale: await pauses the execution of an async function until the Promise is settled. True/False: Async functions always execute synchronously. ## Correct Answer: False Rationale: Async functions allow asynchronous operations to be performed in a synchronous manner, making code easier to read and maintain. Multiple Choice: Which of the following keywords can be used with async/await to handle errors? Multiple Choice: What does the Promise.all method do in JavaScript? A. Executes a group of Promises in parallel B. Executes a group of Promises sequentially C. Executes only the first resolved Promise D. None of the above ## Correct Answer: A. Executes a group of Promises in parallel Rationale: Promise.all takes an iterable of Promises and returns a single Promise that resolves when all the input Promises have resolved. Fill-in-the-Blank: The async keyword can be used with __ to create a Promise-based API. ## Correct Answer: Functions Rationale: By using the async keyword with functions, developers can create asynchronous functions that return Promises. True/False: Using async/await can help avoid callback hell in JavaScript. ## Correct Answer: True Rationale: async/await simplifies asynchronous code and makes it more readable compared to nested callbacks. Multiple Choice: Which of the following is not a state of a Promise in JavaScript? A. Pending B. Resolved C. Rejected D. Completed ## Correct Answer: D. Completed Rationale: Promises in JavaScript have states of Pending, Resolved (Fulfilled), and Rejected. Fill-in-the-Blank: The Promise.race method returns a Promise that resolves or rejects as soon as one of the input Promises __. ## Correct Answer: Settles Rationale: Promise.race returns a Promise that settles (resolves or rejects) as soon as one of the input Promises settles. True/False: Rationale: Async functions can return values using the return keyword, which resolves the Promise with the specified value. True/False: The async keyword can be used with arrow functions in JavaScript. ## Correct Answer: True Rationale: Arrow functions can be declared as async functions by prefixing them with the async keyword. Multiple Choice: Which of the following is a benefit of using async/await over Promises in JavaScript? A. Better performance B. Simpler error handling C. Easier debugging D. All of the above ## Correct Answer: D. All of the above Rationale: Async/await offers improved error handling, readability, and debugging capabilities compared to using Promises directly. Fill-in-the-Blank: The await keyword can only be used within an __ function. ## Correct Answer: Asynchronous Rationale: await can only be used inside asynchronous functions declared with the async keyword to wait for Promises to be resolved. 1. Multiple Choice: Which keyword is used to define a function that can pause execution and resume at a later time in JavaScript? A) async B) await C) yield D) return ## Correct Answer: C) yield Rationale: The yield keyword in JavaScript allows for the creation of generators, which can be paused and resumed at a later time. 2. Fill in the Blank: Async functions always return a/an _____________. ## Correct Answer: Promise Rationale: Async functions always return a Promise, which resolves to the value returned by the async function or to the value it is rejected with. Rationale: The await keyword is used inside an async function to pause execution until the Promise is settled (resolved or rejected). 6. True/False: Using async/await in JavaScript guarantees faster execution of asynchronous tasks compared to using Promises. ## Correct Answer: False Rationale: While async/await can make asynchronous code easier to read and write, it does not guarantee faster execution compared to using Promises. 7. Multiple Choice: What is the purpose of the Promise.all method in JavaScript async operations? A) To execute promises in parallel B) To execute promises sequentially C) To handle errors in asynchronous code D) To convert synchronous code into asynchronous code ## Correct Answer: A) To execute promises in parallel Rationale: Promise.all is used to execute multiple promises in parallel and wait for all of them to settle before continuing execution. 8. Fill in the Blank: The async keyword is used to define a function that returns a/an __________. ## Correct Answer: Promise Rationale: Functions defined with the async keyword return a Promise, which resolves to the value returned by the async function. 9. True/False: The async keyword is used to handle asynchronous operations in JavaScript without blocking the main thread. ## Correct Answer: True Rationale: The async keyword allows JavaScript functions to perform asynchronous operations without blocking the main thread, enhancing performance. 10. Multiple Choice: Which statement correctly describes the behavior of async functions in JavaScript? A) They always run synchronously B) They cannot contain Promises C) They can be paused using the yield keyword D) They return Promises ## Correct Answer: D) They return Promises Rationale: Async functions always return Promises, which can be resolved with the value returned by the function or rejected with an error.