LearnFrenzy
Sign in Take a free test

Data Types · Exercise 3 of 4

Data Types Find the Error

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

Debug fifteen JavaScript programs about data types: invalid literals, TypeErrors from null, undefined, symbols and BigInt, and silent bugs from floating-point comparison, isNaN, typeof and shared references.

Question 11 Hard

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

main.js
const order = { id: 12345678901234567890n, total: 499 };
console.log(JSON.stringify(order));
A TypeError: JSON.stringify does not know how to serialize a BigInt
B It prints {"id":12345678901234567890,"total":499}
C It prints {"id":"12345678901234567890n","total":499}
D It prints {"total":499}
Question page & discussion
Question 12 Hard

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

main.js
const inputs = ["42", "abc", "", "  "];
for (const v of inputs) {
  console.log(JSON.stringify(v), isNaN(v) ? "invalid" : "valid");
}
A JSON.stringify changes the values
B isNaN works only on numbers and always returns false for strings
C The loop skips the last element
D isNaN converts its argument first, and empty or blank strings convert to 0 – check v.trim() !== "" && Number.isFinite(Number(v))
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 paid = 0.1 + 0.2;
const due = 0.3;
if (paid === due) {
  console.log("Fully paid");
} else {
  console.log("Amount pending");
}
A 0.1 + 0.2 is 0.30000000000000004, so === fails – compare with a tolerance or in paise (integers)
B === cannot compare numbers
C due must be declared with let
D The else branch always runs
Question page & discussion
Question 14 Medium

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

main.js
function describe(v) {
  if (typeof v === "array") return "list";
  if (typeof v === "object") return "record";
  return typeof v;
}
console.log(describe([1, 2]), describe(null), describe({}));
A describe must be an arrow function
B Objects cannot be passed to functions
C The second if should use ==
D typeof never returns "array", and typeof null is "object" – use Array.isArray(v) and v === null first
Question page & discussion
Question 15 Easy

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

main.js
const template = { title: "Report", pages: 10 };
const draft = template;
draft.pages = 20;
console.log("Template pages:", template.pages);
A pages cannot be changed on a const object
B draft = template copies the reference, so both names point to one object – copy it with { ...template }
C console.log reads the newest variable
D Object properties are always shared between objects
Question page & discussion
Up next · Exercise 4Data Types Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links