JavaScript JSON Handling (stringify, parse pitfalls) Quiz

JavaScript
0 Passed
0% acceptance

Answer 40 questions covering JSON.stringify basics, JSON.parse behavior, handling undefined and functions, circular structure errors, Date serialization, number precision, reviver/replacer usage, and safe parsing practices.

40 Questions
~80 minutes
1

Question 1

What does JSON.stringify primarily do?

A
Converts JavaScript values into a JSON-formatted string
B
Converts JSON strings into JavaScript values
C
Validates schema
D
Encrypts data
2

Question 2

Why does JSON.stringify ignore non-enumerable properties?

A
Only enumerable own properties are traversed during serialization
B
Non-enumerable properties throw errors
C
Non-enumerable properties become null
D
Enumerability is irrelevant
3

Question 3

What prints?

javascript
const user = { name: 'Ayu', age: 29 }
console.log(JSON.stringify(user))
A
{"name":"Ayu","age":29}
B
{name:Ayu,age:29}
C
undefined
D
[object Object]
4

Question 4

What is the second parameter of JSON.stringify used for?

A
A replacer that filters or transforms values during serialization
B
A reviver that runs after parsing
C
Indentation width
D
Network timeout
5

Question 5

What does JSON.parse do?

A
Converts a JSON string into the corresponding JavaScript value
B
Formats JavaScript objects for display
C
Hashes JSON data
D
Detects SQL injection
6

Question 6

Which JSON string is invalid for JSON.parse?

A
"{\"count\":10}"
B
"[1, 2, 3]"
C
"true"
D
"{name:\"A\"}"
7

Question 7

What prints?

javascript
const data = '{"price": 7.5, "active": true}'
const parsed = JSON.parse(data)
console.log(parsed.price + parsed.active)
A
8.5
B
7.5true
C
NaN
D
Throws SyntaxError
8

Question 8

How does the reviver parameter in JSON.parse work?

A
It allows transforming values as they are parsed, bottom-up through the structure
B
It pretty-prints the output
C
It filters keys before parsing
D
It encrypts the JSON string
9

Question 9

How does JSON.stringify treat undefined in objects?

A
Properties with undefined values are omitted from the result
B
undefined becomes null
C
undefined becomes 0
D
Serialization fails
10

Question 10

What happens to functions during JSON.stringify?

A
They are removed since functions are not valid JSON values
B
They are serialized as their source code
C
They become empty objects
D
They throw errors automatically
11

Question 11

What prints?

javascript
const item = { id: 1, onClick() {} }
console.log(JSON.stringify(item))
A
{"id":1}
B
{"id":1,"onClick":{}}
C
{"id":1,"onClick":"function"}
D
Throws SyntaxError
12

Question 12

How does JSON.stringify treat undefined in arrays?

A
Undefined array elements become null to preserve length
B
They are dropped, shrinking the array
C
They become 0
D
They throw errors
13

Question 13

Why does JSON.stringify throw when encountering a circular reference?

A
JSON format cannot represent cycles, so serialization raises TypeError
B
Cycles corrupt memory
C
Cycles convert to zero
D
Cycles force synchronous IO
14

Question 14

What prints?

javascript
const obj = { name: 'loop' }
obj.self = obj
try {
  JSON.stringify(obj)
} catch (err) {
  console.log(err.name)
}
A
TypeError
B
SyntaxError
C
RangeError
D
ReferenceError
15

Question 15

How can you serialize objects with cycles safely?

A
Use libraries that track references (e.g., flatted) or write custom serialization logic that replaces cycles with identifiers
B
Set JSON.stringify depth to infinity
C
Convert cycles to eval strings
D
Cycles cannot be represented by any means
16

Question 16

How are Date objects serialized by JSON.stringify?

A
As ISO-8601 strings via Date.prototype.toJSON
B
As timestamps (numbers)
C
As objects with year/month/day keys
D
Dates cannot be serialized
17

Question 17

What prints?

javascript
const data = { createdAt: new Date('2025-01-01T10:00:00Z') }
const text = JSON.stringify(data)
console.log(text)
const parsed = JSON.parse(text)
console.log(parsed.createdAt instanceof Date)
A
{"createdAt":"2025-01-01T10:00:00.000Z"} and false
B
{"createdAt":{"date":"..."} } and true
C
{"createdAt":1735725600000} and true
D
Throws TypeError
18

Question 18

How can reviver help with Date restoration?

A
Reviver can detect ISO strings and convert them back to Date objects during parsing
B
Reviver serializes Dates into numbers
C
Reviver prevents Date serialization
D
Reviver only works for arrays
19

Question 19

Why can JSON.stringify lose precision for very large integers?

A
JavaScript numbers are IEEE-754 doubles, so values beyond 2^53-1 cannot be represented exactly
B
JSON truncates numbers randomly
C
JSON stores numbers as strings
D
Large numbers throw SyntaxError
20

Question 20

What prints?

javascript
const big = { value: 9007199254740993 }
const text = JSON.stringify(big)
console.log(text)
const parsed = JSON.parse(text)
console.log(parsed.value === big.value)
A
{"value":9007199254740992} and false
B
{"value":9007199254740993} and true
C
{"value":"9007199254740993"} and false
D
Throws RangeError
21

