LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Exercise 3 of 4

Scope and Hoisting Find the Error

  • 15 questions
  • Page 2 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 11 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const currency = "USD";
function format(amount) {
  return `${amount} ${currency}`;
}
function indianPrice(amount) {
  const currency = "INR";
  return format(amount);
}
console.log(indianPrice(500));
A Template literals cannot use outer variables
B Scope is lexical: format() reads the top-level currency, not the caller's local one – pass the currency as a parameter
C const currency inside indianPrice is a SyntaxError
D format() should be an arrow function
Question page & discussion
Question 12 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
let count = 100;                       // number of visitors today
function countWords(text) {
  count = 0;                           // meant to be a local counter
  for (const w of text.split(" ")) count++;
  return count;
}
console.log(countWords("learn javascript scope"));
console.log("Visitors:", count);
A count inside countWords is not declared, so it finds the outer count and overwrites it – declare let count = 0 inside the function
B split(" ") returns the wrong number of words
C for…of cannot change variables
D let count must be declared after the function
Question page & discussion
Question 13 Hard

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
let discount = 10;
const priceAfter = (p) => p - (p * discount) / 100;
const savedRule = priceAfter;            // meant to remember the 10% rule
discount = 50;                           // a flash sale changes the global discount
console.log(savedRule(1000));
A Arrow functions cannot read outer variables
B savedRule is a copy of the number 900
C The function reads discount when it runs, not when it was saved – create it with a factory that captures the value: makeRule(10)
D let discount must be const
Question page & discussion
Question 14 Hard

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function tax(amount) {
  return amount * 0.18;                // GST on products
}
console.log("Product tax:", tax(100));

// … hundreds of lines later, another developer adds:
function tax(amount) {
  return amount * 0.05;                // tax on services
}
console.log("Service tax:", tax(100));
A Both declarations are hoisted and the second replaces the first everywhere, even for the earlier call – give them different names
B The first function is never hoisted
C Functions cannot return multiplied values
D console.log runs before the functions exist
Question page & discussion
Question 15 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function applyBonus(salary) {
  if (salary < 30000) {
    let salary = 30000;                // raise to the minimum
  }
  return salary + 5000;
}
console.log(applyBonus(20000), applyBonus(40000));
A Parameters cannot be changed
B The bonus should be added before the if
C 30000 must be a string
D let salary inside the if creates a new block variable that shadows the parameter, so the parameter never changes – assign salary = 30000 instead
Question page & discussion
Up next · Exercise 4Scope and Hoisting Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links