LearnFrenzy
Sign in Take a free test

Callbacks · Exercise 3 of 4

Callbacks Find the Error

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen JavaScript callback programs: SyntaxErrors in nested callbacks, mixed function syntax and reserved names; runtime errors from missing callbacks, passing call results, ignoring errors, async throws, string callbacks and out-of-scope results; and silent bugs from missing returns, synchronous reads of async results, repeated calls, finish-order results and inconsistent timing.

Question 1 Easy

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
setTimeout(() => {
  console.log("step 1");
  setTimeout(() => {
    console.log("step 2");
  }, 100;
}, 100);
A It prints step 1 and step 2
B It prints step 1 only
C TypeError
D SyntaxError: the inner setTimeout( call is not closed – write }, 100);
Question page & discussion
Question 2 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function readFile(name, cb) { cb(null, "content"); }
readFile("notes.txt", function (err, data) => {
  console.log(data);
});
A It prints content
B SyntaxError: use either function (err, data) { … } or (err, data) => { … }, not both
C It prints undefined
D TypeError: cb is not a function
Question page & discussion
Question 3 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function runTask(task, finally) {
  task();
  finally();
}
A It defines runTask normally
B SyntaxError: finally is a reserved word and cannot be a parameter name – call it onDone
C ReferenceError at call time
D TypeError at call time
Question page & discussion
Question 4 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const handle = (err, err) => {
  if (err) console.log("failed");
};
handle(null, "ok");
A It prints failed
B SyntaxError: arrow functions never allow duplicate parameter names
C Nothing is printed
D TypeError
Question page & discussion
Question 5 Easy

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function fetchScore(student, callback) {
  const score = student.length * 10;
  callback(score);
}
fetchScore("Asha");
A TypeError: callback is not a function – fetchScore was called without one
B It returns 40
C It prints 40
D Nothing happens
Question page & discussion
Question 6 Easy

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function printItem(item) {
  console.log("Item:", item);
}
["pen", "book"].forEach(printItem());
A It prints Item: pen and Item: book
B printItem() runs immediately (printing "Item: undefined") and returns undefined, so forEach receives undefined: TypeError – pass printItem
C SyntaxError
D It prints nothing
Question page & discussion
Question 7 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function getUser(id, cb) {
  if (id !== 1) return cb(new Error("User not found"));
  cb(null, { name: "Asha" });
}
getUser(2, (err, user) => {
  console.log("Hello,", user.name);
});
A It prints Hello, undefined
B It prints User not found
C It prints Hello, Asha
D TypeError: on failure user is undefined – check err first and return
Question page & discussion
Question 8 Hard

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
try {
  setTimeout(() => {
    throw new Error("Payment gateway timeout");
  }, 0);
} catch (e) {
  console.log("Handled:", e.message);
}
console.log("Waiting for payment…");
A It prints Handled: Payment gateway timeout
B It prints Waiting for payment… and Handled: …
C SyntaxError
D It prints Waiting for payment…, then the error is uncaught – the callback runs after the try block finished, so catch never sees it
Question page & discussion
Question 9 Hard

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
setTimeout("console.log('reminder')", 100);
A It prints reminder after 100 ms
B It prints the string immediately
C TypeError in Node.js: the callback argument must be a function – browsers would eval the string, which is unsafe; pass () => console.log("reminder")
D Nothing happens
Question page & discussion
Question 10 Easy

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function getUser(cb) {
  setTimeout(() => cb({ name: "Ravi" }), 0);
}
getUser((user) => {
  console.log("loaded");
});
console.log(user.name);
A It prints Ravi
B It prints undefined
C ReferenceError: user is a parameter of the callback and does not exist outside it
D It prints loaded then Ravi
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links