MCQs for Day 13: JSON in JavaScript

Rashmi Mishra
0

 MCQs for Day 13: JSON in JavaScript


1.  What does JSON stand for?

o    A) Java Syntax Object Notation

o    B) JavaScript Object Notation

o    C) Java Source Object Network

o    D) Java Simple Object Network

o    Answer: B
Explanation: JSON stands for JavaScript Object Notation, a lightweight data-interchange format.

2.  Which data format does JSON resemble in JavaScript?

o    A) XML

o    B) Array

o    C) Object

o    D) String

o    Answer: C
Explanation: JSON is similar to JavaScript objects and uses a key-value pair format.

3.  In JSON, data is stored in:

o    A) XML format

o    B) Key-value pairs

o    C) Plain text

o    D) HTML format

o    Answer: B
Explanation: JSON uses key-value pairs to structure its data.

4.  Which of the following is a valid JSON format?

o    A) {key: value}

o    B) {"key": value}

o    C) {key: "value"}

o    D) {"key": "value"}

o    Answer: D
Explanation: JSON keys and values must be enclosed in double quotes if they are strings.

5.  What does JSON format NOT support?

o    A) Arrays

o    B) Numbers

o    C) Functions

o    D) Strings

o    Answer: C
Explanation: JSON does not support functions, as it’s purely a data format.

6.  Which of the following data types is NOT supported in JSON?

o    A) Boolean

o    B) Date

o    C) Null

o    D) Array

o    Answer: B
Explanation: JSON does not have a built-in date format; dates are usually stored as strings.

7.  JSON is commonly used for:

o    A) Storing HTML content

o    B) Configuring databases

o    C) Exchanging data between a server and web applications

o    D) Writing CSS rules

o    Answer: C
Explanation: JSON is widely used for data exchange between servers and web applications.

8.  Which of these is NOT a valid JSON string?

o    A) {"name": "Alice"}

o    B) {"age": 25, "city": "New York"}

o    C) {'title': 'Book'}

o    D) {"price": 29.99}

o    Answer: C
Explanation: JSON strings must use double quotes for both keys and values.

9.  In JSON, arrays are represented by:

o    A) Curly braces {}

o    B) Parentheses ()

o    C) Angle brackets <>

o    D) Square brackets []

o    Answer: D
Explanation: JSON arrays are enclosed in square brackets.

10.        In JSON, how would you represent a list of colors?

o    A) ["red", "green", "blue"]

o    B) {colors: "red", "green", "blue"}

o    C) {[red, green, blue]}

o    D) ["red"; "green"; "blue"]

o    Answer: A
Explanation: JSON arrays are enclosed in square brackets, with each value separated by a comma.


JSON Conversion in JavaScript

11.        Which method is used to convert a JavaScript object to a JSON string?

o    A) JSON.parse()

o    B) JSON.convert()

o    C) JSON.stringify()

o    D) JSON.toString()

o    Answer: C
Explanation: JSON.stringify() is used to convert JavaScript objects to JSON strings.

12.        What is the output of JSON.stringify({name: "Alice", age: 25})?

o    A) name: Alice, age: 25

o    B) {name: "Alice", age: 25}

o    C) {"name":"Alice","age":25}

o    D) {"name": Alice, "age": 25}

o    Answer: C
Explanation: JSON.stringify() converts the object into a JSON string with keys and values enclosed in double quotes.

13.        Which method is used to convert a JSON string back into a JavaScript object?

o    A) JSON.stringify()

o    B) JSON.parse()

o    C) JSON.convert()

o    D) JSON.toObject()

o    Answer: B
Explanation: JSON.parse() is used to parse a JSON string and convert it into a JavaScript object.

14.        What will JSON.parse('{"age":25}') return?

o    A) {age: 25}

o    B) {"age":25}

o    C) {age: "25"}

o    D) { age: 25 }

o    Answer: B
Explanation: JSON.parse() converts the JSON string into a JavaScript object with key-value pairs.

15.        What type of data does JSON.stringify() return?

o    A) Object

o    B) Array

o    C) String

o    D) Boolean

o    Answer: C
Explanation: JSON.stringify() returns a JSON-formatted string.

