Asynchronous Programming in JavaScript Fundamentals Quiz

Explore key concepts and features of asynchronous programming in JavaScript, covering callbacks, promises, async/await, and event handling for frontend development.

  1. Understanding setTimeout

    What does the setTimeout function do in JavaScript?

    1. Schedules a function to run after a specified delay
    2. Executes a function repeatedly at set intervals
    3. Pauses code execution for a given duration
    4. Interrupts the main execution thread

    Explanation: setTimeout schedules a function to run once after a given delay. It does not repeat like setInterval, which is a common confusion. JavaScript does not support pausing execution (option 3) or thread interruption (option 4) with setTimeout.

  2. Callback Functions

    Which statement accurately describes a callback function in JavaScript?

    1. A built-in function for asynchronous code only
    2. A function that only runs immediately after declaration
    3. A function passed as an argument to be executed later
    4. A function that stops all JavaScript execution

    Explanation: A callback function is passed into another function to be called at a future point. It does not stop execution (option 2), is not limited to built-in or async code (option 3), and does not run immediately after declaration (option 4).

  3. Promises and Their States

    What are the three possible states of a JavaScript Promise?

    1. Open, Closed, Canceled
    2. Initiated, Failed, Success
    3. Started, Running, Completed
    4. Pending, Fulfilled, Rejected

    Explanation: Promises can be in one of three states: pending (initial), fulfilled (resolved successfully), or rejected (failed). The other options use incorrect or unrelated terms for the standard Promise states.

  4. Async and Await Usage

    Why are async and await keywords useful in JavaScript?

    1. They speed up JavaScript execution
    2. They block the main thread until completion
    3. They are required for all AJAX requests
    4. They simplify asynchronous code by allowing easier-to-read syntax

    Explanation: Async and await make asynchronous code appear more like synchronous code, improving readability. They do not make code execute faster (option 2), do not block the main thread (option 3), and are not required for AJAX (option 4).

  5. Event Loop Fundamentals

    What is the primary role of the JavaScript event loop?

    1. Styling HTML elements dynamically
    2. Compiling JavaScript into machine code
    3. Storing large files in local storage
    4. Handling asynchronous callbacks and managing execution order

    Explanation: The event loop coordinates the execution of asynchronous callbacks and tasks. It does not perform compilation (option 2), styling (option 3), or data storage (option 4).

  6. Identifying Non-blocking Code

    Which statement best describes non-blocking code in JavaScript?

    1. Code that requires user permission
    2. Code that is always executed synchronously
    3. Code that does not prevent other operations from running
    4. Code that pauses until a task completes

    Explanation: Non-blocking code lets other processes continue while a task completes asynchronously. Blocking code (option 2) halts progress, synchronous code (option 3) may be blocking, and option 4 is unrelated.

  7. Working with Promise.all

    What does Promise.all return if any of the promises it receives rejects?

    1. It continues and returns the results of the resolved promises only
    2. It immediately rejects with the reason of the first rejected promise
    3. It returns an array of undefined values
    4. It delays resolution until all promises eventually resolve

    Explanation: Promise.all rejects as soon as any included promise rejects, and does not wait for others. It does not return partial results (option 2), wait for all to resolve (option 3 if any rejected), or return undefined (option 4).

  8. Asynchronous HTTP Requests

    Which method is commonly used for making asynchronous HTTP requests in modern JavaScript?

    1. console.log
    2. localStorage.setItem
    3. setInterval
    4. fetch

    Explanation: The fetch method is widely used for asynchronous HTTP requests. console.log writes to the console, localStorage.setItem stores data locally, and setInterval schedules repeated code execution.

  9. Error Handling in Promises

    Which method is used to handle errors in a JavaScript promise chain?

    1. catch
    2. next
    3. final
    4. resolve

    Explanation: The catch method is used for handling errors in promise chains. next is not a valid method for promises, resolve creates fulfilled promises, and final is not a standard method (the correct form is finally).