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
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()
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
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}’
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
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
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
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
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
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()
Related