16.        If you want to retrieve data from a JSON string, which method should you use?

o    A) JSON.parse()

o    B) JSON.convert()

o    C) JSON.read()

o    D) JSON.stringify()

o    Answer: A
Explanation: JSON.parse() is used to convert JSON strings into JavaScript objects for data retrieval.


Using JSON with Arrays and Objects

17.        Given const jsonString = '[1, 2, 3]', what will JSON.parse(jsonString) return?

o    A) ["1", "2", "3"]

o    B) {1, 2, 3}

o    C) [1, 2, 3]

o    D) { "1", "2", "3" }

o    Answer: C
Explanation: JSON.parse converts the JSON array into a JavaScript array.

18.        Which JSON format is correct for an array of fruits?

o    A) ['apple', 'banana', 'cherry']

o    B) {"apple", "banana", "cherry"}

o    C) ["apple", "banana", "cherry"]

o    D) {["apple"], ["banana"], ["cherry"]}

o    Answer: C
Explanation: JSON arrays are enclosed in square brackets with values separated by commas.

19.        What will JSON.stringify(["apple", "banana", "cherry"]) output?

o    A) ["apple", "banana", "cherry"]

o    B) {"apple", "banana", "cherry"}

o    C) ["apple"; "banana"; "cherry"]

o    D) {apple: "banana", cherry: ""}

o    Answer: A
Explanation: JSON.stringify() converts the array into a JSON string format.

20.        What will JSON.parse('[true, false, true]') return?

o    A) [true, false, true]

o    B) {true: false, true: ""}

o    C) [1, 0, 1]

o    D) ["true", "false", "true"]

o    Answer: A
Explanation: JSON.parse() converts the JSON string into a JavaScript array containing boolean values.


Working with JSON in Applications

21.        Which of the following correctly describes the role of JSON in client-server communication?

o    A) JSON is used for styling web pages.

o    B) JSON is used to format and exchange data between client and server.

o    C) JSON is used for creating layouts.

o    D) JSON replaces HTML in web development.

o    Answer: B
Explanation: JSON is primarily used to exchange data between a client and server in web applications.

22.        In JSON, objects are represented by:

o    A) {}

o    B) []

o    C) <>

o    D) ()

o    Answer: A
Explanation: JSON objects are enclosed in curly braces {}.

23.        What would JSON.stringify({title: "Inception", rating: 8.8}) produce?

o    A) {title: Inception, rating: 8.8}

o    B) {"title": "Inception", "rating": 8.8}

o    C) {"title": "Inception", "rating": 8.8}

o    D) {title: "Inception", rating: "8.8"}

o    Answer: C
Explanation: JSON.stringify converts the object into a JSON string with keys and values properly formatted.


Would you like me to continue creating more JSON-related MCQs or focus on another topic?

4o

You said:

yes

ChatGPT said:

ChatGPT

Here are more multiple-choice questions on JSON in JavaScript to deepen understanding. Each question includes an explanation to clarify the correct answer.


Advanced JSON Parsing and Stringifying

24.        What will JSON.parse('[1, "two", true]') return?

o    A) [1, "two", true]

o    B) {1: "two", "true": ""}

o    C) ["1", "two", "true"]

o    D) Error

o    Answer: A
Explanation: JSON.parse will return a JavaScript array with the values as parsed from the JSON string.

25.        Which of these methods can throw an error if the input is not a valid JSON string?

o    A) JSON.stringify()

o    B) JSON.parse()

o    C) JSON.convert()

o    D) JSON.toObject()

o    Answer: B
Explanation: JSON.parse() can throw an error if the input is not correctly formatted as JSON.

26.        What is the result of JSON.stringify({"key": undefined})?

o    A) {"key": null}

o    B) {"key": undefined}

o    C) {}

o    D) null

o    Answer: C
Explanation: In JSON, properties with undefined values are omitted from the output string.

27.        Which method is used to handle circular references in objects when using JSON.stringify()?

o    A) JSON.handleCircular()

o    B) JSON.parse()

o    C) JSON.stringifyWithCircular()

o    D) JSON.stringify() cannot handle circular references by itself

o    Answer: D
Explanation: JSON.stringify() cannot handle circular references and will throw an error if such references are present.

