HTML5 HTML5 Web Storage and APIs 1 — Questions and Answers
Question 1: Which HTML5 Web Storage object stores data with no expiration date, persisting even after the browser is closed?
- localStorage (Correct answer)
- sessionStorage
- cookieStorage
- cacheStorage
Correct answer: localStorage
localStorage stores key-value pairs with no expiration, persisting across browser sessions until explicitly cleared.
Question 2: What is the difference between localStorage and sessionStorage?
- localStorage persists until cleared; sessionStorage clears when the tab closes (Correct answer)
- sessionStorage is larger; localStorage has a 5 KB limit
- localStorage is per-domain; sessionStorage is per-user
- They are identical in behavior
Correct answer: localStorage persists until cleared; sessionStorage clears when the tab closes
sessionStorage data is cleared when the browser tab or window is closed, while localStorage data persists indefinitely until explicitly removed.
Question 3: Which method is used to store a value in localStorage?
- localStorage.setItem(key, value) (Correct answer)
- localStorage.store(key, value)
- localStorage.save(key, value)
- localStorage.put(key, value)
Correct answer: localStorage.setItem(key, value)
The setItem(key, value) method stores a key-value pair in the localStorage object.
Question 4: What does the Geolocation API's getCurrentPosition() method do?
- Retrieves the device's current geographic coordinates asynchronously (Correct answer)
- Returns a map of the current location
- Sets the browser's location preference
- Continuously tracks position changes
Correct answer: Retrieves the device's current geographic coordinates asynchronously
getCurrentPosition() asynchronously requests the device's current position and passes the coordinates to a success callback.
Question 5: Which HTML5 API enables communication between a web page and a server without page reload?
- XMLHttpRequest / Fetch API
- WebSocket API
- Server-Sent Events
- All of the above (Correct answer)
Correct answer: All of the above
XMLHttpRequest, the Fetch API, WebSocket, and Server-Sent Events all allow web pages to communicate with servers without a full page reload.
Question 6: What is the purpose of the HTML5 History API?
- Allows manipulation of the browser session history without page reloads (Correct answer)
- Records the user's browsing history
- Provides access to cached pages
- Controls browser back/forward button behavior
Correct answer: Allows manipulation of the browser session history without page reloads
The History API lets developers push states to and manipulate the browser's session history, enabling SPA navigation without full page loads.
Which HTML5 Web Storage object stores data with no expiration date, persisting even after the browser is closed?