JSON MCQs – JSON Operations in JavaScript

JSON is widely used in JavaScript for data exchange and manipulation. Understanding how to perform JSON operations in JavaScript such as parsing, stringifying, accessing, and manipulating JSON data is critical for working with APIs and dynamic web applications. This set of MCQs focuses on JSON operations in JavaScript, helping developers prepare for technical interviews and strengthen their coding skills.

1.) What does the JSON.parse() method do in JavaScript?

A) Converts a JavaScript object into a JSON string
B) Converts a JSON string into a JavaScript object
C) Parses a JSON string into an XML object
D) Validates a JSON string

Answer: Option B

Explanation: The JSON.parse() method takes a JSON string and converts it into a corresponding JavaScript object.

2.) Which method is used to convert a JavaScript object into a JSON string?

A) JSON.stringify()
B) JSON.parse()
C) JSON.encode()
D) JSON.toString()

Answer: Option A

Explanation: JSON.stringify() is used to convert a JavaScript object or value into a JSON string.

3.) What happens when you use JSON.stringify() on an object with a circular reference?

A) It throws a syntax error
B) It converts the object to a string successfully
C) It throws a TypeError
D) It ignores the circular reference

Answer: Option C

Explanation: JSON.stringify() cannot process objects with circular references and will throw a TypeError.

4.) Which of the following JSON strings can be parsed without error?

A) ‘{name: “John”, age: 30}’
B) ‘{“name”: “John”, “age”: 30}’
C) ‘{name: John, age: 30}’
D) ‘{“name”: John, “age”: 30}’

Answer: Option B

Explanation: JSON keys and string values must be enclosed in double quotes to be valid.

5.) What is the result of JSON.parse(‘null’) in JavaScript?

A) Throws a syntax error
B) Converts to the string “null”
C) Returns undefined
D) Returns null

Answer: Option D

Explanation: The JSON string ‘null’ is parsed into the JavaScript null value.

6.) What will be the result of JSON.stringify([1, 2, 3])?

A) “[1,2,3]”
B) “1,2,3”
C) [1, 2, 3]
D) Throws an error

Answer: Option A

Explanation: JSON arrays are converted to strings with their elements in the correct JSON format.

7.) What happens when you use JSON.parse() on an invalid JSON string?

A) Returns null
B) Throws a SyntaxError
C) Converts to an empty object
D) Skips invalid parts

Answer: Option B

Explanation: The JSON.parse() method throws a SyntaxError if the JSON string is not valid.

8.) How can you handle errors when parsing JSON in JavaScript?

A) Using a try…catch block
B) Checking the JSON string with isValid()
C) Using the JSON.validate() method
D) JSON parsing cannot produce errors

Answer: Option A

Explanation: Errors from JSON.parse() can be caught using a try…catch block.

9.) How does JSON.stringify() handle functions in objects?

A) Functions are ignored
B) Functions are converted to strings
C) Throws an error
D) Adds null in place of functions

Answer: Option A

Explanation: Functions are not valid JSON values and are ignored during stringification.

10.) How can you format JSON output with spaces using JSON.stringify()?

A) Add a spaces property to the JSON object
B) Use the JSON.stringify(obj, null, 4) method
C) Use JSON.prettyPrint()
D) Pass a null parameter to JSON.stringify()

Answer: Option B

Explanation: The JSON.stringify() method accepts up to three parameters: 1. The value to be stringified, 2. A replacer function or array (optional), 3. A space argument (optional) to control spacing in the output.

Leave a Reply

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