28.        What does JSON.stringify() do if an object has a property with a function as its value?

o    A) Throws an error

o    B) Ignores the property

o    C) Converts the function to a string

o    D) Converts the function to null

o    Answer: B
Explanation: JSON does not support functions as values, so JSON.stringify() ignores any properties that are functions.

29.        What is the output of JSON.stringify([null, undefined, NaN])?

o    A) [null, null, null]

o    B) [undefined, undefined, undefined]

o    C) [null, undefined, "NaN"]

o    D) [null, null, null]

o    Answer: A
Explanation: undefined and NaN are converted to null in JSON format.

30.        What does JSON.parse('"\\u0041"') output?

o    A) "u0041"

o    B) "A"

o    C) "\\u0041"

o    D) Error

o    Answer: B
Explanation: The Unicode escape \u0041 represents the character A.


JSON Structure and Formatting

31.        Which of the following is the correct way to represent an empty JSON array?

o    A) {}

o    B) []

o    C) [""]

o    D) {empty: true}

o    Answer: B
Explanation: An empty JSON array is represented by square brackets [].

32.        What will JSON.stringify({x: 1, y: [2, 3]}) output?

o    A) {x: "1", y: [2, 3]}

o    B) {"x": "1", "y": [2, 3]}

o    C) {"x":1,"y":[2,3]}

o    D) {"x": "1", "y": ["2", "3"]}

o    Answer: C
Explanation: JSON.stringify preserves the data structure, converting numbers and arrays correctly to JSON.

33.        In JSON, which symbol is used to represent an array?

o    A) {}

o    B) []

o    C) <>

o    D) ()

o    Answer: B
Explanation: Arrays in JSON are enclosed in square brackets [].

34.        What will be the output of JSON.stringify({name: "Alice", age: 30})?

o    A) {name: "Alice", age: 30}

o    B) "{name: Alice, age: 30}"

o    C) {"name": "Alice", "age": 30}

o    D) "{'name': 'Alice', 'age': 30}"

o    Answer: C
Explanation: JSON.stringify correctly formats the object with double-quoted keys and values.


Working with Nested JSON

35.        How would you access the value of "city" in the following JSON? { "address": { "city": "New York" } }

o    A) address.city

o    B) address["city"]

o    C) address -> city

o    D) Both A and B

o    Answer: D
Explanation: Both dot notation (address.city) and bracket notation (address["city"]) can be used in JavaScript to access JSON properties.

36.        What will JSON.parse('{ "scores": [10, 20, 30] }') produce?

o    A) {"scores": [10, 20, 30]}

o    B) { scores: "10, 20, 30" }

o    C) { scores: [10, 20, 30] }

o    D) Error

o    Answer: C
Explanation: JSON.parse converts the JSON string into a JavaScript object with an array as the value of the scores key.

37.        Which JSON is correctly formatted for a nested object?

o    A) { "person": { "name": "Bob", "age": 25 } }

o    B) { person: { name: "Bob", age: 25 } }

o    C) person = { "name": "Bob", "age": 25 }

o    D) {"person": {"name": "Bob", "age": "25"}}

o    Answer: A
Explanation: Nested objects in JSON must use double quotes around keys and values (if string type).


Practical JSON Applications

38.        Which of the following is true about JSON in web development?

o    A) JSON is used only for server-side scripting

o    B) JSON is used to describe web page layouts

o    C) JSON is used as a format to exchange data between a client and a server

o    D) JSON is used for CSS styling

o    Answer: C
Explanation: JSON is widely used for data exchange between servers and clients in web applications.

39.        What does JSON.parse('{"height": null}') return?

o    A) {"height": undefined}

o    B) {"height": null}

o    C) {height: 0}

o    D) Error

o    Answer: B
Explanation: JSON.parse retains the null value as it is, resulting in { height: null }.

40.        Which of these functions is useful when sending data in JSON format to a server?

o    A) JSON.send()

o    B) JSON.format()

o    C) JSON.stringify()

o    D) JSON.write()

o    Answer: C
Explanation: JSON.stringify() is often used to convert data into JSON format before sending it to a server.

41.        What would JSON.stringify({a: 1, b: [true, false]}) return?

