JavaScript Asynchronous JS (Callbacks) Quiz

JavaScript
0 Passed
0% acceptance

Answer 75 questions on asynchronous JavaScript callbacks: fundamentals, error-first patterns, execution order, callback hell, timers, DOM and I/O callbacks, higher-order functions, data passing, chaining, and modern replacements like Promises.

75 Questions
~150 minutes
1

Question 1

What distinguishes asynchronous JavaScript from synchronous execution?

A
Async code defers work so the call stack can continue without waiting for long operations
B
Async code blocks the thread until completion
C
Async code only runs in browsers
D
Async code uses multiple threads automatically
2

Question 2

What is a callback in JavaScript?

A
A function passed as an argument to be invoked later by another function
B
A return value
C
A global variable
D
A synchronous loop
3

Question 3

Which statement best describes asynchronous vs synchronous flow?

A
Synchronous code runs line-by-line; asynchronous code can schedule completion for later
B
Asynchronous code executes faster
C
Synchronous code cannot call functions
D
Asynchronous code uses blocking I/O
4

Question 4

Why were callbacks historically important before Promises and async/await?

A
They were the primary mechanism for handling asynchronous operations
B
They prevented all bugs
C
They replaced return statements
D
They required multi-threading
5

Question 5

What does this code print?

javascript
function greetLater(cb) {
  console.log('Preparing greeting')
  cb('Hello')
}
greetLater(message => console.log(message))
A
Preparing greeting Hello
B
Hello Preparing greeting
C
Hello only
D
Nothing
6

Question 6

What are higher-order functions in the context of callbacks?

A
Functions that accept other functions as arguments or return them
B
Functions that only run once
C
Functions that cannot be asynchronous
D
Functions that run in a different thread
7

Question 7

Why is naming callbacks helpful?

A
Named functions make stack traces and debugging easier
B
Anonymous functions run faster
C
Callbacks must be global
D
Naming is required by the runtime
8

Question 8

What happens here?

javascript
setTimeout(function cb() {
  console.log('Later')
}, 0)
console.log('Now')
A
Now Later
B
Later Now
C
Later only
D
Now only
9

Question 9

What is the callback queue?

A
A queue of functions waiting for the event loop to push them onto the call stack after asynchronous work completes
B
The call stack itself
C
A list of synchronous operations
D
A place for global variables
10

Question 10

Which order is logged?

javascript
console.log('A')
setTimeout(() => console.log('B'), 10)
console.log('C')
A
A C B
B
A B C
C
B A C
D
C B A
11

Question 11

What is the error-first callback pattern?

A
Callbacks receive an error as the first argument, followed by data
B
Errors are thrown instead of passed
C
Callbacks never receive data
D
Errors go last
12

Question 12

What does this error-first callback do?

javascript
function readFile(cb) {
  cb(new Error('Fail'), null)
}
readFile((err, data) => {
  if (err) console.error(err.message)
  else console.log(data)
})
A
Logs Fail because err is provided
B
Logs data
C
Throws automatically
D
Does nothing
13

Question 13

Why can deeply nested callbacks become a problem?

A
They reduce readability, make error handling hard, and create callback hell
B
They improve debugging
C
They run faster
D
They use less memory
14

Question 14

Which technique helps avoid callback hell?

A
Break callbacks into named functions or use control flow helpers
B
Always nest callbacks
C
Place all logic in a single callback
D
Avoid error handling
15

Question 15

What is logged?

javascript
function asyncAdd(a, b, cb) {
  setTimeout(() => cb(null, a + b), 5)
}
asyncAdd(2, 3, (err, sum) => {
  if (err) return
  console.log(sum)
})
A
5
B
undefined
C
err
D
Nothing
16

Question 16

Why does asynchronous code rely on the event loop?

A
The event loop coordinates when queued callbacks run after asynchronous APIs complete
B
It executes synchronous loops faster
C
It provides multi-threading
D
It disables garbage collection
17

Question 17

What is callback hell?

A
A phenomenon where nested callbacks become hard to read and maintain
B
A performance optimization
C
An error type
D
A browser feature
18

Question 18

What does this nesting illustrate?

javascript
doA(resultA => {
  doB(resultA, resultB => {
    doC(resultB, resultC => {
      console.log(resultC)
    })
  })
})
A
Callback hell due to nested anonymous callbacks
B
Synchronous recursion
C
Promise chaining
D
Error handling best practice
19

Question 19

Which practice improves callback readability?

A
Extract callback logic into separate named functions
B
Place all logic inside a single anonymous callback
C
Ignore error arguments
D
Avoid documentation
20