Question 21

How can you preserve arbitrary-precision integers through JSON?

A
Serialize them as strings and convert to BigInt manually after parsing
B
Enable JSON bigint mode flag
C
Use JSON.parseExactNumber()
D
Precision cannot be preserved
22

Question 22

What can a replacer array do in JSON.stringify?

A
Whitelist the set of property names to include in the output
B
Blacklist keys
C
Automatically encrypt values
D
Transform the final string
23

Question 23

What prints?

javascript
const payload = { id: 1, secret: 'abc', nested: { secret: 'xyz', id: 2 } }
const replacer = (key, value) => (key === 'secret' ? undefined : value)
console.log(JSON.stringify(payload, replacer))
A
{"id":1,"nested":{"id":2}}
B
{"id":1,"secret":null,"nested":{"secret":null,"id":2}}
C
{"id":1}
D
Throws TypeError
24

Question 24

What does a reviver return to delete a key during parsing?

A
undefined
B
null
C
false
D
0
25

Question 25

How can reviver help convert strings to numbers safely?

A
By detecting numeric strings and returning Number(value), you ensure resulting objects contain actual numbers
B
Reviver runs before parsing, so it cannot help
C
Reviver only handles arrays
D
Reviver automatically validates schemas
26

Question 26

Why should JSON.parse be wrapped in try/catch when dealing with external data?

A
Malformed JSON will throw SyntaxError, so catching prevents crashes and allows fallback logic
B
JSON.parse never throws
C
try/catch converts JSON to YAML
D
Security policies forbid parsing without try/catch
27

Question 27

What prints?

javascript
const bad = "{\"id\":1,,}"
try {
  JSON.parse(bad)
} catch (error) {
  console.log(error instanceof SyntaxError)
}
A
true
B
false
C
undefined
D
Throws TypeError
28

Question 28

Which practice helps avoid prototype pollution when parsing JSON?

A
Validating parsed objects before merging them into application state
B
Using eval for performance
C
Converting JSON to DOM nodes first
D
Disabling reviver
29

Question 29

Why is eval an unsafe alternative to JSON.parse?

A
eval executes arbitrary code, so untrusted JSON could run malicious scripts
B
eval is slower but safe
C
eval automatically sanitizes input
D
eval cannot run strings
30

Question 30

How does JSON.stringify handle Symbol-valued properties?

A
Symbol properties are ignored entirely
B
They become their description strings
C
They throw errors
D
They serialize as {symbol:...}
31

Question 31

What prints?

javascript
const weird = { key: Symbol('token') }
console.log(JSON.stringify(weird))
A
{}
B
{"key":"Symbol(token)"}
C
undefined
D
Throws TypeError
32

Question 32

Why can JSON.parse cause performance issues on massive payloads?

A
Parsing is CPU- and memory-intensive; huge strings block the main thread and allocate large objects
B
JSON.parse writes to disk
C
JSON.parse always fetches over network
D
Large payloads throw automatically
33

Question 33

How can streaming parsers mitigate large JSON issues?

A
They process chunks sequentially, reducing peak memory and allowing early data handling
B
They increase payload size
C
They require eval
D
They eliminate the need for schema checks
34

Question 34

What prints?

javascript
const json = '{"value": NaN }'
try {
  JSON.parse(json)
} catch (error) {
  console.log(error.message)
}
A
Unexpected token N in JSON at position 10
B
NaN
C
undefined
D
No output
35

Question 35

Why does JSON not support Infinity or NaN?

A
JSON specification restricts number format to finite decimal representations
B
Infinity would overflow memory
C
NaN is redundant with null
D
They are deprecated keywords
36

Question 36

How can you safely stringify user-generated content containing control characters?

A
JSON.stringify escapes control characters automatically, but further escaping may be needed before embedding in HTML to prevent injection
B
By removing all newline characters
C
By converting content to eval
D
By base64 encoding only
37

Question 37

What prints?

javascript
const input = "{\"__proto__\":{\"polluted\":true}}"
const parsed = JSON.parse(input)
console.log(parsed.polluted, Object.prototype.polluted)
A
undefined undefined
B
true true
C
true undefined
D
undefined true
38

Question 38

Why is manual schema validation important after parsing trusted but complex JSON?

A
Even trusted sources might evolve fields or break assumptions; validation catches drift early
B
Validation speeds up parsing
C
Validation encrypts data
D
Validation replaces JSON.parse
39

Question 39

What prints?

javascript
const text = JSON.stringify({ emoji: "😀" })
console.log(text.length)
const parsed = JSON.parse(text)
console.log(parsed.emoji)
A
15 and 😀
B
2 and 😀
C
15 and \uD83D
D
Throws SyntaxError
40

Question 40

Which best practice helps when logging JSON.stringify output for debugging?

A
Provide indentation (third argument) to improve readability and spot missing fields
B
Disable console logs entirely
C
Convert JSON to XML first
D
Always stringify twice

QUIZZES IN JavaScript