Asynchronous JavaScript: Callbacks, Promises, and the Event Loop Quiz

  1. Callback Functions in Action

    Which of the following best describes a callback function in JavaScript?

    1. A class used for managing events
    2. A built-in JavaScript function for HTTP requests
    3. A function that is passed as an argument to another function and executed later
    4. A variable that stores asynchronous data
    5. A function that calls itself repeatedly
  2. Understanding setTimeout

    What does the setTimeout function do in JavaScript?

    1. Repeats a function continuously every set interval
    2. Runs a function only when the page loads
    3. Executes a function once after a specified delay
    4. Pauses JavaScript execution completely
    5. Waits until a promise is resolved and then returns
  3. Promise Basics

    Which status is NOT a valid state of a JavaScript Promise?

    1. resolved
    2. settled
    3. fulfilled
    4. rejected
    5. pending
  4. Chaining Promises

    What does the .then() method do when called on a promise?

    1. Converts the promise into a synchronous function
    2. Blocks code execution until the promise completes
    3. Creates a new unresolved promise immediately
    4. Terminates the promise chain
    5. Attaches a callback to execute when the promise is fulfilled
  5. Error Handling with Promises

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

    1. catch()
    2. reject()
    3. error()
    4. abort()
    5. fail()
  6. async/await Syntax

    What does the async keyword do when added before a function declaration?

    1. Specifies error handling for the function
    2. Allows the use of await inside the function
    3. Forces the function to run immediately
    4. Automatically pauses function execution
    5. Converts the function to a callback
  7. Using await with Promises

    What does the await keyword do when used inside an async function?

    1. Runs code in the background without waiting
    2. Converts a regular function into a promise
    3. Automatically retries the awaited promise
    4. Pauses execution until the promise resolves
    5. Rejects the promise if a condition is met
  8. Event Loop Operation

    When does the JavaScript event loop move tasks from the task queue to the call stack?

    1. Whenever a variable is declared
    2. When the call stack is empty
    3. After each line of code runs
    4. Only when setTimeout is called
    5. Just before the page unloads
  9. Microtasks vs Macrotasks

    Which of the following is processed first by the event loop: microtasks or macrotasks?

    1. Neither, they require manual execution
    2. They run in alphabetical order
    3. Microtasks
    4. Macrotasks
    5. Both at the same time
  10. Promise.all Behavior

    What does Promise.all return if at least one of the promises is rejected?

    1. An array of undefined values
    2. A new callback function
    3. A rejected promise
    4. A resolved promise with empty results
    5. A warning message
  11. Avoiding Callback Hell

    Which feature of JavaScript helps avoid 'callback hell' by improving readability?

    1. Globals
    2. Synchronous functions
    3. Loops
    4. setStack
    5. Promises
  12. setInterval Purpose

    What is the role of setInterval in JavaScript?

    1. Closes the event loop
    2. Executes a function repeatedly at specified intervals
    3. Runs code once immediately
    4. Handles HTTP requests automatically
    5. Waits for a promise to resolve
  13. Event Loop Definition

    Which statement best describes the JavaScript event loop?

    1. It automatically handles errors in code
    2. It acts as a scheduler for synchronous code only
    3. It manages execution of multiple tasks by monitoring the call stack and task queues
    4. It ensures variables are always declared
    5. It provides native multithreading support
  14. Promise.resolve Usage

    What is the outcome of calling Promise.resolve(5)?

    1. An immediately rejected promise
    2. A rejected promise with value 5
    3. A delayed execution of the number 5
    4. A promise that resolves with the value 5
    5. A synchronous exception
  15. asynchronous vs synchronous

    In JavaScript, what is one key difference between asynchronous and synchronous code?

    1. Asynchronous code cannot handle errors
    2. Synchronous code is used only in event handlers
    3. Synchronous code uses callbacks, asynchronous does not
    4. Asynchronous code does not block further execution, while synchronous code does
    5. Asynchronous code only runs after page load