Question 20

How do setTimeout and setInterval relate to callbacks?

A
They schedule callbacks for future execution after a delay or repeatedly
B
They execute callbacks immediately
C
They block until completion
D
They can only run synchronous code
21

Question 21

What prints here?

javascript
setInterval(() => console.log('tick'), 50)
setTimeout(() => console.log('stop'), 160)
A
tick tick tick stop
B
stop then tick
C
tick only once
D
Nothing
22

Question 22

Why should intervals usually be cleared?

A
Uncleared intervals keep firing indefinitely, wasting resources
B
Intervals clear themselves
C
Clearing intervals slows code
D
Intervals never run twice
23

Question 23

How can you pass data through callbacks?

A
Invoke the callback with arguments that contain the needed data
B
Callbacks cannot accept parameters
C
Use global variables only
D
Throw errors with data
24

Question 24

What is logged?

javascript
function fetchUser(id, cb) {
  setTimeout(() => cb(null, { id, name: 'Ada' }), 20)
}
fetchUser(7, (err, user) => {
  console.log(user.id, user.name)
})
A
7 Ada
B
undefined undefined
C
An error
D
Nothing
25

Question 25

Why use error-first callbacks instead of throwing exceptions?

A
Thrown errors cannot propagate across asynchronous boundaries, so errors must be passed through callbacks
B
throw is deprecated
C
Callbacks run synchronously
D
Errors are impossible in async code
26

Question 26

What happens here?

javascript
function getData(cb) {
  setTimeout(() => cb(null, 'value'), 5)
}
getData((err, value) => {
  if (err) return
  setTimeout(() => console.log(value), 5)
})
A
value (after about 10ms total)
B
value immediately
C
An error
D
Nothing
27

Question 27

What is the callback execution order for multiple timers with the same delay?

A
The event loop processes them in the order they were scheduled, assuming identical delays
B
Random order
C
Reverse order
D
Timers run simultaneously on separate threads
28

Question 28

Why should callbacks be pure when possible?

A
Pure callbacks avoid unexpected side effects, making async flow easier to reason about
B
Callbacks cannot mutate state
C
Pure functions run faster
D
Purity is required by the spec
29

Question 29

What does the console show?

javascript
console.log('start')
setTimeout(() => console.log('timer'), 0)
Promise.resolve().then(() => console.log('microtask'))
console.log('end')
A
start end microtask timer
B
start microtask end timer
C
microtask start end timer
D
start end timer microtask
30

Question 30

What best practice applies to callback error handling?

A
Handle or propagate errors immediately to avoid silent failures
B
Ignore error arguments
C
Assume errors never occur
D
Log errors only in development
31

Question 31

Which order of logs occurs?

javascript
const fs = require('fs')
console.log('read start')
fs.readFile('file.txt', (err, data) => {
  if (err) return console.error('error')
  console.log('read complete')
})
console.log('after read call')
A
read start after read call read complete
B
read start read complete after read call
C
read complete read start after read call
D
after read call read start read complete
32

Question 32

What role does libuv play in Node.js callbacks?

A
It handles asynchronous I/O operations and notifies JavaScript when callbacks should run
B
It executes JavaScript code directly
C
It only handles DOM events
D
It disables the event loop
33

Question 33

How do DOM event callbacks work?

A
The browser registers listeners and invokes callbacks when matching events occur
B
DOM callbacks run synchronously before events happen
C
They require polling
D
They only work on server
34

Question 34

What does this DOM callback log after a click?

javascript
button.addEventListener('click', event => {
  console.log('Clicked', event.type)
})
A
Clicked click
B
click Clicked
C
Nothing
D
Throws error
35

Question 35

What is a higher-order function in callback composition?

A
A function that accepts callbacks and orchestrates their execution order
B
A function that returns numbers only
C
A function that cannot be asynchronous
D
A function that ignores arguments
36

Question 36

Why is callback execution order important?

A
Incorrect ordering can lead to race conditions or using data before it is ready
B
Order never matters
C
Callbacks run simultaneously
D
The runtime enforces alphabetical order
37

Question 37

What prints?

javascript
function first(cb) {
  setTimeout(() => cb('first'), 30)
}
function second(cb) {
  setTimeout(() => cb('second'), 10)
}
first(value => console.log(value))
second(value => console.log(value))
A
second first
B
first second
C
first only
D
second only
38

Question 38

How do browsers ensure UI responsiveness with callbacks?

A
By offloading long operations to background threads and invoking callbacks when ready
B
By blocking rendering until callbacks finish
C
By running everything on the UI thread simultaneously
D
By disallowing async operations
39

