JSON MCQs – JSON Operations in JavaScript

21.) How do you access the second element of a JSON array [“apple”, “banana”, “cherry”]?

A) array.get(1)
B) array[1]
C) array[“banana”]
D) array.index(1)

Answer: Option B

Explanation: JSON arrays are accessed using zero-based indexing, so array[1] retrieves the second element.

22.) How do you access the value “Alice” from the JSON object {“name”: “Alice”, “age”: 25}?

A) object[0]
B) object[“Alice”]
C) object.name
D) object[1]

Answer: Option C

Explanation: In JavaScript, properties of JSON objects can be accessed using dot notation (object.name) or bracket notation (object[“name”]).

23.) What is the output of JSON.parse(‘[1, 2, , 4]’)?

A) [1, 2, null, 4]
B) Throws a SyntaxError
C) [1, 2, undefined, 4]
D) [1, 2, , 4]

Answer: Option B

Explanation: JSON does not allow missing values in arrays, so parsing this string results in a SyntaxError.

24.) What is the output of JSON.stringify([1, undefined, 2])?

A) “[1, null, 2]”
B) “[1, undefined, 2]”
C) “[1, , 2]”
D) Throws a TypeError

Answer: Option A

Explanation: In arrays, undefined values are replaced with null during stringification.

25.) What will be the output of this code?

JSON.stringify({ key1: "value1", key2: function() { return "value2"; } });
A) {“key1”: “value1”, “key2”: “value2”}
B) {“key1”: “value1”}
C) Throws an error
D) {“key1”: “value1”, “key2”: null}

Answer: Option B

Explanation: Functions are ignored during stringification, so only key1 is included in the JSON string.

26.) What is the output of the following code?

JSON.stringify({ key: undefined, key2: "value" });
A) {“key”: undefined, “key2”: “value”}
B) {“key”: null, “key2”: “value”}
C) {“key2”: “value”}
D) {“key”: “”, “key2”: “value”}

Answer: Option C

Explanation: Properties with undefined values are omitted during the stringification process.

Leave a Reply

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