MCQs on JSON and Local Storage | JavaScript Intermediate

This chapter dives into working with JSON data and local storage in web development. You’ll learn about JSON parsing, storing and retrieving data, and clearing the local storage for efficient data management in the browser.


JSON Format and Parsing (JSON.parse, JSON.stringify)

  1. Which method is used to convert a JavaScript object to a JSON string?
    • a) JSON.parse()
    • b) JSON.stringify()
    • c) JSON.convert()
    • d) JSON.toString()
  2. How do you convert a JSON string back into a JavaScript object?
    • a) JSON.parse()
    • b) JSON.stringify()
    • c) JSON.object()
    • d) JSON.convert()
  3. Which of the following is true about JSON format?
    • a) JSON keys must be in single quotes
    • b) JSON data can include functions
    • c) JSON keys must be in double quotes
    • d) JSON does not support arrays
  4. What type of data can be stored in a JSON string?
    • a) Strings and numbers only
    • b) Objects, arrays, strings, numbers, booleans, null
    • c) Only objects
    • d) Only arrays
  5. What does the JSON.stringify() method do in JavaScript?
    • a) Converts an object to JSON format
    • b) Parses a JSON string into an object
    • c) Converts an array into an object
    • d) Converts a string into an object
  6. When using JSON.parse(), what happens if the input is not valid JSON?
    • a) It returns null
    • b) It throws an error
    • c) It ignores the invalid input
    • d) It returns an empty object
  7. Which of the following types cannot be converted to JSON format?
    • a) Function
    • b) Array
    • c) Object
    • d) String
  8. What is the output of JSON.stringify([1, 2, 3]) in JavaScript?
    • a) [1, 2, 3]
    • b) “1, 2, 3”
    • c) ‘[1, 2, 3]’
    • d) {“1”: 2, “2”: 3}
  9. Which method is used to parse a JSON string into a JavaScript object?
    • a) JSON.parse()
    • b) JSON.stringify()
    • c) JSON.convert()
    • d) JSON.toObject()
  10. What happens when you call JSON.stringify() on an object with functions inside it?
    • a) The functions are included in the output
    • b) The functions are excluded from the output
    • c) It throws an error
    • d) It converts functions into strings

Storing Data in Local Storage

  1. Which web storage API is used to store data persistently across browser sessions?
    • a) Session Storage
    • b) Local Storage
    • c) Cookie Storage
    • d) Web Storage API
  2. What type of data can be stored in Local Storage?
    • a) Only strings
    • b) Objects and arrays
    • c) Strings and numbers
    • d) Only primitive values
  3. What is the maximum size limit for data that can be stored in Local Storage (in most browsers)?
    • a) 1MB
    • b) 5MB
    • c) 10MB
    • d) 50MB
  4. Which method is used to store data in Local Storage?
    • a) localStorage.setItem()
    • b) localStorage.getItem()
    • c) localStorage.removeItem()
    • d) localStorage.clear()
  5. How do you store a string value in Local Storage using JavaScript?
    • a) localStorage.storeItem(‘key’, ‘value’)
    • b) localStorage.addItem(‘key’, ‘value’)
    • c) localStorage.setItem(‘key’, ‘value’)
    • d) localStorage.set(‘key’, ‘value’)
  6. Can you store objects directly in Local Storage?
    • a) Yes, directly as objects
    • b) Yes, but you must first serialize them into strings
    • c) No, only arrays can be stored
    • d) No, Local Storage only supports numbers
  7. What happens if you try to store a new key-value pair in Local Storage with an existing key?
    • a) It overwrites the old value
    • b) It throws an error
    • c) It appends the new value to the old value
    • d) It ignores the new value
  8. Which method would you use to check if a particular item exists in Local Storage?
    • a) localStorage.hasItem()
    • b) localStorage.getItem()
    • c) localStorage.containsItem()
    • d) localStorage.keyExists()
  9. Which of the following can be used as a key when storing data in Local Storage?
    • a) Object
    • b) Array
    • c) String
    • d) Number
  10. What happens if Local Storage exceeds its storage limit?
    • a) It throws an error
    • b) It ignores the new data
    • c) It starts deleting old data
    • d) It automatically compresses the data

Retrieving Data from Local Storage

  1. How do you retrieve a value from Local Storage?
    • a) localStorage.getItem()
    • b) localStorage.get()
    • c) localStorage.retrieveItem()
    • d) localStorage.fetchItem()
  2. What does the localStorage.getItem() method return if the key does not exist?
    • a) null
    • b) undefined
    • c) “undefined”
    • d) An empty string
  3. How would you retrieve an item from Local Storage using a key “userData”?
    • a) localStorage.getItem(“userData”)
    • b) localStorage[“userData”]
    • c) localStorage.retrieve(“userData”)
    • d) localStorage.get(“userData”)
  4. Which of the following is returned when you retrieve an item from Local Storage?
    • a) An object
    • b) A string
    • c) A JSON array
    • d) A function
  5. After retrieving a JSON string from Local Storage, what must you do to convert it back to a JavaScript object?
    • a) Use JSON.parse()
    • b) Use JSON.stringify()
    • c) Use Object.create()
    • d) Nothing, it’s already an object
  6. What will happen if you call JSON.parse() on an invalid JSON string retrieved from Local Storage?
    • a) It throws a SyntaxError
    • b) It returns null
    • c) It returns an empty object
    • d) It returns undefined
  7. Which method would you use to check if a key exists in Local Storage before retrieving the value?
    • a) localStorage.keyExists()
    • b) localStorage.containsKey()
    • c) localStorage.getItem()
    • d) localStorage.hasOwnProperty()
  8. How can you retrieve all keys stored in Local Storage?
    • a) localStorage.getAllKeys()
    • b) localStorage.key(index)
    • c) localStorage.getKeys()
    • d) localStorage.keys()
  9. What is the purpose of the localStorage.length property?
    • a) It gives the number of items stored in Local Storage
    • b) It gives the size of the Local Storage data in bytes
    • c) It gives the first key stored in Local Storage
    • d) It returns a list of all keys in Local Storage
  10. How can you retrieve all key-value pairs stored in Local Storage in a loop?
    • a) Use localStorage.key() and localStorage.getItem()
    • b) Use a forEach() loop
    • c) Use Object.keys(localStorage)
    • d) Use localStorage.items()

Answers

QnoAnswer
1b) JSON.stringify()
2a) JSON.parse()
3c) JSON keys must be in double quotes
4b) Objects, arrays, strings, numbers, booleans, null
5a) Converts an object to JSON format
6b) It throws an error
7a) Function
8c) ‘[1, 2, 3]’
9a) JSON.parse()
10b) The functions are excluded from the output
11b) Local Storage
12a) Only strings
13b) 5MB
14a) localStorage.setItem()
15c) localStorage.setItem(‘key’, ‘value’)
16b) Yes, but you must first serialize them into strings
17a) It overwrites the old value
18b) localStorage.getItem()
19c) String
20a) It throws an error
21a) localStorage.getItem()
22a) null
23a) localStorage.getItem(“userData”)
24b) A string
25a) Use JSON.parse()
26a) It throws a SyntaxError
27d) localStorage.hasOwnProperty()
28b) localStorage.key(index)
29a) It gives the number of items stored in Local Storage
30a) Use localStorage.key() and localStorage.getItem()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top