Question 39

Why must callback APIs document their invocation guarantees?

A
Consumers need to know how many times a callback runs, when it runs, and with what arguments
B
Documentation is optional
C
Callbacks run only once by default
D
All callbacks auto-document themselves
40

Question 40

What is logged?

javascript
const queue = []
function schedule(task) {
  queue.push(task)
}
schedule(() => console.log('task1'))
schedule(() => console.log('task2'))
while (queue.length) {
  const next = queue.shift()
  next()
}
A
task1 task2
B
task2 task1
C
Nothing
D
task1 only
41

Question 41

What are I/O callbacks in JavaScript?

A
Callbacks invoked when file system, network, or other I/O operations complete
B
Callbacks used only for DOM events
C
Synchronous loops
D
Callbacks that run on page load only
42

Question 42

Why do Node.js callbacks usually receive (err, data)?

A
It standardizes success/failure handling across async APIs
B
It is required by ECMAScript
C
It only applies to synchronous functions
D
It eliminates try/catch
43

Question 43

Which order occurs?

javascript
function logLater(label) {
  setTimeout(() => console.log(label), 0)
}
logLater('X')
logLater('Y')
A
X Y
B
Y X
C
Y only
D
X only
44

Question 44

What is the callback queue also known as?

A
Task queue or macrotask queue
B
Call stack
C
Heap
D
Microtask queue
45

Question 45

What is printed?

javascript
const results = []
function asyncPush(value, cb) {
  setTimeout(() => {
    results.push(value)
    cb()
  }, 5)
}
asyncPush('A', () => console.log(results.join(',')))
A
A
B
An empty string
C
undefined
D
Throws error
46

Question 46

Why do callbacks often receive context objects?

A
Passing context avoids reliance on external mutable state
B
Callbacks cannot access closures
C
Context objects slow performance
D
They are mandatory per spec
47

Question 47

What is the event loop responsible for?

A
Moving ready callbacks from queues onto the call stack when it is empty
B
Running CSS animations
C
Allocating memory for objects
D
Compiling JavaScript
48

Question 48

What prints from this microtask example?

javascript
console.log('start')
Promise.resolve().then(() => console.log('microtask'))
setTimeout(() => console.log('timer'), 0)
console.log('end')
A
start end microtask timer
B
start microtask end timer
C
start end timer microtask
D
microtask start end timer
49

Question 49

How can callbacks be debounced?

A
Wrap the callback in logic that delays execution until events stop firing for a duration
B
Run callbacks simultaneously
C
Call callbacks twice
D
Throw errors when events repeat
50

Question 50

Why should callback-based APIs document if callbacks may be called multiple times?

A
Developers need to know whether to expect repeated invocations and design idempotent handlers
B
Callbacks always run once
C
Repeated callbacks are illegal
D
Documentation is unrelated
51

Question 51

How can data be carried between chained callbacks?

A
Pass the data as arguments to each subsequent callback
B
Use only global variables
C
Data cannot flow through callbacks
D
Store data on window only
52

Question 52

What prints?

javascript
function first(cb) {
  cb(null, 'A')
}
function second(value, cb) {
  cb(null, value + 'B')
}
first((err, value) => {
  second(value, (err2, combined) => {
    console.log(combined)
  })
})
A
AB
B
BA
C
A only
D
Nothing
53

Question 53

What is an error aggregator callback?

A
A callback that collects multiple error results before taking action
B
A callback that ignores errors
C
A callback that only handles success
D
A callback that throws automatically
54

Question 54

Why do higher-order utilities like async.series exist?

A
They manage multiple callbacks sequentially or in parallel without deep nesting
B
They replace callbacks entirely
C
They block the event loop
D
They require Promises
55

Question 55

What prints from this waterfall?

javascript
function step1(cb) {
  setTimeout(() => cb(null, 'data1'), 5)
}
function step2(input, cb) {
  setTimeout(() => cb(null, input + '-data2'), 5)
}
step1((err, result1) => {
  if (err) return
  step2(result1, (err2, result2) => {
    if (err2) return
    console.log(result2)
  })
})
A
data1-data2
B
data2-data1
C
data1 only
D
Nothing
56

Question 56

What is callback composition?

A
Building larger asynchronous workflows by combining small callbacks
B
Running callbacks synchronously
C
Replacing callbacks with loops
D
Throwing errors from callbacks
57

Question 57

Why is consistent error propagation important in callback chains?

A
Missing error propagation leaves later callbacks unaware of failures
B
Errors resolve themselves
C
Chains never fail
D
Error arguments slow code
58