o    A) {a: 1, b: [true, false]}

o    B) {"a": 1, "b": [true, false]}

o    C) {"a": "1", "b": ["true", "false"]}

o    D) {"a": 1, "b": [1, 0]}

o    Answer: B
Explanation: JSON.stringify will correctly format the object, preserving the array and boolean values as they are.

42.        How would you represent a boolean value false in JSON?

o    A) "false"

o    B) false

o    C) "no"

o    D) 0

o    Answer: B
Explanation: JSON represents booleans as true or false without quotes.

43.        What will JSON.parse('[{"id": 1}, {"id": 2}]') output?

o    A) [{id: 1}, {id: 2}]

o    B) [{"id": 1, "id": 2}]

o    C) [{ "id": 1 }, { "id": 2 }]

o    D) [{"id": "1"}, {"id": "2"}]

o    Answer: C
Explanation: JSON.parse returns an array of objects, preserving the key-value pairs.


I can continue with more, or focus on specific types of JSON MCQs if you'd like!

4o

You said:

yes

ChatGPT said:

ChatGPT

Here are additional JSON-related multiple-choice questions with explanations, diving deeper into practical applications, advanced usage, and common pitfalls in JSON handling.


Advanced JSON Manipulation

44.        What is the output of JSON.stringify([1, "two", { "key": "value" }])?

o    A) [1, "two", {"key": "value"}]

o    B) { "0": 1, "1": "two", "2": { "key": "value" } }

o    C) "{1, 'two', { 'key': 'value' }}"

o    D) Error

o    Answer: A
Explanation: JSON.stringify correctly converts the array and nested object to JSON format with the expected syntax.

45.        What will JSON.stringify({ key: "value", extra: undefined }) output?

o    A) {"key": "value", "extra": undefined}

o    B) {"key": "value"}

o    C) { "key": "value", "extra": null }

o    D) {}

o    Answer: B
Explanation: Properties with undefined values are omitted in JSON.stringify’s output.

46.        Which of the following JSON strings is valid?

o    A) {'key': 'value'}

o    B) {"key": value}

o    C) {"key": "value"}

o    D) {key: value}

o    Answer: C
Explanation: JSON keys and values must be wrapped in double quotes unless the value is a number or boolean.

47.        What will JSON.parse('"[1,2,3]"') output?

o    A) [1,2,3]

o    B) "1,2,3"

o    C) [ "1", "2", "3" ]

o    D) "[1,2,3]"

o    Answer: D
Explanation: Since the string itself is wrapped in double quotes, it is treated as a single string rather than an array.

48.        Which method allows you to replace certain values during JSON serialization?

o    A) JSON.replacer()

o    B) JSON.stringify() with a replacer function

o    C) JSON.replace()

o    D) JSON.parser()

o    Answer: B
Explanation: JSON.stringify() can accept a replacer function as an optional parameter to transform values.

49.        What is the result of JSON.stringify({a: 1, b: 2}, null, 2)?

o    A) {"a":1,"b":2}

o    B) {"a": 1, "b": 2}

o    C) { "a": 1, "b": 2 }

o    D) Error

o    Answer: C
Explanation: The third parameter in JSON.stringify() specifies the number of spaces for indentation.

50.        Which of these is true for JSON in terms of security?

o    A) JSON is immune to injection attacks

o    B) JSON is vulnerable to certain injection attacks

o    C) JSON is only secure when parsed on the client-side

o    D) JSON must always be encrypted

o    Answer: B
Explanation: JSON can be susceptible to injection attacks if not handled securely, especially if malicious JSON is parsed on the server side.

51.        What will JSON.parse('{"name": "Alice", "age": "twenty"}') output?

o    A) { name: "Alice", age: 20 }

o    B) { "name": "Alice", "age": NaN }

o    C) { "name": "Alice", "age": "twenty" }

o    D) Error

o    Answer: C
Explanation: JSON.parse does not convert values to their correct data types; it keeps values as strings if they’re in string form.

52.        When using JSON.stringify({a: [1, 2], b: { c: "text" }}, null, 4), how will the output look?

o    A) "{a:[1,2],b:{c:'text'}}"

