Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” Quiz: Object to Text

What does this code print?

console.log(JSON.stringify({ a: 1 }));
Options
β€’ {"a":1}
β€’ { a: 1 }
β€’ [object Object]
β€’ a,1

Hint : JSON wraps keys in double quotes and drops the spaces.

Exercise 2 β€” Quiz: Result Type

What does this code print?

console.log(typeof JSON.stringify([1, 2]));
Options
β€’ "string"
β€’ "object"
β€’ "array"
β€’ "json"

Hint : The method's whole job is to produce text.

Exercise 3 β€” Quiz: Array to Text

What does this code print?

console.log(JSON.stringify(["Zap", 4]));
Options
β€’ ["Zap",4]
β€’ Zap,4
β€’ [Zap, 4]
β€’ 2

Hint : Arrays keep their brackets, strings keep their quotes.

Exercise 4 β€” Quiz: The Dropped Method

What does this code print?

const o = { a: 1, f() { return 2; } }; console.log(JSON.stringify(o));
Options
β€’ {"a":1}
β€’ {"a":1,"f":2}
β€’ {"a":1,"f":null}
β€’ An error

Hint : JSON stores data, never behaviour.

Exercise 5 β€” Quiz: Make It Readable

Print the object indented by 2 spaces.

Hint : The middle slot stays empty, the last one is the indentation.

Exercise 6 β€” Quiz: Text to Object

What does this code print?

const o = JSON.parse('{"a":5}'); console.log(o.a);
Options
β€’ 5
β€’ "a"
β€’ undefined
β€’ An error

Hint : After parsing, the keys are real properties again.

Exercise 7 β€” Quiz: Parsed Type

What does this code print?

console.log(typeof JSON.parse('{"a":1}'));
Options
β€’ "object"
β€’ "string"
β€’ "json"
β€’ "number"

Hint : The method rebuilds a real value from the text.

Exercise 8 β€” Quiz: Reading a Number

What does this code print?

const o = JSON.parse('{"level":4}'); console.log(o.level + 1);
Options
β€’ 5
β€’ 41
β€’ undefined
β€’ An error

Hint : Numbers come back as real numbers, not text.

Exercise 9 β€” Quiz: Load It

Rebuild the object and print its name.

Hint : One method reads text into an object, which then has its keys back.

Exercise 10 β€” Quiz: Invalid JSON

What happens when `JSON.parse("{a: 1}")` runs?

Options
β€’ It throws a SyntaxError
β€’ It returns undefined
β€’ It returns an empty object
β€’ It fixes the quotes itself

Hint : JSON is strict about quotes around keys.

Exercise 11 β€” Quiz: Round Trip Identity

What does this code print?

const a = { x: 1 }; const b = JSON.parse(JSON.stringify(a)); console.log(a === b);
Options
β€’ false
β€’ true
β€’ undefined
β€’ An error

Hint : Parsing builds a brand-new object.

Exercise 12 β€” Quiz: Deep Copy

What does this code print?

const a = { s: { hp: 40 } }; const b = JSON.parse(JSON.stringify(a)); b.s.hp = 5; console.log(a.s.hp);
Options
β€’ 40
β€’ 5
β€’ undefined
β€’ An error

Hint : The round trip rebuilds every level, not just the first.

Exercise 13 β€” Quiz: Parse Safely

Protect the parse from corrupted text.

Hint : Outside text can always be invalid, so the read goes in the protected area.

Exercise 14 β€” Quiz: Before Reading

A server answer sits in `response` as text. What is the first step?

Options
β€’ Parse it into an object
β€’ Stringify it
β€’ Read response.team directly
β€’ Split it on spaces

Hint : Text has no properties until it is rebuilt.

Exercise 15 β€” Quiz: Nested Path

What does this code print?

const d = JSON.parse('{"team":[{"name":"Zap"}]}'); console.log(d.team[0].name);
Options
β€’ Zap
β€’ undefined
β€’ "team"
β€’ An error

Hint : Array, then first object, then property.

Exercise 16 β€” Quiz: Missing Field

What does this code print?

const d = JSON.parse('{"status":"ok"}'); console.log(d.trainer?.badges ?? 0);
Options
β€’ 0
β€’ undefined
β€’ An error
β€’ null

Hint : One symbol survives the missing link, the other supplies the fallback.

Exercise 17 β€” Quiz: Crashing Dive

What happens on the last line?

const d = JSON.parse('{"status":"error"}'); console.log(d.team.length);
Options
β€’ An error
β€’ 0
β€’ undefined
β€’ Nothing

Hint : Reading a property of undefined is not allowed.

Exercise 18 β€” Quiz: Safe Count

Print the team size, or 0 when there is no team.

Hint : Survive the missing link, then replace the empty answer.

Exercise 19 β€” Quiz: Which Layer?

The JSON is valid but `status` is `"error"`. What handles it?

Options
β€’ The status check
β€’ The try...catch
β€’ Optional chaining
β€’ JSON.stringify

Hint : Parsing succeeded; the problem is what the answer says.

Exercise 20 β€” Quiz: Which Layer, Again

The text that arrived is `{broken`. What handles it?

Options
β€’ The try...catch around parse
β€’ The status check
β€’ Optional chaining
β€’ Nothing can

Hint : This input never becomes an object at all.

Exercise 21 β€” Quiz: Key Quotes

In JSON text, how are keys written?

Options
β€’ Always in double quotes
β€’ Without quotes
β€’ In single quotes
β€’ In backticks

Hint : JSON has one rule for keys and it never bends.

Exercise 22 β€” Quiz: Set in JSON

What does this code print?

console.log(JSON.stringify(new Set([1, 2])));
Options
β€’ {}
β€’ [1,2]
β€’ Set(2)
β€’ An error

Hint : JSON doesn't know this collection; convert it to an array first.

Exercise 23 β€” Quiz: undefined Property

What does this code print?

console.log(JSON.stringify({ a: 1, b: undefined }));
Options
β€’ {"a":1}
β€’ {"a":1,"b":undefined}
β€’ {"a":1,"b":null}
β€’ An error

Hint : A value that means nothing is simply left out.

Exercise 24 β€” Quiz: Why JSON?

Why turn an object into JSON before saving or sending it?

Options
β€’ Files and networks only carry text
β€’ It makes the object faster
β€’ Objects can't have arrays
β€’ It removes the need for variables

Hint : Think about what can actually travel outside the program.

Exercise 25 β€” Quiz: The Pair

Which method is the opposite of `JSON.stringify()`?

Options
β€’ JSON.parse()
β€’ JSON.read()
β€’ JSON.load()
β€’ JSON.object()

Hint : One turns objects into text, the other turns text back into objects.

Back to the course