Question 58

What prints from this parallel pattern?

javascript
function work(label, delay, cb) {
  setTimeout(() => cb(null, label), delay)
}
const results = []
work('A', 30, (err, label) => {
  results.push(label)
  if (results.length === 2) console.log(results.join(''))
})
work('B', 10, (err, label) => {
  results.push(label)
  if (results.length === 2) console.log(results.join(''))
})
A
BA
B
AB
C
AA
D
BB
59

Question 59

What is an inversion of control concern with callbacks?

A
Once you pass a callback to another function, you trust it to call your callback correctly
B
Callbacks always run synchronously
C
You retain full control over execution timing
D
Inversion of control is unrelated
60

Question 60

Why is it risky when callbacks run both sync and async depending on conditions?

A
Callers might assume asynchronous ordering and miss synchronous edge cases, leading to bugs
B
It improves reliability
C
Callbacks cannot run synchronously
D
It is required by spec
61

Question 61

What prints from this inconsistent callback?

javascript
function maybeAsync(flag, cb) {
  if (flag) {
    setTimeout(() => cb('async'), 0)
  } else {
    cb('sync')
  }
}
maybeAsync(false, value => console.log('first', value))
maybeAsync(true, value => console.log('second', value))
A
first sync second async
B
second async first sync
C
second async only
D
first sync only
62

Question 62

How can you ensure callbacks always run asynchronously?

A
Wrap the callback invocation in setTimeout or queueMicrotask
B
Rely on synchronous execution
C
Throw errors instead
D
Avoid calling the callback
63

Question 63

What is callback chaining?

A
Executing multiple asynchronous steps in sequence by invoking the next callback inside the previous one
B
Running callbacks simultaneously
C
Using loops only
D
Replacing callbacks with events
64

Question 64

How does queueMicrotask relate to callbacks?

A
It schedules a callback as a microtask, running before the next macrotask
B
It blocks the event loop
C
It replaces setTimeout entirely
D
It only works in Node.js
65

Question 65

Why were Promises introduced as a callback alternative?

A
Promises standardize asynchronous composition, error propagation, and chaining without deep nesting
B
Promises are synchronous
C
Promises eliminate the event loop
D
Promises run faster than callbacks by default
66

Question 66

What is the callback-to-promise conversion process often called?

A
Promisification
B
Serialization
C
Memoization
D
Currying
67

Question 67

What does util.promisify accomplish?

javascript
import { promisify } from 'node:util'
import { readFile } from 'node:fs'
const readFileAsync = promisify(readFile)
readFileAsync('note.txt', 'utf8').then(console.log)
A
Converts the callback-based readFile into a Promise-returning version
B
Runs readFile synchronously
C
Removes error handling
D
Blocks the event loop
68

Question 68

Why might you still use callbacks instead of Promises?

A
Some legacy APIs expose only callbacks, or callbacks fit simple event-driven scenarios
B
Promises are unavailable in modern engines
C
Callbacks never cause issues
D
Callbacks run in parallel automatically
69

Question 69

How does async/await relate to callbacks?

A
Async/await is syntax sugar over Promises, which themselves often wrap callbacks
B
Async/await eliminates callbacks internally
C
Async functions block the thread
D
Async/await only works with setTimeout
70

Question 70

What is a continuation-passing style (CPS) function?

A
A function that accepts a continuation (callback) to execute after the current work
B
A function that returns continuations
C
A synchronous function only
D
A function that cancels callbacks
71

Question 71

What prints from this promisified pattern?

javascript
function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}
delay(5).then(() => console.log('done'))
A
done
B
nothing
C
ms
D
resolve
72

Question 72

Why is it easier to compose Promises than raw callbacks?

A
Promises chain sequentially with .then and propagate errors automatically
B
Promises avoid the event loop
C
Promises run on multiple threads
D
Promises never fail
73

Question 73

How can callbacks be converted to async/await?

A
Wrap the callback API in a Promise (promisify) and await it inside an async function
B
Call await on callbacks directly
C
Use setTimeout
D
Async/await cannot interact with callbacks
74

Question 74

What is a downside of callbacks compared to Promises?

A
Manual error handling and nesting make callbacks harder to compose
B
Callbacks cannot represent asynchronous work
C
Callbacks block the event loop
D
Callbacks are incompatible with Node.js
75

Question 75

When transitioning from callbacks to Promises, what should you verify?

A
That the callback API follows error-first conventions so promisification can map errors and results correctly
B
That callbacks never receive data
C
That errors are ignored
D
That Promises run synchronously

QUIZZES IN JavaScript