o    B) A JSON string formatted with 4 spaces for indentation

o    C) A JSON string formatted with no spacing or indentation

o    D) Error

o    Answer: B
Explanation: The third parameter 4 specifies indentation level in spaces, creating a well-formatted JSON string.

53.        What would JSON.parse('{}') return?

o    A) null

o    B) undefined

o    C) {}

o    D) Error

o    Answer: C
Explanation: Parsing an empty JSON object returns an empty JavaScript object {}.

54.        Which statement about JSON.parse() is correct?

o    A) It only works with arrays.

o    B) It converts JSON strings into JavaScript objects.

o    C) It converts JavaScript objects to JSON format.

o    D) It only works with primitive data types.

o    Answer: B
Explanation: JSON.parse() parses JSON strings, transforming them into JavaScript objects.


JSON in AJAX and APIs

55.        When using JSON data in an API call, which content type is typically specified in the request header?

o    A) text/html

o    B) application/x-www-form-urlencoded

o    C) application/json

o    D) text/plain

o    Answer: C
Explanation: application/json is the standard MIME type for JSON data.

56.        Which of the following describes a correct JSON structure to send multiple user objects to an API?

o    A) [ { "name": "Alice", "age": 25 }, { "name": "Bob", "age": 30 } ]

o    B) { "users": { "name": "Alice", "age": 25 }, "name": "Bob", "age": 30 }

o    C) { "user1": "Alice", "user2": "Bob" }

o    D) [ "name": "Alice", "age": 25, "name": "Bob", "age": 30 ]

o    Answer: A
Explanation: An array of JSON objects is the correct structure to send multiple user objects.

57.        What does JSON.parse('{"a":1}') return?

o    A) undefined

o    B) null

o    C) { "a": 1 }

o    D) { "a": "1" }

o    Answer: C
Explanation: JSON.parse converts the JSON string into a JavaScript object.

58.        In AJAX, if JSON data is received, what is the typical next step to use this data in JavaScript?

o    A) JSON.stringify the data

o    B) Directly use it without parsing

o    C) Use JSON.parse() on the data

o    D) Convert to XML

o    Answer: C
Explanation: JSON.parse() is used to convert JSON data into a usable JavaScript object.

59.        In JSON, can an object have duplicate keys?

o    A) Yes, JSON allows it.

o    B) No, keys must be unique.

o    C) Only if they have different values.

o    D) Only in arrays.

o    Answer: B
Explanation: JSON objects require unique keys; otherwise, behavior is undefined.

60.        Which of the following JSON strings is valid for sending a nested address object?

o    A) {"address": { city: "New York", state: "NY" }}

o    B) {"address": {"city": "New York", "state": "NY"}}

o    C) {"address": ["city": "New York", "state": "NY"]}

o    D) address = { "city": "New York", "state": "NY" }

o    Answer: B
Explanation: Proper JSON format requires double-quoted keys and values for strings in nested objects.


Let me know if you'd like further questions or a focus on any particular JSON topics!

4o

You said:

yes

ChatGPT said:

ChatGPT

Here’s an additional set of multiple-choice questions focused on JSON, covering nuanced aspects, best practices, and real-world applications in JavaScript.


Intermediate and Advanced JSON Usage

61.        Which JSON string is valid for representing a list of product IDs?

o    A) { "products": "101, 102, 103" }

o    B) { "products": [101, 102, 103] }

o    C) [ "products": [101, 102, 103] ]

o    D) products: [101, 102, 103]

o    Answer: B
Explanation: JSON arrays are represented with square brackets, and key-value pairs must be enclosed in curly braces.

62.        Which statement about JSON objects is true?

o    A) JSON object keys can be any data type.

o    B) JSON objects can have multiple values for the same key.

o    C) JSON object keys must be unique strings.

o    D) JSON allows only string values.

o    Answer: C
Explanation: JSON object keys must be unique and are always strings.

63.        How will JSON.stringify({ a: NaN, b: Infinity }) output these properties?

o    A) { "a": NaN, "b": Infinity }

o    B) { "a": "NaN", "b": "Infinity" }

o    C) { "a": null, "b": null }

o    D) {}

o    Answer: C
Explanation: NaN and Infinity are converted to null in JSON.

