JSON MCQs – Advanced JSON Structures

Advanced JSON structures, such as nested objects, arrays, and mixed data types, are frequently encountered in modern applications. These structures are critical for modeling complex data in APIs and databases. The following MCQs are designed to provide insights into advanced JSON structures, ensuring you’re well-prepared for technical interviews and problem-solving tasks.

1.) Which of the following is an example of a nested JSON object?

A) { “name”: “John” }
B) { “person”: { “name”: “John”, “age”: 30 } }
C) [ “John”, 30 ]
D) { “name”: “John”, “age”: 30 }

Answer: Option B

Explanation: A nested JSON object contains an object as a value for one or more of its keys.

2.) What does a JSON array of objects look like?

A) { “object1”: “value1”, “object2”: “value2” }
B) [ { “key”: “value” }, { “key”: “value” } ]
C) { “array”: [ “value1”, “value2” ] }
D) [ “object1”, “object2” ]

Answer: Option B

Explanation: A JSON array of objects contains multiple objects within square brackets.

3.) Which of the following is valid JSON for a mixed data type array?

A) [ “Alice”, 25, true ]
B) [ { “name”: “Alice” }, 25, “true” ]
C) [ “Alice”, “25”, true ]
D) All of the above

Answer: Option D

Explanation: JSON arrays can contain mixed data types, including strings, numbers, booleans, and objects.

4.) How do you access the name of the first employee in the above JSON structure?

A) department.employees[0].name
B) department.employees[1].name
C) employees[0].name
D) department[employees][name]

Answer: Option A

Explanation: To access nested objects within an array, use dot notation followed by array indexing.

5.) How do you represent a key-value pair where the value is an array?

A) { “key”: { “value1”: “value2” } }
B) { “key”: [ “value1”, “value2” ] }
C) { “key”: “value1”, “value2” }
D) [ { “key”: “value1” } ]

Answer: Option B

Explanation: A JSON key can have an array as its value, denoted by square brackets.

6.) What is the output of JSON.stringify() on a nested object?

A) A string representation of the nested object
B) A flattened version of the object
C) A deep copy of the object
D) Throws an error

Answer: Option A

Explanation: JSON.stringify() converts a nested object into a string representation.

7.) What is the maximum depth for nesting in JSON?

A) Unlimited
B) 10 levels
C) 5 levels
D) Depends on the programming language and environment

Answer: Option D

Explanation: While JSON itself has no strict nesting limit, practical limits depend on the environment or interpreter.

8.) Which of the following is an invalid JSON structure?

A) { “key”: [1, 2, { “nested”: “value” }] }
B) { “key”: { “nested”: { “deep”: [1, 2] } } }
C) { “key”: “value”, “key”: “anotherValue” }
D) D)

Answer: Option C

Explanation: JSON does not allow duplicate keys within the same object.

9.) How can you find the length of an array inside a JSON object?

A) jsonObject.array.length
B) jsonObject.array.size()
C) jsonObject.array.count()
D) jsonObject.array.len

Answer: Option A

Explanation: Use the length property to determine the size of an array in JavaScript.

10.) What is the result of trying to access a non-existent key in a JSON object?

A) Throws an error
B) Returns null
C) Returns undefined
D) Returns an empty string

Answer: Option C

Explanation: Accessing a non-existent key in a JavaScript object returns undefined.

Leave a Reply

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