LearnFrenzy
Sign in Take a free test

Objects · Exercise 3 of 4

Objects Find the Error

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

Debug fifteen JavaScript object programs: SyntaxErrors in literals, methods and accessors; runtime errors from null objects, lost this, locked properties and invalid or circular JSON; and silent bugs from dot notation with variables, shallow copies, truthiness checks, inherited keys and nested defaults.

Question 11 Easy

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

main.js
const prices = { pen: 10, book: 50, bag: 899 };
function priceOf(item) {
  return prices.item;
}
console.log(priceOf("book"), priceOf("bag"));
A prices.item reads a property literally named "item" – use prices[item]
B The keys must be quoted
C item must be converted to a number
D Functions cannot read outer objects
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
const saved = { name: "Asha", address: { city: "Pune" } };
const draft = { ...saved };
draft.address.city = "Mumbai";        // user edits the draft only
console.log("Saved city:", saved.address.city);
A Spread copies nothing
B Spread makes a shallow copy, so address is shared – use structuredClone(saved) or copy address too
C const objects are always shared
D city must be changed with Object.assign
Question page & discussion
Question 13 Medium

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

main.js
const stock = { pens: 0, books: 12 };
function status(item) {
  if (!stock[item]) return `${item}: not in catalogue`;
  return `${item}: ${stock[item]} left`;
}
console.log(status("pens"), "|", status("books"), "|", status("bags"));
A stock must be a Map
B A stock of 0 is falsy, so it looks like a missing key – test Object.hasOwn(stock, item) instead
C Template literals cannot show numbers
D The ! operator does not work with objects
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
const base = { currency: "INR" };
const prices = Object.create(base);
prices.pen = 10;
prices.book = 50;
let total = 0;
for (const k in prices) total += prices[k];
console.log(total);
A Object.create copies base into prices
B prices.pen must be a string
C += cannot add numbers
D for…in also visits the inherited currency key, adding "INR" – loop over Object.values(prices) instead
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
const defaults = { notify: { email: true, sms: true } };
const user = { notify: { sms: false } };
const settings = { ...defaults, ...user };
console.log(settings.notify.email, settings.notify.sms);
A Spread merges only the top level, so user.notify replaces the whole notify object – merge it too: notify: { ...defaults.notify, ...user.notify }
B Spread cannot copy booleans
C sms must be listed first
D Objects cannot be nested
Question page & discussion
Up next · Exercise 4Objects Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links