64.        Which method in JavaScript is commonly used to fetch JSON data from an API?

o    A) JSON.fetch()

o    B) XMLHttpRequest()

o    C) fetch()

o    D) API.getJSON()

o    Answer: C
Explanation: The fetch() function is a modern way to retrieve JSON data from APIs.

65.        If you want to add indentation to a JSON string, which parameter should you use with JSON.stringify()?

o    A) The first parameter

o    B) The second parameter

o    C) The third parameter

o    D) It’s not possible to add indentation

o    Answer: C
Explanation: The third parameter in JSON.stringify() allows you to add spaces for readability.

66.        In JSON, how are boolean values represented?

o    A) True and False

o    B) "true" and "false"

o    C) 1 and 0

o    D) true and false

o    Answer: D
Explanation: JSON boolean values are lowercase true and false without quotes.

67.        What would JSON.stringify([1, "two", null]) output?

o    A) "[1, 'two', null]"

o    B) "[1, "two", null]"

o    C) [1, "two", null]

o    D) "[1, \"two\", null]"

o    Answer: D
Explanation: JSON.stringify returns a string, and internal double quotes are escaped.

68.        What will JSON.stringify({ foo: function() {} }) return?

o    A) { "foo": "function" }

o    B) { "foo": null }

o    C) { "foo": {} }

o    D) {}

o    Answer: D
Explanation: Functions are omitted in JSON serialization, so they don’t appear in the final string.

69.        What’s the purpose of the second parameter in JSON.stringify(object, replacer)?

o    A) To handle circular references

o    B) To filter or transform properties before serialization

o    C) To add spaces for indentation

o    D) To exclude arrays from the output

o    Answer: B
Explanation: The replacer function in JSON.stringify allows modification or filtering of values.

70.        If you need to send nested JSON data in a POST request, how should it be represented in the HTTP header?

o    A) Content-Type: text/json

o    B) Content-Type: application/json

o    C) Content-Type: json

o    D) Content-Type: application/x-www-form-urlencoded

o    Answer: B
Explanation: The correct header for JSON data is Content-Type: application/json.

71.        What is the correct JSON representation for the following JavaScript object { isActive: true, score: 100 }?

o    A) {"isActive": true, "score": 100}

o    B) {'isActive': true, 'score': 100}

o    C) {"isActive": "true", "score": "100"}

o    D) { isActive: true, score: 100 }

o    Answer: A
Explanation: JSON requires double-quoted keys and values that match JavaScript types without modification.

72.        When using JSON.parse with a reviver function, what does the reviver function do?

o    A) It replaces all values with new values

o    B) It selectively transforms parsed values

o    C) It stops parsing JSON if an error is found

o    D) It adds indentation to the output

o    Answer: B
Explanation: A reviver function is used to transform values as they are parsed.

73.        Which of the following data types is not supported directly in JSON?

o    A) String

o    B) Array

o    C) Date

o    D) Boolean

o    Answer: C
Explanation: JSON does not support Date objects directly; dates are typically serialized as strings.

74.        What would JSON.parse('[1, 2, 3]') output?

o    A) "[1, 2, 3]"

o    B) [1, 2, 3]

o    C) { 1: 1, 2: 2, 3: 3 }

o    D) [ "1", "2", "3" ]

o    Answer: B
Explanation: JSON.parse correctly returns an array of numbers as [1, 2, 3].

75.        What happens if you try to parse invalid JSON with JSON.parse()?

o    A) It returns null

o    B) It returns undefined

o    C) It throws an error

o    D) It returns an empty object

o    Answer: C
Explanation: JSON.parse() throws a SyntaxError if the JSON is invalid.

76.        How can you handle circular references when using JSON.stringify()?

o    A) JSON.stringify handles circular references automatically

o    B) By passing a replacer function to JSON.stringify

o    C) Circular references are not possible in JSON

o    D) By passing a reviver function to JSON.parse

o    Answer: B
Explanation: A replacer function can be used to handle circular references manually.

77.        Which of the following is an example of invalid JSON?

o    A) { "name": "John", "age": 30 }

o    B) [ "apple", "banana", "cherry" ]

