MCQs Of Day 27: JavaScript with JSON
MCQ 1:
What does JSON stand for?
- A)
JavaScript Object Notation
- B)
Java Syntax Object Notation
- C)
Java Simple Object Notation
- D)
JavaScript Object Network
Answer: A)
JavaScript Object Notation
Explanation: JSON is a lightweight data-interchange format that is easy
for humans to read and write and easy for machines to parse and generate.
MCQ 2:
Which of the following is a valid JSON format?
- A) {name:
"John", age: 30}
- B) {
"name": "John", age: 30 }
- C) {
"name": "John", "age": 30 }
- D) {
"name": "John", "30": age }
Answer: C) {
"name": "John", "age": 30 }
Explanation: In valid JSON, property names must be enclosed in double
quotes, and the value can be of various data types.
MCQ 3:
Which JavaScript method is used to parse JSON strings into JavaScript objects?
- A) JSON.parse()
- B) JSON.stringify()
- C) JSON.objectify()
- D) JSON.decode()
Answer: A) JSON.parse()
Explanation: JSON.parse() is used to convert a JSON string into a
JavaScript object.
MCQ 4:
Which method is used to convert a JavaScript object into a JSON string?
- A) JSON.toString()
- B) JSON.parse()
- C) JSON.stringify()
- D) JSON.convert()
Answer: C) JSON.stringify()
Explanation: JSON.stringify() is used to convert a JavaScript object
into a JSON string.
MCQ 5:
What does the JSON.parse() function return?
- A) A
JavaScript string
- B) A
JavaScript array
- C) A
JavaScript object
- D) A
JSON string
Answer: C) A
JavaScript object
Explanation: JSON.parse() converts a JSON string into a JavaScript
object.
MCQ 6:
Which of the following is NOT a valid JSON data type?
- A)
String
- B)
Number
- C)
Undefined
- D)
Boolean
Answer: C)
Undefined
Explanation: JSON supports only string, number, object, array, boolean,
and null types. undefined is not supported.
MCQ 7:
What is the output of JSON.stringify({ "name": "John",
"age": 30 })?
- A) {
"name": "John", "age": 30 }
- B) "{"name":"John","age":30}"
- C) "{
name: 'John', age: 30 }"
- D) {"name":"John","age":30}
Answer: D) {"name":"John","age":30}
Explanation: JSON.stringify() converts an object into a JSON-formatted
string.
MCQ 8:
What does the JSON.stringify() method do when it encounters a function in an
object?
- A)
Converts it to null
- B)
Ignores the function
- C)
Converts the function to a string
- D)
Throws an error
Answer: B)
Ignores the function
Explanation: JSON.stringify() ignores functions in objects and does not
serialize them.
MCQ 9:
Which method in JavaScript is used to check if a given value is a valid JSON?
- A) JSON.isValid()
- B) JSON.parse()
- C) JSON.validate()
- D) JSON.test()
Answer: B) JSON.parse()
Explanation: JSON.parse() will throw an error if the string is not valid
JSON, which can be used to test if a string is valid JSON.
MCQ 10:
How can you access the name property from the following JSON object?
json
Copy code
{
"name": "Alice",
"age": 25,
"city": "London"
}
- A) json.name
- B) json["name"]
- C) json.name()
- D)
Both A and B
Answer: D) Both
A and B
Explanation: You can access properties of a JSON object using either dot
notation (json.name) or bracket notation (json["name"]).
MCQ 11:
What will be the result of JSON.stringify() on a JavaScript object containing a
circular reference?
- A)
Throws an error
- B)
Returns the object with undefined for circular references
- C)
Converts the object to a string
- D)
Returns null
Answer: A)
Throws an error
Explanation: JSON.stringify() will throw a TypeError when trying to
serialize an object with circular references.
MCQ 12:
What is the output of JSON.parse('{"age": 30}')?
- A) {
age: 30 }
- B) {"age":
30}
- C) undefined
- D) null
Answer: B) {"age":
30}
Explanation: The output will be a JavaScript object with the age property
set to 30.
MCQ 13:
Which method is used to check if a property exists in a JSON object?
- A) Object.exists()
- B) in
operator
- C) Object.hasOwnProperty()
- D)
Both B and C
Answer: D) Both
B and C
Explanation: The in operator and Object.hasOwnProperty() are commonly
used to check if a property exists in an object.
MCQ 14:
What will JSON.parse('{"id": 101, "name":
"John"}') return?
- A)
An array
- B) A
string
- C) A
JavaScript object
- D) A
boolean
Answer: C) A
JavaScript object
Explanation: JSON.parse() converts the given JSON string into a
JavaScript object.
MCQ 15:
Which of the following is a valid JSON array?
- A) ["apple",
"banana", "cherry"]
- B) {
"fruits": ["apple", "banana",
"cherry"] }
- C) ["apple",
"banana", {"fruit": "cherry"}]
- D)
All of the above
Answer: D) All
of the above
Explanation: All the options are valid JSON arrays or objects that
contain arrays.
MCQ 16:
How do you convert a JSON string to an object in JavaScript?
- A) JSON.parse()
- B) JSON.objectify()
- C) JSON.stringify()
- D) JSON.convert()
Answer: A) JSON.parse()
Explanation: JSON.parse() is the correct method to convert a JSON string
into a JavaScript object.
MCQ 17:
Which of the following is NOT allowed in a valid JSON?
- A)
Double-quoted keys
- B)
Single-quoted strings
- C)
Boolean values
- D)
Number values
Answer: B)
Single-quoted strings
Explanation: JSON strings must be enclosed in double quotes, not single
quotes.
MCQ 18:
What does JSON.stringify() return when applied to a JavaScript object?
- A) A
JSON object
- B) A
JSON string
- C) A
JavaScript object
- D) A
function
Answer: B) A
JSON string
Explanation: JSON.stringify() converts a JavaScript object into a
JSON-formatted string.
MCQ 19:
What is the purpose of JSON.parse() in JavaScript?
- A)
Convert a JavaScript object to JSON string
- B)
Convert a JSON string into a JavaScript object
- C)
Create a JSON object from an array
- D)
None of the above
Answer: B)
Convert a JSON string into a JavaScript object
Explanation: JSON.parse() is used to convert a JSON string into a
JavaScript object.
MCQ 20:
What is the output of JSON.stringify([1, 2, 3])?
- A) ["1",
"2", "3"]
- B) [1,
2, 3]
- C) "{1,2,3}"
- D) "[1,2,3]"
Answer: D) "[1,2,3]"
Explanation: JSON.stringify() converts an array into a string
representation of that array.
MCQ 21:
How would you access a nested value in a JSON object?
- A) json.name[0]
- B) json['name']['value']
- C) json['name'][0]
- D) json.name[1]
Answer: B) json['name']['value']
Explanation: To access nested properties, you can use dot notation or
bracket notation for each level of the object.
MCQ 22:
How would you remove a property from a JavaScript object?
- A) delete
json.propertyName
- B) json.remove('propertyName')
- C) json.removeProperty('propertyName')
- D) delete
propertyName from json
Answer: A) delete
json.propertyName
Explanation: You can use the delete operator to remove properties from
an object.
MCQ 23:
Which of the following is a valid JSON string?
- A) "Hello,
World!"
- B) Hello
World
- C) {Hello,
World}
- D) null
Answer: A) "Hello,
World!"
Explanation: A valid JSON string must be enclosed in double quotes.
MCQ 24:
What is the correct way to define a JSON object with multiple properties?
- A) {"name"
= "Alice", "age" = 25}
- B) {"name":
"Alice", "age": 25}
- C) {"name":
"Alice", "age": 25}
- D) {"name":
Alice, age: 25}
Answer: B) {"name":
"Alice", "age": 25}
Explanation: JSON objects must use the colon (:) to separate keys from
values, and keys must be enclosed in double quotes.
MCQ 25:
In JavaScript, how can you check if a variable contains a valid JSON string?
- A)
Use JSON.parse() inside a try-catch block
- B)
Use JSON.isValid()
- C)
Use JSON.check()
- D)
Use typeof operator
Answer: A) Use JSON.parse()
inside a try-catch block
Explanation: You can use JSON.parse() within a try-catch block to test
whether a string is valid JSON.
You can continue creating
additional MCQs following this pattern, covering various aspects of JSON and
JavaScript, such as error handling, complex data structures, performance
optimization, and practical uses of JSON in applications.
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are more MCQs on JavaScript
and JSON with detailed explanations:
MCQ 26:
What does the JSON.parse() method do when it encounters invalid JSON?
- A)
Returns null
- B)
Returns undefined
- C)
Throws a SyntaxError
- D)
Ignores the invalid data
Answer: C)
Throws a SyntaxError
Explanation: If the JSON string is not valid, JSON.parse() will throw a SyntaxError.
MCQ 27:
What does JSON.stringify() return when it is called on a null value?
- A) "null"
- B) null
- C) undefined
- D) {}
Answer: A) "null"
Explanation: JSON.stringify() converts null into the string "null".
MCQ 28:
Which of the following is a correct way to send JSON data from a server to a
client?
- A) JSON.stringify()
to convert the data to a string and send it over HTTP
- B) JSON.parse()
to convert the data into an object and send it over HTTP
- C)
Directly sending a JavaScript object via HTTP
- D)
Sending a JavaScript array as the data
Answer: A) JSON.stringify()
to convert the data to a string and send it over HTTP
Explanation: JSON data is typically sent as a string (using JSON.stringify())
in HTTP requests.
MCQ 29:
How can you parse a JSON string that contains an array?
- A) JSON.parse("[1,
2, 3]")
- B) JSON.parse("{1,
2, 3}")
- C) JSON.parse([1,
2, 3])
- D) JSON.parse(1,
2, 3)
Answer: A) JSON.parse("[1,
2, 3]")
Explanation: You can parse a JSON string that represents an array by
using JSON.parse("[1, 2, 3]").
MCQ 30:
Which of the following is NOT a valid JSON structure?
- A) {"name":
"Alice", "age": 25}
- B) [1,
2, 3]
- C) {"name":
"Alice", "age": "25"}
- D) {"name":
"Alice", age: 25}
Answer: D) {"name":
"Alice", age: 25}
Explanation: JSON property names must be enclosed in double quotes. In
option D, age is not quoted, making it invalid.
MCQ 31:
How can you check if a key exists in a JSON object?
- A) key
in json
- B) json.hasOwnProperty(key)
- C) json.contains(key)
- D)
Both A and B
Answer: D) Both
A and B
Explanation: Both the in operator and Object.hasOwnProperty() can be
used to check if a key exists in a JSON object.
MCQ 32:
What does JSON.stringify() return when applied to a JavaScript array?
- A) A
JSON object
- B) A
JSON string representing the array
- C) A
JavaScript array
- D) A
function
Answer: B) A
JSON string representing the array
Explanation: JSON.stringify() converts a JavaScript array into a JSON
string.
MCQ 33:
Which of the following is a valid way to handle JSON data in JavaScript?
- A)
Parse JSON data into a JavaScript object using JSON.parse()
- B)
Convert a JavaScript object into JSON using JSON.stringify()
- C)
Use JSON.parse() to retrieve JSON from a server
- D)
All of the above
Answer: D) All
of the above
Explanation: All the listed options are valid ways to handle JSON data
in JavaScript.
MCQ 34:
Which of the following JSON data types can be nested within another JSON
object?
- A)
String
- B)
Boolean
- C)
Object
- D)
All of the above
Answer: C)
Object
Explanation: JSON objects can contain nested objects, arrays, strings,
numbers, and booleans.
MCQ 35:
How can you access a property name in the following JSON object?
json
Copy code
{
"person": {
"name": "John",
"age": 30
}
}
- A) json.person.name
- B) json["person"].name
- C) json.person["name"]
- D)
All of the above
Answer: D) All
of the above
Explanation: You can access the name property using any of the three
syntaxes: dot notation or bracket notation for both levels of nesting.
MCQ 36: What
does the JSON.parse() method return when it is applied to an empty JSON string
("")?
- A) null
- B) undefined
- C) {}
(empty object)
- D) []
(empty array)
Answer: C) {}
(empty object)
Explanation: An empty JSON string ("") is not valid JSON.
However, JSON.parse("") will throw an error.
MCQ 37:
In a valid JSON string, what must wrap the data?
- A)
Parentheses ()
- B)
Square brackets []
- C)
Curly braces {}
- D)
Angle brackets <>
Answer: C) Curly
braces {}
Explanation: JSON data is wrapped in curly braces for objects and square
brackets for arrays.
MCQ 38:
Which of the following JSON strings is valid?
- A) {"age":30,
"name":"John"}
- B) {"age":30,
name: "John"}
- C) ["age":30,
"name":"John"]
- D) {"age":30
"name":"John"}
Answer: A) {"age":30,
"name":"John"}
Explanation: JSON keys and values must be properly formatted, with keys
in double quotes and key-value pairs separated by commas.
MCQ 39:
Can a JavaScript array be stored as a valid JSON?
- A)
Yes
- B)
No
Answer: A) Yes
Explanation: JSON arrays are valid and are represented as arrays in JSON
format, just like in JavaScript.
MCQ 40:
What does JSON.parse('{"name": "John"}') return?
- A) "name":
"John"
- B) {
"name": "John" }
- C) "John"
- D) null
Answer: B) {
"name": "John" }
Explanation: JSON.parse() returns a JavaScript object when it parses a
valid JSON string.
MCQ 41:
If you have a JSON string that contains a property name, which of the following
will parse it into a JavaScript object?
- A) JSON.parse(name)
- B) JSON.parse('{"name":
"Alice"}')
- C) JSON.parse("name")
- D) JSON.parse([name])
Answer: B) JSON.parse('{"name":
"Alice"}')
Explanation: The correct syntax to parse a JSON string into a JavaScript
object is to ensure the string is properly formatted as JSON.
MCQ 42:
How would you handle errors when parsing a potentially invalid JSON string?
- A)
Wrap the JSON.parse() in a try-catch block
- B)
Use a conditional statement to check for errors
- C)
Use the JSON.validate() method
- D)
Both A and B
Answer: A) Wrap
the JSON.parse() in a try-catch block
Explanation: Wrapping JSON.parse() in a try-catch block is the best way
to handle potential errors when parsing JSON.
MCQ 43:
What is the correct syntax to parse a JSON string and retrieve a property age
from it?
- A) JSON.parse('{"age":
25}').age
- B) JSON.parse("{age:
25}").age
- C) JSON.parse('age:
25').age
- D) JSON.parse({"age":
25}).age
Answer: A) JSON.parse('{"age":
25}').age
Explanation: You first parse the valid JSON string into a JavaScript
object and then access the property age.
MCQ 44:
Can JSON handle functions as values?
- A)
Yes, functions can be included in JSON
- B)
No, functions cannot be included in JSON
- C)
Yes, but only if the function is serialized as a string
- D)
Functions can only be serialized as objects
Answer: B) No,
functions cannot be included in JSON
Explanation: JSON can only represent data types such as strings,
numbers, arrays, and objects. Functions cannot be directly included in JSON.
MCQ 45:
What is the output of JSON.stringify({"name":"John",
"age":30})?
- A) {
"name": "John", "age": 30 }
- B) {"name":"John","age":30}
- C) "name:
John, age: 30"
- D) ['name':'John',
'age':30]
Answer: B) {"name":"John","age":30}
Explanation: JSON.stringify() converts the object into a JSON string
with properties in double quotes, and without any spaces between keys and
values.
These MCQs help in understanding
various aspects of working with JSON in JavaScript, from parsing and
stringifying data to accessing and modifying properties within JSON objects.
4o mini
You said:
more
ChatGPT said:
ChatGPT
Here are additional MCQs on JavaScript
and JSON with detailed explanations:
MCQ 46:
What does the JSON.stringify() method do?
- A)
Converts a JavaScript object into a JSON string
- B)
Converts a JSON string into a JavaScript object
- C)
Extracts data from a JSON object
- D)
Encodes a JSON object to base64
Answer: A)
Converts a JavaScript object into a JSON string
Explanation: JSON.stringify() is used to convert a JavaScript object
into a string in JSON format.
MCQ 47:
What happens when JSON.parse() is called on a non-JSON string, such as "Hello,
World!"?
- A)
It throws a SyntaxError
- B)
It returns "Hello, World!" as a string
- C)
It returns an empty object
- D)
It returns null
Answer: A) It
throws a SyntaxError
Explanation: When JSON.parse() is used on a non-JSON string (i.e., a
string that doesn't follow JSON syntax), it throws a SyntaxError.
MCQ 48:
What is the result of this operation: JSON.stringify([1, 2, 3])?
- A) "1,2,3"
- B) [1,
2, 3]
- C) "[1,2,3]"
- D) [1,2,3]
(without quotes)
Answer: C) "
[1,2,3] "
Explanation: JSON.stringify() converts an array into a JSON string with
elements separated by commas and wrapped in square brackets.
MCQ 49:
Which of the following JavaScript methods is used to convert a JSON string into
a JavaScript object?
- A) JSON.parse()
- B) JSON.stringify()
- C) JSON.decode()
- D) JSON.convert()
Answer: A) JSON.parse()
Explanation: JSON.parse() is used to convert a valid JSON string into a
corresponding JavaScript object.
MCQ 50:
What does JSON.stringify() do when it encounters a null value in an object?
- A)
It removes the null value from the object
- B)
It converts the null value into an empty string
- C)
It includes null as part of the JSON string
- D)
It throws an error
Answer: C) It
includes null as part of the JSON string
Explanation: JSON.stringify() includes null values in the resulting JSON
string.
MCQ 51:
Which of the following data types is NOT supported by JSON?
- A)
Boolean
- B)
Number
- C)
Function
- D)
String
Answer: C)
Function
Explanation: JSON supports boolean values, numbers, strings, arrays, and
objects, but it does not support functions.
MCQ 52:
How do you convert a JSON string into a JavaScript object and access a property
of that object?
- A) JSON.stringify()
and use dot notation
- B) JSON.parse()
and use dot notation
- C) JSON.parse()
and use bracket notation
- D)
Both B and C
Answer: D) Both
B and C
Explanation: After parsing a JSON string with JSON.parse(), you can
access properties using either dot notation or bracket notation.
MCQ 53:
Which method can be used to check if a given string is a valid JSON?
- A) JSON.isValid()
- B) JSON.test()
- C) JSON.parse()
- D)
Try-catch block
Answer: D)
Try-catch block
Explanation: A try-catch block is the best way to check if a string is
valid JSON. If parsing the string fails, it will throw an error.
MCQ 54:
Which of the following is a valid JSON string?
- A) {name:
"John"}
- B) {"name":
"John"}
- C) ["name":
"John"]
- D) [name:
"John"]
Answer: B) {"name":
"John"}
Explanation: In JSON, keys must be enclosed in double quotes, as shown
in option B.
MCQ 55:
What is the output of JSON.parse('{}')?
- A) {}
(an empty object)
- B) []
(an empty array)
- C) null
- D) undefined
Answer: A) {}
(an empty object)
Explanation: JSON.parse('{}') returns an empty object because the string
represents a valid empty JSON object.
MCQ 56:
What happens if you use JSON.parse() on an incorrectly formatted JSON string,
like {"name": "John"?
- A)
It returns null
- B)
It returns undefined
- C)
It throws a SyntaxError
- D)
It returns an empty object
Answer: C) It
throws a SyntaxError
Explanation: A missing closing brace in the JSON string will cause JSON.parse()
to throw a SyntaxError.
MCQ 57:
What is the correct way to create a JSON string from a JavaScript object?
- A) JSON.parse(obj)
- B) JSON.stringify(obj)
- C) JSON.convert(obj)
- D) JSON.serialize(obj)
Answer: B) JSON.stringify(obj)
Explanation: JSON.stringify() converts a JavaScript object into a
JSON-formatted string.
MCQ 58:
Which of the following is true about JSON?
- A)
JSON supports functions
- B)
JSON supports arrays
- C)
JSON supports dates
- C)
JSON supports variables
Answer: B) JSON
supports arrays
Explanation: JSON supports data types such as arrays, strings, numbers,
booleans, and objects, but not functions or variables.
MCQ 59:
How would you correctly parse the following JSON string in JavaScript? {"name":
"Alice", "age": 25}
- A) JSON.parse('{"name":
"Alice", "age": 25}')
- B) JSON.parse(name:
"Alice", age: 25)
- C) JSON.parse({name:
"Alice", age: 25})
- D) JSON.stringify('{"name":
"Alice", "age": 25}')
Answer: A) JSON.parse('{"name":
"Alice", "age": 25}')
Explanation: JSON.parse() takes a valid JSON string and converts it into
a JavaScript object.
MCQ 60:
What is the output of JSON.parse('[1, 2, 3]')?
- A) [1,
2, 3]
- B) "1,
2, 3"
- C) [1,
2, 3] as a string
- D) [1,
2, 3] as a JavaScript array
Answer: D) [1,
2, 3] as a JavaScript array
Explanation: JSON.parse() converts the JSON string into a JavaScript
array.
MCQ 61:
What will happen if you try to parse an invalid JSON string like "{name:
John}"?
- A)
It returns an empty object
- B)
It throws a SyntaxError
- C)
It returns null
- D)
It returns the string "{name: John}"
Answer: B) It
throws a SyntaxError
Explanation: The key name in "{name: John}" should be enclosed
in double quotes, so this is an invalid JSON string that throws an error.
MCQ 62:
Can JSON handle dates?
- A)
Yes, JSON can handle Date objects natively
- B)
No, JSON cannot represent Date objects
- C)
JSON can handle Date objects but only as strings
- D)
JSON can handle Date objects but only in certain environments
Answer: C) JSON
can handle Date objects but only as strings
Explanation: JSON does not natively support Date objects, but Date
objects can be converted into strings using methods like toISOString().
MCQ 63:
How would you retrieve the value of the "name" property from a parsed
JSON object?
json
Copy code
{
"name": "Alice",
"age": 25
}
- A) json.name
- B) json["name"]
- C)
Both A and B
- D)
None of the above
Answer: C) Both
A and B
Explanation: You can access the "name" property using either
dot notation (json.name) or bracket notation (json["name"]).
MCQ 64:
How do you convert a JavaScript object into JSON format for sending to a
server?
- A) JSON.parse()
- B) JSON.stringify()
- C) JSON.serialize()
- D) JSON.decode()
Answer: B) JSON.stringify()
Explanation: JSON.stringify() is used to convert a JavaScript object
into a JSON string before sending it to a server.
MCQ 65:
What would be the result of JSON.stringify({a: 1, b: undefined})?
- A) {"a":1,"b":null}
- B) {"a":1}
- C) {"a":1,
"b": undefined}
- D) {"a":1,
"b":""}
Answer: B) {"a":1}
Explanation: When JSON.stringify() encounters undefined, it omits the
key-value pair from the resulting JSON string.
These additional MCQs help
reinforce concepts related to working with JSON data in JavaScript, covering
methods such as JSON.stringify() and JSON.parse(), as well as understanding
JSON formatting and limitations.
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are more MCQs on JavaScript
and JSON with explanations:
MCQ 66:
What will happen if you try to parse the following string using JSON.parse('{"name":
"John", "age": }')?
- A)
It will return an object with "name": "John" and "age":
null
- B) It
will throw a SyntaxError
- C)
It will return an empty object
- D)
It will return the string {"name": "John",
"age": }
Answer: B) It
will throw a SyntaxError
Explanation: The string {"name": "John",
"age": } is invalid JSON because the value of "age" is missing,
which results in a syntax error.
MCQ 67:
Which of the following is a correct JSON array format?
- A) {1,
2, 3}
- B) ["1",
"2", "3"]
- C) [1,
2, "3"]
- D)
All of the above
Answer: D) All
of the above
Explanation: All the above options represent valid JSON arrays with
different types of data, such as strings, numbers, and mixed data types.
MCQ 68:
Which of the following JSON data types is NOT allowed?
- A)
Object
- B)
Array
- C)
Function
- D)
String
Answer: C)
Function
Explanation: JSON does not support functions as data types. JSON
supports objects, arrays, strings, numbers, and booleans.
MCQ 69:
What is the purpose of JSON.parse() in JavaScript?
- A)
Converts a JavaScript object to a string
- B)
Converts a JavaScript object to an array
- C)
Converts a JSON string into a JavaScript object
- D)
Converts a JavaScript string into a number
Answer: C)
Converts a JSON string into a JavaScript object
Explanation: JSON.parse() takes a JSON string and converts it into an
equivalent JavaScript object.
MCQ 70:
Which of the following is a valid JSON object?
- A) {"key":
"value"}
- B) key:
"value"
- C) {key
= "value"}
- D) {key:
"value"};
Answer: A) {"key":
"value"}
Explanation: A valid JSON object consists of key-value pairs, where the
keys are strings enclosed in double quotes and the values can be various data
types.
MCQ 71:
If you use JSON.stringify() on an object with a circular reference, what will
happen?
- A)
It throws a TypeError
- B)
It converts the circular reference into null
- C)
It omits the circular reference and converts the object into a string
- D)
It returns the object as is
Answer: A) It
throws a TypeError
Explanation: If the object has a circular reference, JSON.stringify()
will throw a TypeError because it cannot serialize circular structures.
MCQ 72:
What will be the result of JSON.stringify({name: "Alice", age: 25,
greet: function() {}})?
- A) {"name":"Alice",
"age":25, "greet":{}}
- B) {"name":"Alice",
"age":25}
- C) {"name":"Alice",
"age":25, "greet":null}
- D) {"name":"Alice",
"age":25, "greet":"function()"}
Answer: B) {"name":"Alice",
"age":25}
Explanation: JSON.stringify() excludes functions from the resulting JSON
string, so only the properties name and age are included.
MCQ 73:
Which method is used to test if a given JSON string is valid or not?
- A) JSON.validate()
- B) JSON.isValid()
- C) JSON.parse()
- D)
None of the above
Answer: D) None
of the above
Explanation: To test whether a JSON string is valid, you need to use a
try-catch block around JSON.parse(), as it will throw an error if the string is
invalid.
MCQ 74:
What is the output of JSON.stringify([undefined, null, 1, "test"])?
- A) "[null,null,1,"test"]"
- B) "[null,null,1,"test",null]"
- C) "[null,1,"test"]"
- D) "[undefined,null,1,"test"]"
Answer: A) "[null,null,1,"test"]"
Explanation: JSON.stringify() removes undefined values and omits them
from the JSON string, but null is included.
MCQ 75:
What will JSON.parse('{"name": "John", "age":
"25"}') return?
- A) {name:
"John", age: "25"}
- B) {"name":
"John", age: 25}
- C) null
- D) "{"name":
"John", "age": "25"}"
Answer: B) {"name":
"John", age: 25}
Explanation: JSON.parse() converts a JSON string into a JavaScript
object. The string "25" will be parsed as a string, so the value of "age"
will be a string "25", not a number.
MCQ 76:
Which data type is NOT supported in JSON?
- A)
String
- B)
Array
- C)
Function
- D)
Boolean
Answer: C)
Function
Explanation: JSON does not support functions. Only basic data types such
as strings, numbers, booleans, arrays, and objects are supported.
MCQ 77:
What is the purpose of the JSON.parse() method?
- A)
To create a JSON string from an object
- B)
To convert a JSON string to a JavaScript object
- C)
To validate JSON data
- D)
To send JSON data to the server
Answer: B) To
convert a JSON string to a JavaScript object
Explanation: JSON.parse() is used to convert a valid JSON string into a
JavaScript object.
MCQ 78:
Which of the following is the correct way to send JSON data in a POST request
using fetch in JavaScript?
- A) fetch(url,
{ method: "POST", body: JSON.stringify(data) })
- B) fetch(url,
{ method: "GET", body: JSON.stringify(data) })
- C) fetch(url,
{ method: "POST", body: data })
- D) fetch(url,
{ method: "POST", data: JSON.stringify(data) })
Answer: A) fetch(url,
{ method: "POST", body: JSON.stringify(data) })
Explanation: When sending data as JSON in a POST request, use JSON.stringify(data)
to convert the data to a JSON string before sending it in the request body.
MCQ 79:
What is the result of JSON.parse('{"a": "1", "b":
2}') in JavaScript?
- A) {a:
"1", b: 2}
- B) ["a":
"1", "b": 2]
- C) {a:
"1", b: 2} (as a JavaScript object)
- D) null
Answer: C) {a:
"1", b: 2} (as a JavaScript object)
Explanation: JSON.parse() converts a JSON string into a JavaScript
object, where the key-value pairs are preserved.
MCQ 80:
How can you ensure that your JSON string is correctly formatted?
- A)
Manually check the format
- B)
Use JSON.stringify()
- C)
Use a JSON validator
- D)
Use JSON.parse()
Answer: C) Use a
JSON validator
Explanation: A JSON validator can help ensure that your JSON string is
correctly formatted before using it in your code.
MCQ 81:
What does JSON.parse('{"key": "value"}') return?
- A) A
string "key": "value"
- B) A
JavaScript object {key: "value"}
- C) A
JSON string "{key: value}"
- D)
An array ["key", "value"]
Answer: B) A
JavaScript object {key: "value"}
Explanation: JSON.parse() converts a JSON string into a JavaScript
object, with properties as key-value pairs.
MCQ 82:
How can you handle errors when parsing JSON in JavaScript?
- A)
Using the catch method
- B)
Using the try-catch block
- C)
Using a callback function
- D)
Using the validate method
Answer: B) Using
the try-catch block
Explanation: A try-catch block is used to catch and handle errors when
parsing invalid JSON strings with JSON.parse().
These additional MCQs help
reinforce your understanding of working with JSON in JavaScript, covering key
methods like JSON.parse(), JSON.stringify(), and handling errors in JSON data.
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are more MCQs on JavaScript
and JSON:
MCQ 83:
What is the result of JSON.stringify({a: undefined})?
- A) {"a":
undefined}
- B) {"a":
null}
- C) {"a":
""}
- D) {}
Answer: D) {}
Explanation: JSON.stringify() omits any properties with undefined
values, so the resulting string is an empty object.
MCQ 84:
How can you access a value in a JSON object after parsing it in JavaScript?
- A)
By using the dot notation or bracket notation
- B)
By using the get() method
- C)
By using the valueOf() method
- D)
By using the toString() method
Answer: A) By
using the dot notation or bracket notation
Explanation: Once a JSON string is parsed into an object, you can access
the properties using dot notation (e.g., obj.property) or bracket notation
(e.g., obj["property"]).
MCQ 85:
What is the main purpose of the JSON.stringify() method in JavaScript?
- A)
To convert JavaScript objects into a JSON string
- B)
To parse a JSON string into a JavaScript object
- C)
To convert a JSON string into an array
- D)
To validate JSON data
Answer: A) To
convert JavaScript objects into a JSON string
Explanation: JSON.stringify() is used to convert a JavaScript object or
array into a JSON-formatted string.
MCQ 86:
Which of the following is NOT a valid JSON data type?
- A)
String
- B)
Object
- C)
Undefined
- D)
Boolean
Answer: C)
Undefined
Explanation: undefined is not a valid JSON data type. JSON supports
strings, numbers, booleans, arrays, and objects.
MCQ 87:
What will JSON.parse('{"name": "John", "age":
30}') return?
- A) "John"
- B) {name:
"John", age: 30}
- C) A
string {"name": "John", "age": 30}
- D)
An array ["name", "John", "age", 30]
Answer: B) {name:
"John", age: 30}
Explanation: JSON.parse() converts the JSON string into a JavaScript
object with properties name and age.
MCQ 88:
Which of the following will JSON.stringify() do with NaN values?
- A)
It will replace NaN with null
- B)
It will throw an error
- C)
It will exclude the NaN value
- D)
It will convert NaN to a string "NaN"
Answer: D) It will
convert NaN to a string "NaN"
Explanation: JSON.stringify() converts NaN to the string "NaN"
during serialization.
MCQ 89:
What is the correct syntax to represent an empty JSON object?
- A) {}
- B) []
- C) null
- D) ""
Answer: A) {}
Explanation: An empty JSON object is represented by {} in JSON notation.
MCQ 90:
What will be the result of JSON.stringify([1, 2, 3, undefined])?
- A) [1,
2, 3, undefined]
- B) [1,
2, 3]
- C) [null,
null, null, null]
- D) "[1,2,3]"
Answer: B) [1,
2, 3]
Explanation: JSON.stringify() omits undefined values, so the resulting
array will not include undefined.
MCQ 91:
Which JavaScript method can be used to convert a JavaScript object into a JSON
string?
- A) JSON.stringify()
- B) JSON.parse()
- C) JSON.convert()
- D) JSON.objectify()
Answer: A) JSON.stringify()
Explanation: JSON.stringify() is used to convert a JavaScript object
into a JSON string.
MCQ 92:
How can you check if a JSON string is valid before parsing it?
- A)
Using JSON.parse() inside a try-catch block
- B)
Using the validateJSON() function
- C)
Using JSON.validate()
- D)
Using the isValidJSON() method
Answer: A) Using
JSON.parse() inside a try-catch block
Explanation: You should use JSON.parse() inside a try-catch block to
handle any errors if the JSON is not valid.
MCQ 93:
What does the replace() method in JavaScript do when working with JSON strings?
- A)
It replaces JSON objects in a string
- B)
It replaces specific values in a JSON string using regular expressions
- C)
It parses a JSON string into a JavaScript object
- D)
It converts a JavaScript object into a JSON string
Answer: B) It
replaces specific values in a JSON string using regular expressions
Explanation: replace() is a JavaScript method that can be used to search
and replace specific patterns in strings, including JSON strings.
MCQ 94:
What will JSON.parse('{"name": "John", "age":
25}') return in JavaScript?
- A) {name:
"John", age: 25}
- B) "name":
"John", "age": 25
- C) {"name":
"John", "age": "25"}
- D) ["name",
"John", "age", 25]
Answer: A) {name:
"John", age: 25}
Explanation: JSON.parse() converts a JSON string into a JavaScript
object with keys and values.
MCQ 95:
What is the result of JSON.stringify({key: 1, key: 2})?
- A) {"key":
2}
- B) {"key":
1, "key": 2}
- C) {key:
1, key: 2}
- D) {key:
"2"}
Answer: A) {"key":
2}
Explanation: JSON objects cannot have duplicate keys, so the second
key-value pair overwrites the first one.
MCQ 96:
What type of error will occur if you try to parse a malformed JSON string?
- A) TypeError
- B) SyntaxError
- C) ReferenceError
- D) RangeError
Answer: B) SyntaxError
Explanation: Malformed JSON will result in a SyntaxError when you try to
parse it using JSON.parse().
MCQ 97:
What does JSON.parse() do when given a valid JSON string?
- A)
It converts the string into a JavaScript object
- B)
It checks if the string is valid
- C)
It removes unwanted data from the string
- D)
It creates an empty object
Answer: A) It
converts the string into a JavaScript object
Explanation: JSON.parse() is used to parse a JSON string and convert it
into a corresponding JavaScript object.
MCQ 98:
How would you handle errors when parsing JSON data from an external source?
- A)
Use a try-catch block to catch parsing errors
- B)
Check the validity of the data before parsing
- C)
Log the error for debugging purposes
- D)
All of the above
Answer: D) All
of the above
Explanation: When parsing external JSON data, it's best to use a try-catch
block, check for data validity, and log any errors.
MCQ 99:
If you parse the string "{a:1,b:2}" using JSON.parse(), what will
happen?
- A)
It will return a JavaScript object
- B)
It will throw a SyntaxError because the keys are not enclosed in double
quotes
- C)
It will return a string "{a:1,b:2}"
- D)
It will return an array [a: 1, b: 2]
Answer: B) It
will throw a SyntaxError because the keys are not enclosed in double quotes
Explanation: In JSON, keys must be enclosed in double quotes. The string
"{a:1,b:2}" is invalid JSON.
MCQ 100:
What will JSON.stringify({name: "John", age: 30, greet:
function(){}}) return?
- A) {"name":"John","age":30,"greet":{}}
- B) {"name":"John","age":30}
- C) {"name":"John","age":30,"greet":"function(){}"}
- D) {}
Answer: B) {"name":"John","age":30}
Explanation: JSON.stringify() excludes functions from the output, so the
greet function will be omitted.
