MCQs Of Day 16: Local Storage and Session Storage
1. What
is the main difference between localStorage and sessionStorage?
- A)
localStorage persists data across sessions, while sessionStorage only
persists data for a single session.
- B)
sessionStorage is used to store large data, while localStorage stores
small data.
- C)
localStorage and sessionStorage have the same purpose.
- D)
localStorage stores data in cookies, while sessionStorage does not.
Answer: A)
localStorage persists data across sessions, while sessionStorage only persists
data for a single session.
Explanation: localStorage data remains even after closing the browser,
while sessionStorage is cleared when the page session ends.
2. Which
method is used to store data in localStorage?
- A)
store()
- B)
save()
- C)
setItem()
- D)
putItem()
Answer: C)
setItem()
Explanation: The setItem() method is used to store data in localStorage
or sessionStorage.
3. What
is the maximum size limit of data that can be stored in localStorage?
- A)
1MB
- B)
5MB
- C)
10MB
- D)
100MB
Answer: B) 5MB
Explanation: The typical limit for localStorage data is around 5MB per origin.
4. How
can you retrieve data from sessionStorage?
- A)
sessionStorage.getItem()
- B)
sessionStorage.get()
- C)
sessionStorage.read()
- D)
sessionStorage.fetch()
Answer: A)
sessionStorage.getItem()
Explanation: The getItem() method is used to retrieve data from
sessionStorage.
5. Which
of the following data types can be stored in localStorage and sessionStorage?
- A)
Objects
- B)
Arrays
- C)
Strings
- D)
Functions
Answer: C)
Strings
Explanation: Both localStorage and sessionStorage can only store strings.
Non-string data types must be serialized using JSON.stringify().
6. How do
you convert a JavaScript object into a string for storage in localStorage?
- A)
JSON.stringify()
- B)
JSON.convert()
- C)
Object.toString()
- D)
Stringify()
Answer: A) JSON.stringify()
Explanation: To store an object in localStorage, it needs to be
converted into a string using JSON.stringify().
7. Which
of the following methods is used to remove an item from localStorage?
- A)
remove()
- B)
deleteItem()
- C)
clearItem()
- D)
removeItem()
Answer: D)
removeItem()
Explanation: The removeItem() method is used to remove a specific item
from localStorage.
8. What
will happen if you try to store an object directly in localStorage?
- A)
It will throw an error.
- B)
It will store the object as-is.
- C)
It will be automatically converted to a string.
- D)
It will be ignored.
Answer: C) It
will be automatically converted to a string.
Explanation: Since localStorage can only store strings, non-string data
types like objects will be automatically converted to a string using the
toString() method.
9. How do
you parse a string back into an object from localStorage?
- A)
JSON.parse()
- B)
Object.parse()
- C)
JSON.deserialize()
- D)
JSON.decode()
Answer: A)
JSON.parse()
Explanation: JSON.parse() is used to convert a JSON string back into an
object.
10. How
long does data in sessionStorage persist?
- A)
Until the browser is closed
- B)
Until the session ends
- C)
Until the page is refreshed
- D)
Until the user logs out
Answer: B)
Until the session ends
Explanation: Data stored in sessionStorage lasts only for the duration
of the page session. It is cleared once the browser tab is closed.
11. What
will happen if you call localStorage.setItem('key', null)?
- A)
It will store the value as "null".
- B)
It will throw an error.
- C)
It will store an empty value.
- D)
It will store the string "undefined".
Answer: A) It
will store the value as "null".
Explanation: null will be converted to the string "null" when
stored in localStorage.
12. Which
method would you use to clear all data from localStorage?
- A)
clear()
- B)
clearItem()
- C)
clearAll()
- D)
removeAll()
Answer: A)
clear()
Explanation: The clear() method removes all items from localStorage or
sessionStorage.
13. Which
of the following is NOT a valid use case for localStorage?
- A)
Storing user preferences.
- B)
Storing user authentication tokens.
- C)
Storing large files like images or videos.
- D)
Storing session-related data.
Answer: C)
Storing large files like images or videos.
Explanation: localStorage is not suitable for storing large files, as it
has a size limit (usually around 5MB). For large data, consider using IndexedDB
or server-side storage.
14. What
is the purpose of sessionStorage?
- A)
To persist data even after closing the browser.
- B)
To store data for a specific browser session.
- C)
To share data across multiple tabs in the browser.
- D)
To store data in cookies.
Answer: B) To
store data for a specific browser session.
Explanation: sessionStorage is used to store data for the duration of
the page session, meaning the data will be lost once the browser or tab is
closed.
15. Which
of the following is the correct syntax to store a value in localStorage?
- A)
localStorage.add('key', 'value')
- B)
localStorage.set('key', 'value')
- C)
localStorage.store('key', 'value')
- D)
localStorage.setItem('key', 'value')
Answer: D)
localStorage.setItem('key', 'value')
Explanation: localStorage.setItem() is the correct method to store data
in localStorage.
16. What
happens if you try to retrieve a non-existent item from localStorage?
- A)
It returns null.
- B)
It throws an error.
- C)
It returns an empty string.
- D)
It returns undefined.
Answer: A) It
returns null.
Explanation: If the item does not exist in localStorage, getItem() will
return null.
17. Which
of the following can cause localStorage data to be lost?
- A)
Refreshing the page
- B)
Clearing browser cookies
- C)
Closing the browser tab
- D)
The storage quota is exceeded
Answer: D) The
storage quota is exceeded
Explanation: If the data exceeds the storage limit of localStorage, the
data will be lost.
18. Can
localStorage be accessed by JavaScript in a different domain?
- A)
Yes, if the domain is the same.
- B)
No, it is restricted to the origin (domain).
- C)
Yes, if the browser allows third-party cookies.
- D)
Yes, if the localStorage is shared.
Answer: B) No,
it is restricted to the origin (domain).
Explanation: localStorage is bound to the domain and cannot be accessed
from different domains.
19. Which
of the following is true about localStorage?
- A)
It can be used to store user-specific data across all devices.
- B)
It can only store strings.
- C)
It automatically expires after a certain time.
- D)
It shares data between tabs in different browsers.
Answer: B) It
can only store strings.
Explanation: localStorage stores only string data. Other data types need
to be converted to strings first.
20. How
do you convert a string stored in localStorage back into an object?
- A)
JSON.deserialize()
- B)
Object.parse()
- C)
JSON.parse()
- D)
parseJSON()
Answer: C) JSON.parse()
Explanation: JSON.parse() is used to convert a JSON-formatted string
back into a JavaScript object.
21. Which
of the following methods can be used to clear the entire sessionStorage?
- A)
sessionStorage.removeAll()
- B)
sessionStorage.delete()
- C)
sessionStorage.clear()
- D)
sessionStorage.clearAll()
Answer: C)
sessionStorage.clear()
Explanation: The clear() method clears all items stored in sessionStorage.
22. Can
sessionStorage be used to persist data across browser tabs?
- A)
Yes
- B)
No
- C)
Only with specific permissions
- D)
Yes, only in the same window
Answer: B) No
Explanation: Data stored in sessionStorage is not shared between tabs.
It is only available in the tab where it was created.
23. Which
of the following is the correct syntax for removing a single item from
localStorage?
- A)
localStorage.delete('key')
- B)
localStorage.remove('key')
- C)
localStorage.clear('key')
- D)
localStorage.removeItem('key')
Answer: D)
localStorage.removeItem('key')
Explanation: removeItem() is used to remove a single item from
localStorage.
24. How
do you store a JavaScript array in localStorage?
- A)
Use localStorage.setItem() and JSON.stringify() to convert the array to a
string.
- B)
Directly store the array using localStorage.setItem().
- C)
Convert the array into an object before storing.
- D)
Store the array as a comma-separated string.
Answer: A) Use
localStorage.setItem() and JSON.stringify() to convert the array to a string.
Explanation: Arrays need to be converted to strings using
JSON.stringify() before storing them in localStorage.
25. What
is the primary limitation of localStorage?
- A)
Limited to storing data as strings only
- B)
It is slower than cookies
- C)
It can only be accessed by server-side scripts
- D)
It has a small data storage capacity (usually 5MB per domain)
Answer: D) It
has a small data storage capacity (usually 5MB per domain)
Explanation: The storage limit of localStorage is typically 5MB per
domain, making it unsuitable for large data.
26. Which
of the following is NOT a valid operation for localStorage or sessionStorage?
- A)
getItem()
- B)
setItem()
- C)
deleteItem()
- D)
clear()
Answer: C)
deleteItem()
Explanation: The correct method to remove a single item is removeItem(),
not deleteItem().
27. How
can you store complex data (like objects or arrays) in localStorage?
- A)
Convert the data to JSON format using JSON.stringify().
- B)
Store the object directly in localStorage.
- C)
Serialize the data using localStorage.serialize().
- D)
localStorage only supports primitive data types.
Answer: A)
Convert the data to JSON format using JSON.stringify().
Explanation: JavaScript objects or arrays must be converted to a string
(using JSON.stringify()) before storing in localStorage.
28. Which
of the following is true about localStorage in relation to the browser history?
- A)
It can be used to store items in the browser's history.
- B)
It is separate from the browser history and does not affect it.
- C)
It automatically clears when navigating to a new page.
- D)
It is used to store the browser's session history.
Answer: B) It
is separate from the browser history and does not affect it.
Explanation: localStorage is used for storing data locally, while the
browser's history is used to track page navigation.
29. If a
website has a localStorage item with the key "theme", what would
localStorage.getItem('theme') return?
- A)
The value of "theme" if it exists, or null if it doesn't.
- B)
undefined
- C) A
boolean value indicating if the "theme" exists
- D)
An error message indicating "theme" is missing
Answer: A) The
value of "theme" if it exists, or null if it doesn't.
Explanation: getItem() retrieves the value of the item stored with the
given key or null if the key doesn't exist.
30. What
will localStorage.setItem('key', {}) do?
- A)
It will throw an error.
- B)
It will store the object as-is.
- C)
It will store the object as a string "[object Object]".
- D)
It will store undefined.
Answer: C) It
will store the object as a string "[object Object]".
Explanation: If you try to store an object directly in localStorage, it
is automatically converted to a string (which results in "[object
Object]").
31. Which
of the following methods is used to store a boolean value in localStorage?
- A)
JSON.serialize()
- B)
JSON.stringify()
- C)
localStorage.setItem()
- D)
localStorage.add()
Answer: B)
JSON.stringify()
Explanation: localStorage can only store strings, so you would use
JSON.stringify() to convert a boolean value into a string before storing it.
32. What
happens if you call sessionStorage.setItem('key', 'value') in one tab and call
it in another tab?
- A)
The data will be shared between the tabs.
- B)
The second tab will overwrite the data from the first tab.
- C)
The second tab will not be able to access the data.
- D)
The first tab will be cleared.
Answer: C) The
second tab will not be able to access the data.
Explanation: sessionStorage is limited to the tab where the data was
stored. It cannot be shared across tabs.
33. Which
event can be used to detect when a localStorage item is changed?
- A)
storage
- B)
change
- C)
modified
- D)
updated
Answer: A)
storage
Explanation: The storage event is triggered when the localStorage or
sessionStorage is modified from a different window or tab.
34. Which
of the following is NOT a characteristic of localStorage?
- A)
It stores data persistently across sessions.
- B)
It can be accessed across different tabs and windows.
- C)
It has a small data storage capacity (typically 5MB).
- D)
It can only store string data.
Answer: B) It
can be accessed across different tabs and windows.
Explanation: localStorage data is accessible across the same origin in
all tabs and windows, but only within the same domain and browser.
35. What
will the following code do? localStorage.setItem('user', JSON.stringify({ name:
'John' }));
- A)
It will store the string "user" with the value { name: 'John' }.
- B)
It will store the object as a string.
- C)
It will throw an error.
- D)
It will store the object as-is.
Answer: B) It
will store the object as a string.
Explanation: JSON.stringify() is used to convert the object into a
string before storing it in localStorage.
36. What
does localStorage.getItem('user') return after calling the code in the previous
question?
- A)
undefined
- B) {
name: 'John' }
- C)
"[object Object]"
- D)
"{"name":"John"}"
Answer: D)
"{"name":"John"}"
Explanation: Since the object was stored as a string using
JSON.stringify(), getItem() will return it as a string.
37. Which
of the following is true about localStorage and sessionStorage?
- A)
Both are shared across multiple devices.
- B)
Both are part of the window object.
- C)
Both can store data persistently until the browser is closed.
- D)
Both can store data for the duration of a user session.
Answer: B) Both
are part of the window object.
Explanation: localStorage and sessionStorage are both accessible via the
window object in JavaScript.
I hope
these questions help you better understand localStorage and sessionStorage. Let
me know if you'd like more questions or additional explanations!
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are
more MCQs to help you dive deeper into Local Storage and Session
Storage:
38. Which
of the following is the correct method to check if an item exists in
localStorage?
- A)
localStorage.exists('key')
- B)
localStorage.getItem('key') !== null
- C)
localStorage.hasItem('key')
- D)
localStorage.isItem('key')
Answer: B)
localStorage.getItem('key') !== null
Explanation: You can check if a key exists in localStorage by retrieving
the item and comparing it to null. If it’s null, the item doesn’t exist.
39. Which
of the following would store the boolean true in localStorage?
- A)
localStorage.setItem('flag', true)
- B)
localStorage.setItem('flag', 'true')
- C)
localStorage.setItem('flag', JSON.stringify(true))
- D)
localStorage.setItem('flag', JSON.parse('true'))
Answer: C)
localStorage.setItem('flag', JSON.stringify(true))
Explanation: localStorage only stores strings, so a boolean value like
true must be serialized using JSON.stringify().
40. Which
method is used to retrieve an item from localStorage?
- A)
localStorage.get()
- B)
localStorage.retrieve()
- C)
localStorage.getItem()
- D)
localStorage.getData()
Answer: C)
localStorage.getItem()
Explanation: The getItem() method is used to retrieve data from
localStorage using the key.
41. Which
of the following is NOT a valid use case for localStorage?
- A)
Storing session-related data during a user's visit to a page.
- B)
Storing user preferences for the website, like themes and language.
- C)
Storing login credentials for automatic login.
- D)
Storing a shopping cart's content across multiple visits.
Answer: C)
Storing login credentials for automatic login.
Explanation: Storing sensitive data like login credentials in
localStorage is not secure, as the data is stored as plain text.
42. What
will localStorage.setItem('key', 'value') do if the key already exists in
localStorage?
- A)
It will update the value of the existing key.
- B)
It will throw an error.
- C)
It will create a new item with the same key.
- D)
It will delete the existing key and add a new one.
Answer: A) It
will update the value of the existing key.
Explanation: If the key already exists, setItem() will overwrite the
previous value.
43. Which
of the following is true about sessionStorage data?
- A)
It is shared between all tabs and windows.
- B)
It persists even when the browser is closed and reopened.
- C)
It is cleared when the tab or browser window is closed.
- D)
It can store data for as long as the page is in memory, even after
refresh.
Answer: C) It
is cleared when the tab or browser window is closed.
Explanation: sessionStorage persists data for the duration of the page
session and is cleared when the session ends (when the tab is closed).
44. How
do you remove all items from localStorage?
- A)
localStorage.removeAll()
- B)
localStorage.delete()
- C)
localStorage.clear()
- D)
localStorage.reset()
Answer: C)
localStorage.clear()
Explanation: clear() removes all items stored in localStorage.
45. How
can you store a JavaScript object in localStorage?
- A)
Convert the object into a string using JSON.stringify().
- B)
Store it as an object directly.
- C)
Convert it into a number before storing.
- D)
Use localStorage.set() to store an object.
Answer: A)
Convert the object into a string using JSON.stringify().
Explanation: Since localStorage only supports strings, you need to use
JSON.stringify() to convert objects into a string before storing.
46. If
you attempt to retrieve an item from localStorage that does not exist, what
will be returned?
- A)
undefined
- B)
null
- C)
false
- D)
An empty string ""
Answer: B) null
Explanation: If the item doesn’t exist in localStorage, getItem() will
return null.
47. How
would you parse a stringified object retrieved from localStorage?
- A)
JSON.stringify()
- B)
JSON.parse()
- C)
JSON.decode()
- D)
parseObject()
Answer: B) JSON.parse()
Explanation: JSON.parse() is used to parse a stringified object and
convert it back into a JavaScript object.
48. Which
of the following methods would you use to check if a localStorage item exists?
- A) localStorage.exists('item')
- B)
localStorage.getItem('item') !== undefined
- C)
localStorage.has('item')
- D)
localStorage.getItem('item') !== null
Answer: D)
localStorage.getItem('item') !== null
Explanation: To check if an item exists, you can retrieve it with
getItem() and check if the result is not null.
49. Can
data stored in sessionStorage be accessed after the browser is closed and
reopened?
- A)
Yes, if the user has enabled cookies.
- B)
No, sessionStorage is cleared when the browser is closed.
- C)
Yes, sessionStorage data persists even after the browser is closed.
- D)
Only if the user stores data in both localStorage and sessionStorage.
Answer: B) No,
sessionStorage is cleared when the browser is closed.
Explanation: Data in sessionStorage persists only for the duration of
the page session and is cleared when the browser or tab is closed.
50. Which
of the following is true about the data stored in localStorage?
- A)
It is stored as encrypted data.
- B)
It is stored as plain text and can be easily accessed from the browser
console.
- C)
It is stored as a binary file.
- D)
It is stored in a separate file on the server.
Answer: B) It
is stored as plain text and can be easily accessed from the browser console.
Explanation: Data in localStorage is stored as plain text, and anyone
with access to the browser's developer tools can inspect it.
51. What
is the main difference between localStorage and sessionStorage?
- A)
localStorage has a smaller storage limit than sessionStorage.
- B)
localStorage persists even after the browser is closed, while
sessionStorage does not.
- C)
localStorage can only be used on secure websites, while sessionStorage can
be used on all websites.
- D)
sessionStorage can store more data than localStorage.
Answer: B)
localStorage persists even after the browser is closed, while sessionStorage
does not.
Explanation: The main difference is that localStorage persists until
explicitly cleared, while sessionStorage is cleared when the session
(browser/tab) ends.
52. What
is the maximum storage limit for localStorage in most browsers?
- A)
2MB
- B)
5MB
- C)
10MB
- D)
50MB
Answer: B) 5MB
Explanation: Most modern browsers allow up to 5MB of data storage per
domain in localStorage.
53. How
would you update an item in sessionStorage?
- A)
Use sessionStorage.setItem('key', 'new value').
- B)
Use sessionStorage.update('key', 'new value').
- C)
Use sessionStorage.modifyItem('key', 'new value').
- D)
Use sessionStorage.addItem('key', 'new value').
Answer: A) Use sessionStorage.setItem('key',
'new value').
Explanation: setItem() is used to update an existing item in
sessionStorage.
I hope
these additional MCQs give you a more comprehensive understanding of Local
Storage and Session Storage! Feel free to ask if you need
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are
more MCQs to help you understand Local Storage and Session Storage
in depth:
54. What
will happen if you exceed the storage limit for localStorage?
- A)
The browser will automatically delete the oldest data.
- B)
The browser will throw a "QuotaExceededError" and stop allowing
further storage.
- C)
The data will be stored in a separate file on the server.
- D)
The data will be automatically compressed to fit within the limit.
Answer: B) The
browser will throw a "QuotaExceededError" and stop allowing further
storage.
Explanation: If you try to store more data than the storage limit
allows, the browser will throw an error called "QuotaExceededError."
55. Which
method is used to remove a specific item from localStorage?
- A)
localStorage.remove()
- B)
localStorage.removeItem()
- C)
localStorage.delete()
- D)
localStorage.clearItem()
Answer: B)
localStorage.removeItem()
Explanation: removeItem() is used to remove a specific item from
localStorage by its key.
56. Which
of the following will be the correct way to store a JavaScript array in
localStorage?
- A)
localStorage.setItem('myArray', [1, 2, 3])
- B)
localStorage.setItem('myArray', JSON.stringify([1, 2, 3]))
- C)
localStorage.setArray('myArray', [1, 2, 3])
- D)
localStorage.setItem('myArray', '1,2,3')
Answer: B)
localStorage.setItem('myArray', JSON.stringify([1, 2, 3]))
Explanation: To store an array in localStorage, you need to serialize it
using JSON.stringify() since localStorage only accepts strings.
57. Which
of the following is NOT a valid use case for localStorage?
- A)
Storing shopping cart items.
- B)
Storing a user’s session token.
- C)
Storing user preferences such as theme.
- D)
Storing sensitive information such as passwords.
Answer: D)
Storing sensitive information such as passwords.
Explanation: localStorage is not secure enough to store sensitive data
like passwords. Sensitive data should be stored in a more secure way, such as
using a server-side session or encrypted cookies.
58. What
is the data storage limit for sessionStorage in most browsers?
- A)
5MB
- B)
10MB
- C)
2MB
- D)
50MB
Answer: A) 5MB
Explanation: Similar to localStorage, sessionStorage generally allows up
to 5MB of storage per domain in most browsers.
59. If
you try to store undefined in localStorage, what will happen?
- A)
It will throw an error.
- B)
It will store the string 'undefined'.
- C)
It will store an empty object.
- D)
It will store null.
Answer: B) It
will store the string 'undefined'.
Explanation: Since localStorage only stores strings, attempting to store
undefined will convert it to the string 'undefined'.
60. Which
of the following statements is true about localStorage and sessionStorage?
- A)
Both localStorage and sessionStorage can store data for unlimited time
periods.
- B)
localStorage can store data across browser sessions, while sessionStorage
can only store data for the current session.
- C)
localStorage can only store data for the current session, while
sessionStorage stores data for unlimited time periods.
- D)
Both localStorage and sessionStorage are cleared when the browser is
closed.
Answer: B)
localStorage can store data across browser sessions, while sessionStorage can
only store data for the current session.
Explanation: The key difference is the persistence of data. localStorage
persists even after the browser is closed, while sessionStorage is cleared when
the session ends (when the browser or tab is closed).
61. How
do you retrieve an object stored in localStorage after it has been stringified?
- A) JSON.parse(localStorage.getItem('key'))
- B)
localStorage.parse(localStorage.getItem('key'))
- C)
localStorage.getItem('key').parse()
- D)
JSON.stringify(localStorage.getItem('key'))
Answer: A) JSON.parse(localStorage.getItem('key'))
Explanation: To retrieve a stringified object from localStorage, you use
JSON.parse() to convert it back into a JavaScript object.
62. Which
of the following methods is used to check whether an item exists in
localStorage?
- A)
localStorage.contains('key')
- B)
localStorage.exists('key')
- C)
localStorage.getItem('key') !== null
- D)
localStorage.isStored('key')
Answer: C)
localStorage.getItem('key') !== null
Explanation: To check if a key exists in localStorage, use getItem() and
check if the returned value is not null.
63. What
is the primary difference between localStorage and cookies?
- A)
Cookies are more secure than localStorage.
- B)
localStorage has a higher storage limit than cookies.
- C)
Cookies are stored per domain and per path, while localStorage stores data
per domain only.
- D)
Both cookies and localStorage store data for the same duration.
Answer: B)
localStorage has a higher storage limit than cookies.
Explanation: localStorage allows around 5MB of data storage, while
cookies have a much smaller storage limit (typically around 4KB).
64. Can
sessionStorage be shared between different tabs or windows of the same browser?
- A)
Yes, sessionStorage is shared across tabs.
- B)
No, sessionStorage is unique to each tab or window.
- C)
Yes, but only if the tabs are opened from the same origin.
- D)
No, sessionStorage is stored on the server and can be accessed from any
tab.
Answer: B) No,
sessionStorage is unique to each tab or window.
Explanation: sessionStorage is specific to the window or tab in which
the data is stored, and it cannot be shared between tabs or windows.
65. Which
of the following data types can be directly stored in localStorage without
conversion?
- A)
Object
- B)
Array
- C)
Boolean
- D)
String
Answer: D)
String
Explanation: localStorage only stores strings, so other data types like
objects and arrays must be serialized (converted to strings) before storage.
66. Which
of the following is true about the localStorage object?
- A)
It is a global object available on every page of the website.
- B)
It is a function that can be called to store data.
- C)
It is only available on secure (HTTPS) websites.
- D)
It is a property of the document object.
Answer: A) It
is a global object available on every page of the website.
Explanation: localStorage is a global object that can be accessed and
used on any page of the website that shares the same origin (protocol, domain,
and port).
67. Which
of the following actions will remove all data stored in sessionStorage?
- A)
sessionStorage.clear()
- B)
sessionStorage.removeItem()
- C)
sessionStorage.deleteAll()
- D)
sessionStorage.clearAll()
Answer: A)
sessionStorage.clear()
Explanation: sessionStorage.clear() will remove all data from
sessionStorage for the current page session.
68. How
would you store a user’s theme preference (e.g., dark mode) in localStorage?
- A)
localStorage.setItem('theme', 'dark')
- B)
localStorage.setItem('theme', true)
- C)
localStorage.set('theme', 'dark')
- D)
localStorage.theme = 'dark'
Answer: A)
localStorage.setItem('theme', 'dark')
Explanation: To store the theme preference, you would use setItem() to
store the key-value pair in localStorage.
I hope
these additional MCQs provide you with more clarity and depth on Local
Storage and Session Storage. Let me know if you need further
assistance!
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are
more MCQs to help you understand Local Storage and Session Storage
in detail:
69. Which
of the following is true about the data stored in sessionStorage?
- A)
Data is available to all pages from the same domain, including other tabs.
- B)
Data is cleared when the page is refreshed.
- C)
Data is available for the entire session and is cleared when the browser
is closed.
- D)
Data is available for the entire session until the user manually clears
it.
Answer: C) Data
is available for the entire session and is cleared when the browser is closed.
Explanation: sessionStorage stores data only for the duration of the
page session, and the data is cleared once the browser or tab is closed.
70. Which
of the following will correctly retrieve a JSON object stored in localStorage?
- A)
JSON.parse(localStorage.getItem('data'))
- B)
localStorage.getItem('data').JSON()
- C)
localStorage.get('data').parseJSON()
- D)
localStorage.retrieveItem('data')
Answer: A)
JSON.parse(localStorage.getItem('data'))
Explanation: To retrieve a stored JSON object, you must first use
localStorage.getItem() and then parse it using JSON.parse() to convert it back
into a JavaScript object.
71. Which
of the following is a correct way to clear all data stored in localStorage?
- A)
localStorage.clearAll()
- B)
localStorage.clear()
- C)
localStorage.removeAll()
- D)
localStorage.delete()
Answer: B)
localStorage.clear()
Explanation: To remove all data stored in localStorage, use the clear()
method, which will remove all key-value pairs.
72. What
happens if you attempt to retrieve a non-existing key from localStorage?
- A)
It returns null.
- B)
It returns an empty object {}.
- C)
It throws an error.
- D)
It returns undefined.
Answer: A) It
returns null.
Explanation: When attempting to retrieve a non-existing key,
localStorage returns null rather than throwing an error.
73. Which
of the following is the correct method to check the number of items stored in
localStorage?
- A)
localStorage.size()
- B)
localStorage.count()
- C)
localStorage.length
- D)
localStorage.items()
Answer: C)
localStorage.length
Explanation: localStorage.length returns the number of key-value pairs
stored in localStorage.
74. Can
sessionStorage be accessed across different browsers (e.g., Chrome, Firefox) on
the same machine?
- A)
Yes, sessionStorage is shared across browsers on the same machine.
- B)
No, sessionStorage is specific to each browser and is not shared.
- C)
Yes, sessionStorage is shared across different machines.
- D)
No, sessionStorage is only accessible from a particular website.
Answer: B) No,
sessionStorage is specific to each browser and is not shared.
Explanation: sessionStorage is unique to the session within the same
browser. It is not shared between browsers or even tabs in different browsers.
75. Which
method would you use to store a key-value pair in localStorage?
- A)
localStorage.saveItem('key', 'value')
- B)
localStorage.setItem('key', 'value')
- C)
localStorage.addItem('key', 'value')
- D)
localStorage.store('key', 'value')
Answer: B)
localStorage.setItem('key', 'value')
Explanation: To store a key-value pair in localStorage, you use the
setItem() method.
76. What
happens to data in localStorage when the browser is closed and reopened?
- A)
Data is automatically deleted.
- B)
Data persists and remains available after reopening the browser.
- C)
Data is stored on the server and can be accessed from any device.
- D)
Data is moved to cookies.
Answer: B) Data
persists and remains available after reopening the browser.
Explanation: localStorage data is persistent even after the browser is
closed and reopened.
77. What
is the maximum storage limit of localStorage in most browsers?
- A)
5KB
- B)
10MB
- C)
2MB
- D)
50MB
Answer: B) 10MB
Explanation: Most modern browsers allow localStorage to store up to 5MB
to 10MB of data.
78. Can
you store an array directly in localStorage?
- A)
Yes, localStorage can store arrays directly.
- B)
No, arrays need to be converted into a string first using
JSON.stringify().
- C)
Yes, but arrays are stored as objects.
- D)
No, arrays cannot be stored in localStorage.
Answer: B) No,
arrays need to be converted into a string first using JSON.stringify().
Explanation: localStorage only stores strings, so you must serialize an
array using JSON.stringify() before storing it.
79. What
is the main difference between localStorage and cookies?
- A)
localStorage data is stored on the server, while cookies are stored on the
client side.
- B)
localStorage can store more data than cookies.
- C)
localStorage data is sent to the server with each HTTP request, while
cookies are not.
- D)
Cookies are faster than localStorage for retrieving data.
Answer: B)
localStorage can store more data than cookies.
Explanation: localStorage has a much higher storage limit (typically
5MB) compared to cookies, which are limited to about 4KB.
80. Which
of the following is true about localStorage and sessionStorage?
- A)
Both are used for server-side storage.
- B)
Both are used to store data in the browser, but sessionStorage data is
cleared when the browser is closed.
- C)
sessionStorage stores data permanently, while localStorage is only
temporary.
- D)
localStorage and sessionStorage are the same in terms of data persistence.
Answer: B) Both
are used to store data in the browser, but sessionStorage data is cleared when
the browser is closed.
Explanation: sessionStorage is session-specific and is cleared when the
session ends (when the browser or tab is closed), while localStorage data
persists across sessions.
81. Which
of the following is a correct method for checking if a specific item exists in
localStorage?
- A)
localStorage.isItem('key')
- B)
localStorage.hasItem('key')
- C)
localStorage.getItem('key') !== null
- D)
localStorage.contains('key')
Answer: C)
localStorage.getItem('key') !== null
Explanation: To check if a key exists in localStorage, you retrieve it
using getItem() and check if the returned value is not null.
82. How
do you store a number value in localStorage?
- A)
localStorage.setItem('age', 25)
- B)
localStorage.setItem('age', '25')
- C)
localStorage.setItem('age', JSON.stringify(25))
- D)
localStorage.setItem('age', number(25))
Answer: B)
localStorage.setItem('age', '25')
Explanation: localStorage only accepts strings, so even though 25 is a
number, it will be stored as the string '25'. If you need to store a number as
a number, use JSON.stringify() to serialize it and JSON.parse() when
retrieving.
83. Which
of the following can be stored in localStorage?
- A)
Functions
- B)
Dates
- C)
Arrays
- D)
Objects (without converting them to a string)
Answer: C)
Arrays
Explanation: localStorage can store arrays if they are serialized using
JSON.stringify() before being stored, but it cannot directly store objects,
functions, or dates without serialization.
84. What
is the method used to remove all data from sessionStorage?
- A)
sessionStorage.removeAll()
- B)
sessionStorage.clear()
- C)
sessionStorage.remove()
- D)
sessionStorage.deleteAll()
Answer: B) sessionStorage.clear()
Explanation: The clear() method removes all key-value pairs from
sessionStorage.
85. Which
storage method should you use for data that should persist even after the user
closes the browser?
- A)
localStorage
- B)
sessionStorage
- C)
Cookies
- D)
None of the above
Answer: A)
localStorage
Explanation: localStorage data persists even after the browser is
closed, unlike sessionStorage which is cleared when the session ends.
Let me
know if you'd like more questions or need further assistance!
4o mini
You said:
yes
ChatGPT
said:
ChatGPT
Here are
more MCQs to continue enhancing your understanding of Local Storage and Session
Storage:
86. Which
of the following methods is used to remove a single item from localStorage?
- A) localStorage.remove()
- B)
localStorage.deleteItem()
- C)
localStorage.removeItem()
- D)
localStorage.clearItem()
Answer: C)
localStorage.removeItem()
Explanation: The removeItem() method is used to remove a specific
key-value pair from localStorage.
87. Which
of the following is NOT true about localStorage?
- A)
It is available only within the same origin (domain).
- B)
Data stored in localStorage persists until the user manually clears it.
- C)
Data in localStorage is automatically removed when the session ends.
- D)
It can only store data as strings.
Answer: C) Data
in localStorage is automatically removed when the session ends.
Explanation: localStorage persists data even after the session ends. It
is cleared only if the user manually clears the browser’s cache.
88. Which
of the following correctly sets an item in localStorage?
- A)
localStorage.addItem("name", "John")
- B)
localStorage.storeItem("name", "John")
- C)
localStorage.setItem("name", "John")
- D)
localStorage.insertItem("name", "John")
Answer: C)
localStorage.setItem("name", "John")
Explanation: The correct method for setting an item in localStorage is
setItem(). It stores a key-value pair.
89. How
can you clear the data stored in both localStorage and sessionStorage?
- A) localStorage.clear()
&& sessionStorage.clear()
- B)
clearStorage()
- C)
localStorage.removeAll() && sessionStorage.removeAll()
- D)
clearAllData()
Answer: A)
localStorage.clear() && sessionStorage.clear()
Explanation: To clear the data stored in both localStorage and
sessionStorage, use clear() method for both.
90. What
happens when you try to set an item in localStorage that exceeds the storage
limit?
- A)
The item will be stored, but it will be truncated.
- B)
The item will be stored with an error message.
- C)
The item will not be stored, and an error will be thrown.
- D)
The browser will automatically compress the data.
Answer: C) The
item will not be stored, and an error will be thrown.
Explanation: If the data exceeds the storage limit of localStorage (usually
around 5MB), an exception will be thrown.
91. Which
of the following methods can you use to retrieve all keys in localStorage?
- A)
localStorage.getAllKeys()
- B)
Object.keys(localStorage)
- C)
localStorage.keys()
- D)
localStorage.getItem()
Answer: B)
Object.keys(localStorage)
Explanation: Object.keys(localStorage) returns an array of all keys
stored in localStorage.
92. What
will be the result of calling localStorage.setItem('name', 'John') twice with
the same key ('name')?
- A)
It will create a new entry in localStorage with the same key.
- B)
It will overwrite the existing value of 'name' with 'John'.
- C)
It will cause an error since you can't store the same key twice.
- D)
It will append the new value to the old value.
Answer: B) It
will overwrite the existing value of 'name' with 'John'.
Explanation: If you call setItem() with the same key, the value will be
overwritten, and the key-value pair will be updated.
93. Which
of the following is NOT a feature of localStorage?
- A)
Stores data permanently across sessions.
- B)
Accessible from different tabs and windows within the same domain.
- C)
Data is sent to the server with each HTTP request.
- D)
Data persists even when the browser is closed and reopened.
Answer: C) Data
is sent to the server with each HTTP request.
Explanation: localStorage does not send data to the server with each
request; it is stored locally on the client side.
94. Which
of the following is the correct way to retrieve a number from localStorage?
- A) localStorage.getItem('age')
- B)
localStorage.getItem('age') + 0
- C)
parseInt(localStorage.getItem('age'))
- D)
All of the above
Answer: D) All
of the above
Explanation: All the options are correct for retrieving a number from
localStorage. However, the value is stored as a string, so you may need to
convert it using parseInt() or adding 0 to convert it to a number.
95. Which
statement is true about the storage limit of localStorage?
- A)
It is fixed and cannot be changed.
- B)
It is limited to 2MB in all browsers.
- C)
It is typically between 5MB and 10MB in most modern browsers.
- D)
It is unlimited and can store an infinite amount of data.
Answer: C) It
is typically between 5MB and 10MB in most modern browsers.
Explanation: The storage limit for localStorage varies between browsers
but is usually between 5MB and 10MB.
96. Which
of the following is true about sessionStorage?
- A)
It persists across different tabs and windows of the browser.
- B)
It is accessible only in the current tab or window.
- C)
It stores data permanently across sessions.
- D)
Data is automatically synchronized across different browsers.
Answer: B) It
is accessible only in the current tab or window.
Explanation: sessionStorage is specific to the current tab or window and
does not persist across tabs or windows, unlike localStorage.
97. What
is the maximum size of a single string that can be stored in localStorage in
most browsers?
- A)
4KB
- B)
10MB
- C)
2MB
- D)
500KB
Answer: B) 10MB
Explanation: Most modern browsers allow localStorage to store up to 5MB
to 10MB of data per domain.
98. Which
of the following will correctly store an array in localStorage?
- A)
localStorage.setItem('myArray', ['a', 'b', 'c'])
- B)
localStorage.setItem('myArray', JSON.stringify(['a', 'b', 'c']))
- C)
localStorage.setItem('myArray', ['a', 'b', 'c'].toString())
- D)
localStorage.storeItem('myArray', ['a', 'b', 'c'])
Answer: B)
localStorage.setItem('myArray', JSON.stringify(['a', 'b', 'c']))
Explanation: Since localStorage only stores strings, you need to use
JSON.stringify() to convert the array into a string before storing it.
99. How
can you check whether a specific key exists in sessionStorage?
- A)
sessionStorage.hasItem('key')
- B)
sessionStorage.getItem('key') !== null
- C)
sessionStorage.contains('key')
- D)
sessionStorage.itemExists('key')
Answer: B)
sessionStorage.getItem('key') !== null
Explanation: To check if a specific key exists in sessionStorage, use
sessionStorage.getItem() and check if it returns null.
100.
Which of the following will correctly store an object in localStorage?
- A)
localStorage.setItem('user', {name: 'John', age: 30})
- B)
localStorage.setItem('user', JSON.stringify({name: 'John', age: 30}))
- C)
localStorage.set('user', {name: 'John', age: 30})
- D)
localStorage.store('user', {name: 'John', age: 30})
Answer: B)
localStorage.setItem('user', JSON.stringify({name: 'John', age: 30}))
Explanation: To store an object in localStorage, it must first be
converted into a string using JSON.stringify().
