LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Exercise 3 of 4

Scope and Hoisting Find the Error

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

Debug fifteen JavaScript programs about scope and hoisting: SyntaxErrors from conflicting declarations, ReferenceErrors from the TDZ, classes and out-of-scope helpers, and silent bugs from lexical scope, accidental globals, late closures, duplicate functions and shadowed parameters.

Question 1 Medium

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

main.js
function applyTax(amount) {
  let amount = amount * 1.18;
  return amount;
}
console.log(applyTax(100));
A It prints 118
B ReferenceError: amount is in the TDZ
C It prints NaN
D SyntaxError: amount is already declared as a parameter, so let cannot declare it again in the function body
Question page & discussion
Question 2 Medium

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

main.js
class Product {}
console.log("Catalogue loaded");
class Product {
  constructor(name) { this.name = name; }
}
A It prints Catalogue loaded
B The second class replaces the first
C TypeError: Product is not a constructor
D SyntaxError: Product has already been declared – class names cannot be redeclared in the same scope
Question page & discussion
Question 3 Hard

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

main.js
const format = "json";
function format(data) {
  return JSON.stringify(data);
}
console.log(format);
A It prints json
B It prints the function
C SyntaxError: format is declared twice in one scope – a function declaration cannot share a name with a const
D TypeError: Assignment to constant variable
Question page & discussion
Question 4 Hard

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

main.js
try {
  JSON.parse("{bad json}");
} catch (err) {
  let err = "Invalid input";
  console.log(err);
}
A It prints Invalid input
B It prints the SyntaxError message
C ReferenceError
D SyntaxError: err is already declared as the catch parameter in this block
Question page & discussion
Question 5 Easy

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

main.js
function login() {
  const token = "abc123";
}
function fetchProfile() {
  return `Bearer ${token}`;
}
login();
console.log(fetchProfile());
A ReferenceError: token is local to login() and not in fetchProfile's scope chain – return it and pass it in
B It prints Bearer abc123
C It prints Bearer undefined
D TypeError
Question page & discussion
Question 6 Medium

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

main.js
const order = new Order(42);
console.log(order.id);

class Order {
  constructor(id) { this.id = id; }
}
A It prints 42
B TypeError: Order is not a constructor
C It prints undefined
D ReferenceError: Order cannot be accessed before initialization – classes are not usable before their declaration
Question page & discussion
Question 7 Hard

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

main.js
function getConfig() {
  return config.mode;
}
console.log(getConfig());
let config = { mode: "production" };
A ReferenceError: getConfig runs before the let line, so config is still in its TDZ
B It prints production
C TypeError: config is undefined
D It prints undefined
Question page & discussion
Question 8 Easy

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

main.js
const items = ["a", "b", "c"];
for (let i = 0; i < items.length; i++) {
  if (items[i] === "b") break;
}
console.log("Found at index", i);
A It prints Found at index 1
B It prints Found at index 3
C It prints Found at index undefined
D ReferenceError: i exists only inside the loop – declare let index before the loop and assign it
Question page & discussion
Question 9 Medium

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

main.js
const total = 100;
{
  console.log("Total:", total);
  const total = 200;
}
A It prints Total: 100
B It prints Total: 200
C ReferenceError: the block's own total shadows the outer one from the start of the block and is in its TDZ
D It prints Total: undefined
Question page & discussion
Question 10 Easy

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

main.js
function report() {
  function formatDate(d) {
    return d.toISOString().slice(0, 10);
  }
  return "Report ready";
}
console.log(report());
console.log(formatDate(new Date(0)));
A It prints Report ready and 1970-01-01
B It prints Report ready, then ReferenceError: formatDate is defined inside report() and is not visible outside
C SyntaxError before anything runs
D TypeError: formatDate is not a function
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links