JSON MCQs – JSON Operations in JavaScript

11.) What will JSON.stringify({ key: undefined }) return?

A) {“key”: “undefined”}
B) {“key”: null}
C) Throws an error
D) {}

Answer: Option D

Explanation: Properties with undefined values are omitted when an object is converted to JSON.

12.) How do you check if a string is valid JSON in JavaScript?

A) Use JSON.isValid()
B) Parse it with JSON.parse() inside a try…catch block
C) Use typeof to check its type
D) Use JSON.check()

Answer: Option B

Explanation: To verify a JSON string, use JSON.parse() in a try…catch block to handle parsing errors.

13.) What happens when you pass a replacer function to JSON.stringify()?

A) It filters the properties to be included in the JSON string
B) It ensures all properties are stringified
C) It validates the JSON string
D) Throws an error if the function is invalid

Answer: Option A

Explanation: The replacer function determines which properties are included in the JSON string by modifying or filtering values during stringification.

14.) Which of the following is NOT a valid use case of JSON.stringify()?

A) Serializing objects for storage or transmission
B) Converting JavaScript objects into readable JSON format
C) Serializing objects with circular references
D) Formatting JSON output with indentation

Answer: Option C

Explanation: JSON.stringify() cannot handle circular references and will throw a TypeError.

15.) What will JSON.stringify(true) return?

A) “true”
B) true
C) “True”
D) Throws an error

Answer: Option A

Explanation: Boolean values are converted to their JSON string representation (“true” or “false”) by JSON.stringify().

16.) How do you deep clone a JSON-compatible object in JavaScript?

A) Use JSON.stringify() and JSON.parse()
B) Use the clone() method
C) Use Object.assign()
D) Use JSON.clone()

Answer: Option A

Explanation: A combination of JSON.stringify() and JSON.parse() is commonly used to deep clone JSON-compatible objects.

17.) How do you check if a given JavaScript object is a valid JSON object?

A) Use typeof to check if it is an object
B) Convert it to a string using JSON.stringify() and back using JSON.parse()
C) Use Array.isArray()
D) Use Object.prototype.toString()

Answer: Option B

Explanation: A round trip of JSON.stringify() and JSON.parse() is a reliable way to verify JSON compatibility.

18.) What is the purpose of the space parameter in JSON.stringify()?

A) To add extra spacing between keys and values
B) To format the output with indentation
C) To add a specific number of spaces to the start of the output
D) To compress the JSON output

Answer: Option B

Explanation: The space parameter is used to format JSON output with a specified number of spaces for readability.

19.) What happens if you call JSON.stringify() on a Date object?

A) The date is converted to a string in ISO format
B) The date is ignored
C) The date is serialized as a timestamp
D) Throws an error

Answer: Option A

Explanation: When you call JSON.stringify() on a Date object, the Date object is converted to a string in the ISO 8601 format, which looks like “YYYY-MM-DDTHH:mm:ss.sssZ”.

20.) Which method can be used to transform a JSON object into an iterable array?

A) Object.entries()
B) Object.keys()
C) Object.values()
D) All of the above

Answer: Option D

Explanation: Methods like Object.entries(), Object.keys(), and Object.values() can be used to extract information from a JSON object in an iterable format.

Leave a Reply

Your email address will not be published. Required fields are marked *