o    C) { "isAdmin": true, "score": undefined }

o    D) { "username": null }

o    Answer: C
Explanation: JSON does not support undefined as a value.

78.        Which format is typically used to pass data between a server and client in web development?

o    A) HTML

o    B) XML

o    C) JSON

o    D) SQL

o    Answer: C
Explanation: JSON is commonly used for data interchange in web applications.

79.        What is the output of JSON.stringify({ a: 'Hello', b: 'World' }, null, "\t")?

o    A) { "a": "Hello", "b": "World" }

o    B) "{ "a": "Hello", "b": "World" }"

o    C) { "a": "Hello", "b": "World" }

o    D) Error

o    Answer: C
Explanation: Using \t as the third parameter provides tab-based indentation.

80.        How are JavaScript null values represented in JSON?

o    A) null

o    B) NULL

o    C) undefined

o    D) "null"

o    Answer: A
Explanation: JSON represents null values as null without quotes.


These questions cover more scenarios and advanced aspects of JSON handling. Let me know if you'd like even further exploration on any sub-topic!

4o

You said:

yes

ChatGPT said:

ChatGPT

Here’s an additional set of advanced and nuanced multiple-choice questions about JSON, focusing on practical usage and optimization.


Advanced JSON Topics

81.        Which of the following is the correct way to represent an object with a property that holds an array in JSON?

o    A) { "data": [1, 2, 3] }

o    B) { "data": 1, 2, 3 }

o    C) { "data": {"1": 1, "2": 2, "3": 3} }

o    D) { "data": (1, 2, 3) }

o    Answer: A
Explanation: An array in JSON is correctly represented with square brackets, and it is a valid key-value pair.

82.        Which of the following data types can be stored as values in a JSON object?

o    A) Functions

o    B) undefined

o    C) Arrays

o    D) Symbols

o    Answer: C
Explanation: Functions, undefined, and Symbols are not valid JSON data types. JSON supports strings, numbers, arrays, booleans, and null.

83.        What is the purpose of a replacer function in JSON.stringify()?

o    A) To filter out specific keys before serialization

o    B) To transform the values during parsing

o    C) To format the output with indentation

o    D) To prevent circular references in the object

o    Answer: A
Explanation: The replacer function allows you to filter and modify properties before serializing the object to JSON.

84.        Which statement about JSON.parse() is true?

o    A) It can be used to create an object from a string representation of a JavaScript function.

o    B) It can convert valid JSON strings into JavaScript objects.

o    C) It can throw a TypeError if the input string is valid.

o    D) It directly executes JavaScript code embedded in the JSON string.

o    Answer: B
Explanation: JSON.parse() can only parse valid JSON strings and convert them into JavaScript objects. If the string is invalid, it throws a SyntaxError.

85.        Which of the following is a valid JSON string representation of the object { foo: "bar", x: 42 }?

o    A) { foo: "bar", x: 42 }

o    B) "{ "foo": "bar", "x": 42 }"

o    C) {"foo": "bar", "x": 42}

o    D) ("foo": "bar", "x": 42)

o    Answer: C
Explanation: In JSON, keys must be enclosed in double quotes.

86.        How can you prevent circular references when stringifying a JSON object?

o    A) Use JSON.stringify() with the replacer function

o    B) Use JSON.parse() with a custom reviver function

o    C) Use JSON.stringify() with a space parameter

o    D) JSON does not support circular references

o    Answer: A
Explanation: By providing a replacer function, you can modify or filter out circular references before stringifying the object.

87.        Which of the following data structures can be stored in a JSON array?

o    A) Only strings and numbers

o    B) Objects and arrays

o    C) Only primitive values

o    D) Only numbers

o    Answer: B
Explanation: JSON arrays can hold any data type, including numbers, strings, objects, and other arrays.

88.        Which of the following is a correct JSON representation for a nested object?

o    A) { "person": { "name": "John", "age": 30 } }

o    B) { person: { "name": "John", "age": 30 } }

o    C) { "person": "John", "name": "John", "age": 30 }

o    D) { { "name": "John", "age": 30 } }

o    Answer: A
Explanation: Nested objects are represented by curly braces and should follow the standard key-value pair format.

89.        Which JSON function would you use to convert a string like '{ "a": 1, "b": 2 }' into a JavaScript object?

o    A) JSON.stringify()

o    B) JSON.parse()

o    C) JSON.convert()

o    D) JSON.objectify()

o    Answer: B
Explanation: JSON.parse() is used to convert a valid JSON string into a JavaScript object.

90.        Which of the following is NOT a valid property type in a JSON object?

o    A) Array

o    B) Boolean

o    C) Date

o    D) Null

o    Answer: C
Explanation: JSON does not support the Date object directly. Dates are usually represented as strings in JSON.

91.        What is the result of calling JSON.parse('{ "a": "true" }') in JavaScript?

o    A) { "a": true }

o    B) { "a": "true" }

o    C) SyntaxError

o    D) { "a": true } (Boolean)

o    Answer: B
Explanation: The string "true" is a string, not a boolean value. JSON does not automatically convert string values to boolean values.

92.        Which JavaScript method allows you to safely retrieve JSON from a potentially invalid string?

o    A) JSON.tryParse()

o    B) JSON.safeParse()

o    C) JSON.parse() with try-catch

o    D) JSON.parse()

o    Answer: C
Explanation: Using JSON.parse() inside a try-catch block allows you to handle invalid JSON strings gracefully.

93.        What will be the output of JSON.stringify([1, 'apple', { key: 'value' }, [1, 2]])?

o    A) [1, "apple", {"key": "value"}, [1, 2]]

o    B) [1, "apple", {"key": value}, [1, 2]]

o    C) [1, "apple", {"key": "value"}, "1,2"]

o    D) "[1, 'apple', {key: 'value'}, [1, 2]]"

o    Answer: A
Explanation: Nested arrays and objects are properly serialized in JSON.

94.        Which function is used to serialize JavaScript objects into JSON format?

o    A) JSON.stringify()

o    B) JSON.serialize()

o    C) JSON.encode()

o    D) JSON.serializeObject()

o    Answer: A
Explanation: JSON.stringify() converts JavaScript objects into JSON strings.

95.        What is the result of the following code snippet?

javascript

Copy code

const data = { x: 10, y: 20 };

const result = JSON.stringify(data);

console.log(result);

o    A) {"x":10,"y":20}

o    B) { "x": 10, y: 20 }

o    C) 10,20

o    D) "[10, 20]"

o    Answer: A
Explanation: JSON.stringify() converts the object into a valid JSON string with proper formatting.

96.        If a Date object is serialized using JSON.stringify(), what will happen?

o    A) The Date object will be converted into a string.

o    B) It will throw a TypeError.

o    C) The Date object will be omitted.

o    D) It will remain unchanged.

o    Answer: A
Explanation: Date objects are converted to strings in ISO 8601 format.

97.        Which JSON method can be used to parse strings containing JSON data that include specific property transformations?

o    A) JSON.parse() with replacer

o    B) JSON.parse() with reviver

o    C) JSON.transform()

o    D) JSON.parse() with filter

o    Answer: B
Explanation: The reviver function is used in JSON.parse() to transform properties during parsing.

98.        In JSON, if a property’s value is undefined, what will happen when stringifying the object?

o    A) undefined will appear as a value in the resulting string

o    B) undefined will be converted to null

o    C) undefined will be omitted from the final JSON string

o    D) It will throw an error

o    Answer: C
Explanation: When stringifying an object, properties with the value undefined are omitted from the resulting JSON string.

99.        Which method in JavaScript can be used to make a deep copy of an object while handling its JSON serialization and deserialization?

o    A) JSON.clone()

o    B) Object.copy()

o    C) JSON.parse(JSON.stringify(object))

o    D) Object.deepClone()

o    Answer: C
Explanation: This technique ensures a deep copy by serializing and deserializing the object.

100. What is the most efficient way to deal with large JSON files in JavaScript?
- A) Load the entire file at once and process it
- B) Stream the file and process chunks progressively
- C) Parse the file immediately after loading it
- D) Use a worker thread for parsing
Answer: B
Explanation: Streaming the file allows for more efficient processing, especially for large files that may not fit into memory

Tags

Post a Comment

0Comments

Post a Comment (0)