MCQs Of Day 29: JavaScript and Web APIs

Rashmi Mishra
0

MCQs Of Day 29: JavaScript and Web APIs


1. Which method is used to make HTTP requests in JavaScript to fetch data from an API?

  • A) get()
  • B) fetch()
  • C) axios()
  • D) request()

Answer: B) fetch()

Explanation: fetch() is the native JavaScript method used for making HTTP requests to retrieve data from an API.


2. What is the default HTTP method used by the fetch() API if no method is specified?

  • A) GET
  • B) POST
  • C) PUT
  • D) DELETE

Answer: A) GET

Explanation: By default, fetch() uses the GET method unless otherwise specified.


3. How can you parse the response from a fetch() call to convert it into JSON?

  • A) response.text()
  • B) response.json()
  • C) response.data()
  • D) response.parse()

Answer: B) response.json()

Explanation: The json() method is used to parse the response and return a JavaScript object.


4. Which method is used to handle errors in fetch()?

  • A) .catch()
  • B) .finally()
  • C) .error()
  • D) .then()

Answer: A) .catch()

Explanation: .catch() is used to handle errors that occur in the fetch() promise chain.


5. What does the Geolocation API allow you to do?

  • A) Fetch data from a weather API
  • B) Retrieve the user's geographic location
  • C) Store user preferences in the browser
  • D) Send data to a server

Answer: B) Retrieve the user's geographic location

Explanation: The Geolocation API provides methods to retrieve the geographical location of the device, such as latitude and longitude.


6. Which method is used to get the current position of the user in the Geolocation API?

  • A) getLocation()
  • B) getPosition()
  • C) getCurrentPosition()
  • D) getGeoPosition()

Answer: C) getCurrentPosition()

Explanation: The getCurrentPosition() method is used to get the user's current geographical location.


7. How do you check if the Geolocation API is supported by the browser?

  • A) navigator.geolocation.exists
  • B) navigator.geolocation.isSupported
  • C) navigator.geolocation
  • D) navigator.geolocation.enable

Answer: C) navigator.geolocation

Explanation: If navigator.geolocation is defined, it means the Geolocation API is supported by the browser.


8. What does the Web Storage API allow you to do?

  • A) Store data on a server
  • B) Create temporary storage in the browser
  • C) Access local files from the system
  • D) Encrypt data in the browser

Answer: B) Create temporary storage in the browser

Explanation: The Web Storage API allows you to store data on the client side, either temporarily using sessionStorage or persistently using localStorage.


9. Which method is used to store data in localStorage?

  • A) store()
  • B) setItem()
  • C) saveItem()
  • D) addItem()

Answer: B) setItem()

Explanation: The setItem() method is used to store a key-value pair in localStorage.


10. How can you retrieve a value from localStorage?

  • A) getItem()
  • B) fetch()
  • C) retrieve()
  • D) getData()

Answer: A) getItem()

Explanation: The getItem() method retrieves the value associated with the given key from localStorage.


11. What is the maximum amount of data you can store in localStorage?

  • A) 5MB
  • B) 2MB
  • C) 10MB
  • D) 1MB

Answer: A) 5MB

Explanation: Browsers typically limit the amount of data that can be stored in localStorage to around 5MB.


12. Which of the following is used to store data that expires after the session ends?

  • A) sessionStorage
  • B) localStorage
  • C) indexedDB
  • D) cookieStorage

Answer: A) sessionStorage

Explanation: sessionStorage stores data for the duration of the page session, meaning it is cleared once the browser window or tab is closed.


13. Which of the following is NOT a valid HTTP request method?

  • A) GET
  • B) POST
  • C) PATCH
  • D) PUT
  • E) DELETE
  • F) UPDATE

Answer: F) UPDATE

Explanation: "UPDATE" is not a valid HTTP request method; the correct method would be PATCH or PUT, depending on the type of update.


14. How can you send data in the body of a POST request using the fetch() API?

  • A) Add the data directly in the URL
  • B) Set the method property to POST and include a body property
  • C) Use localStorage
  • D) Set headers to application/json

Answer: B) Set the method property to POST and include a body property

Explanation: In a POST request, you include data in the body property of the fetch() options, and the method is set to "POST".


15. Which of the following is a valid response status code for a successful HTTP request?

  • A) 200
  • B) 404
  • C) 500
  • D) 403

Answer: A) 200

Explanation: A 200 OK status code indicates that the request was successful.


16. Which of the following is used to parse the response data to a string in fetch()?

  • A) response.text()
  • B) response.json()
  • C) response.data()
  • D) response.parse()

Answer: A) response.text()

Explanation: response.text() is used to parse the response data to a string, while response.json() is used to parse JSON data.


17. How do you check the status of the fetch() response?

  • A) response.status()
  • B) response.success()
  • C) response.status
  • D) response.ok()

Answer: C) response.status

Explanation: response.status contains the HTTP status code of the response, while response.ok checks if the status code is in the range 200-299.


18. Which of the following is the correct syntax to handle JSON data in fetch()?

  • A) fetch(url).then(response => response.parse())
  • B) fetch(url).then(response => response.json())
  • C) fetch(url).then(response => response.toJSON())
  • D) fetch(url).then(response => response.getJSON())

Answer: B) fetch(url).then(response => response.json())

Explanation: The .json() method is used to parse the response as JSON data.


19. What does the navigator.geolocation.getCurrentPosition() method require as arguments?

  • A) Success callback, Error callback
  • B) API key, Location type
  • C) User credentials
  • D) Latitude, Longitude

Answer: A) Success callback, Error callback

Explanation: The getCurrentPosition() method requires two arguments: a success callback and an error callback to handle the result or errors.


20. Which of the following APIs can be used to persist data even after the user closes the browser window?

  • A) sessionStorage
  • B) cookieStorage
  • C) localStorage
  • D) indexedDB

Answer: C) localStorage

Explanation: localStorage persists data across sessions until explicitly cleared, whereas sessionStorage is cleared when the browser window is closed.


(These are the first 20 questions. You can repeat the pattern to reach the total of 100 MCQs, covering various scenarios and APIs like Fetch, Geolocation, Web Storage, etc.)

Let me know if you'd like to continue with more or need additional explanations!

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are more MCQs (questions 21 to 40) on the topic JavaScript and Web APIs with explanations:


21. What does the fetch() API return when called?

  • A) A promise
  • B) A JSON object
  • C) An array
  • D) A string

Answer: A) A promise

Explanation: fetch() returns a Promise that resolves to the Response object representing the response to the request.


22. What is the purpose of response.ok in the fetch() API?

  • A) It indicates whether the request was successful (status 200-299).
  • B) It checks the response for errors.
  • C) It parses the response body to JSON.
  • D) It indicates whether the request has been completed.

Answer: A) It indicates whether the request was successful (status 200-299).

Explanation: response.ok is a boolean that returns true if the response status code is between 200 and 299, indicating a successful response.


23. Which method of the Web Storage API allows you to remove a key-value pair from localStorage?

  • A) removeItem()
  • B) deleteItem()
  • C) clearItem()
  • D) unsetItem()

Answer: A) removeItem()

Explanation: The removeItem() method is used to remove a key-value pair from localStorage by specifying the key.


24. Which method in the Geolocation API would you use to handle errors when retrieving a user’s location?

  • A) getPosition()
  • B) getCurrentPosition()
  • C) error()
  • D) onError()

Answer: C) error()

Explanation: In the getCurrentPosition() method, the error handling function (callback) is used to handle errors, not a specific error() method.


25. Which of the following is NOT supported by the Web Storage API?

  • A) localStorage
  • B) sessionStorage
  • C) cookieStorage
  • D) indexedDB

Answer: C) cookieStorage

Explanation: The Web Storage API supports localStorage and sessionStorage. cookieStorage is not a part of the Web Storage API.


26. How do you store an object in localStorage?

  • A) localStorage.setItem('user', {name: 'John'})
  • B) localStorage.setItem('user', JSON.stringify({name: 'John'}))
  • C) localStorage.saveItem('user', {name: 'John'})
  • D) localStorage.store('user', {name: 'John'})

Answer: B) localStorage.setItem('user', JSON.stringify({name: 'John'}))

Explanation: Since localStorage only stores strings, objects must be converted to a JSON string using JSON.stringify() before storing.


27. What does sessionStorage store?

  • A) Data that persists across sessions
  • B) Data only for the current session
  • C) Data that can only be accessed by the server
  • D) Data that expires after a certain time

Answer: B) Data only for the current session

Explanation: sessionStorage stores data for the duration of the page session, which is cleared when the tab or browser window is closed.


28. What type of data is suitable for using localStorage?

  • A) Sensitive personal data
  • B) Temporary session data
  • C) Large binary files
  • D) Non-sensitive, small-to-medium-sized data

Answer: D) Non-sensitive, small-to-medium-sized data

Explanation: localStorage should be used for storing small-to-medium-sized non-sensitive data because it's persistent but has limited storage capacity (about 5MB).


29. How can you clear all data from localStorage?

  • A) localStorage.removeAll()
  • B) localStorage.clear()
  • C) localStorage.deleteAll()
  • D) localStorage.clearData()

Answer: B) localStorage.clear()

Explanation: The clear() method removes all key-value pairs from localStorage.


30. Which of the following is true about the getCurrentPosition() method in the Geolocation API?

  • A) It returns the position synchronously.
  • B) It returns the position as a promise.
  • C) It takes a callback function to handle the position or error.
  • D) It only works if the browser is connected to the internet.

Answer: C) It takes a callback function to handle the position or error.

Explanation: getCurrentPosition() takes two callback functions: one for success (position) and one for failure (error).


31. What is the default value of navigator.geolocation if Geolocation is not supported in the browser?

  • A) null
  • B) undefined
  • C) false
  • D) {}

Answer: B) undefined

Explanation: If the browser does not support the Geolocation API, navigator.geolocation will be undefined.


32. How can you send data as a JSON object in the body of a POST request using fetch()?

  • A) Set Content-Type to application/json and send a stringified object in the body.
  • B) Send the object directly without setting the Content-Type.
  • C) Send an object inside a headers property.
  • D) Use sessionStorage to store the data first.

Answer: A) Set Content-Type to application/json and send a stringified object in the body.

Explanation: When sending a JSON object in a POST request, set the Content-Type header to application/json and use JSON.stringify() to convert the object into a JSON string in the body.


33. How do you handle multiple promises in JavaScript, such as multiple fetch() requests?

  • A) Use Promise.all()
  • B) Use Promise.finally()
  • C) Use Promise.error()
  • D) Use Promise.single()

Answer: A) Use Promise.all()

Explanation: Promise.all() allows you to run multiple promises concurrently and handle their results once all promises are resolved.


34. Which of the following is a valid method to check if the browser supports Web Storage?

  • A) if (typeof localStorage !== 'undefined')
  • B) if (localStorage.supports())
  • C) if (navigator.storage.isAvailable)
  • D) if (window.localStorage)

Answer: A) if (typeof localStorage !== 'undefined')

Explanation: You can check if Web Storage is supported by verifying if localStorage is defined in the global object.


35. What is the maximum storage limit of localStorage in most modern browsers?

  • A) 1MB
  • B) 5MB
  • C) 10MB
  • D) 100MB

Answer: B) 5MB

Explanation: Most modern browsers support a maximum storage limit of around 5MB for localStorage.


36. Which of the following is a correct way to remove all entries from sessionStorage?

  • A) sessionStorage.clear()
  • B) sessionStorage.removeAll()
  • C) sessionStorage.clearAll()
  • D) sessionStorage.remove()

Answer: A) sessionStorage.clear()

Explanation: The clear() method removes all key-value pairs from sessionStorage.


37. Which of the following methods is used to store data in sessionStorage?

  • A) setItem()
  • B) storeItem()
  • C) addItem()
  • D) saveItem()

Answer: A) setItem()

Explanation: sessionStorage.setItem() is used to store data in the session storage for the current session.


38. How do you remove an item from sessionStorage?

  • A) sessionStorage.remove()
  • B) sessionStorage.removeItem()
  • C) sessionStorage.deleteItem()
  • D) sessionStorage.clearItem()

Answer: B) sessionStorage.removeItem()

Explanation: removeItem() is used to remove a specific key-value pair from sessionStorage.


39. Which method is used to handle the result of a fetch() API call in case of success?

  • A) .then()
  • B) .catch()
  • C) .success()
  • D) .error()

Answer: A) .then()

Explanation: The .then() method is used to handle the result of a successful fetch() API call.


40. What is the key advantage of using localStorage over cookies?

  • A) It has unlimited storage capacity.
  • B) It stores data in a server-side database.
  • C) It is easier to clear manually.
  • D) It allows storing larger amounts of data.

Answer: D) It allows storing larger amounts of data.

Explanation: localStorage provides more storage capacity (about 5MB) compared to cookies, which are limited to around 4KB.


These are additional questions from the total set of 100 MCQs. You can continue following this format for further questions.

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are additional MCQs (questions 41 to 60) for the topic JavaScript and Web APIs with explanations:


41. Which of the following is a correct syntax to fetch data from an API using fetch()?

  • A) fetch('/api/data')
  • B) fetch().get('/api/data')
  • C) fetch('/api/data').then(response => response.json())
  • D) fetch('/api/data').catch(error => console.log(error))

Answer: C) fetch('/api/data').then(response => response.json())

Explanation: The correct way to use fetch() is to call it with the API URL, then use .then() to handle the response. To convert the response to JSON, you need to call response.json().


42. Which method of the navigator.geolocation object allows continuous location tracking?

  • A) watchPosition()
  • B) getCurrentPosition()
  • C) trackPosition()
  • D) positionUpdate()

Answer: A) watchPosition()

Explanation: watchPosition() is used to continuously track a user's position and receive periodic updates.


43. Which of the following does the JSON.stringify() method do in JavaScript?

  • A) Converts a JavaScript object to a JSON string.
  • B) Parses a JSON string into a JavaScript object.
  • C) Converts an array into a JSON object.
  • D) Stores data in localStorage.

Answer: A) Converts a JavaScript object to a JSON string.

Explanation: JSON.stringify() converts a JavaScript object into a JSON string, which is necessary for storing or transmitting data in JSON format.


44. Which of the following is the correct way to store a number in localStorage?

  • A) localStorage.setItem('age', 25)
  • B) localStorage.setItem('age', String(25))
  • C) localStorage.setItem('age', Number(25))
  • D) localStorage.setItem('age', JSON.stringify(25))

Answer: B) localStorage.setItem('age', String(25))

Explanation: localStorage only stores strings, so the number must be converted to a string before storing.


45. How can you access the value of a specific key from localStorage?

  • A) localStorage.get('key')
  • B) localStorage.getItem('key')
  • C) localStorage.fetch('key')
  • D) localStorage.readItem('key')

Answer: B) localStorage.getItem('key')

Explanation: The getItem() method is used to retrieve the value associated with a given key from localStorage.


46. How can you check if the user has granted permission to access their geolocation?

  • A) By checking navigator.geolocation.permission
  • B) By calling navigator.permissions.query({name: 'geolocation'})
  • C) By using navigator.geolocation.permissionGranted()
  • D) By checking navigator.geolocation.status

Answer: B) By calling navigator.permissions.query({name: 'geolocation'})

Explanation: You can check geolocation permission by querying navigator.permissions.query().


47. Which of the following Web APIs can be used to store data locally on the client's browser?

  • A) Web Storage API
  • B) WebSocket API
  • C) Geolocation API
  • D) Fetch API

Answer: A) Web Storage API

Explanation: The Web Storage API, which includes localStorage and sessionStorage, is used to store data on the client's browser.


48. What happens when JSON.parse() encounters invalid JSON syntax?

  • A) It throws an error.
  • B) It returns null.
  • C) It ignores the invalid data and continues.
  • D) It returns a default value.

Answer: A) It throws an error.

Explanation: JSON.parse() will throw an error if the input string is not valid JSON.


49. How do you retrieve the geolocation of the user in JavaScript?

  • A) navigator.getLocation()
  • B) navigator.geolocation.getPosition()
  • C) navigator.geolocation.getCurrentPosition()
  • D) navigator.location.getCurrentPosition()

Answer: C) navigator.geolocation.getCurrentPosition()

Explanation: getCurrentPosition() is used to retrieve the user's current position from the geolocation object.


50. Which of the following statements about localStorage is true?

  • A) Data stored in localStorage is available only for the current session.
  • B) Data in localStorage is automatically cleared when the page is refreshed.
  • C) localStorage can store data only for the current browser tab.
  • D) Data stored in localStorage persists across sessions.

Answer: D) Data stored in localStorage persists across sessions.

Explanation: Data in localStorage is persistent, meaning it will remain even after the browser is closed and reopened.


51. What type of data can be stored in localStorage?

  • A) Only strings
  • B) Any data type
  • C) Only numbers
  • D) Only JSON objects

Answer: A) Only strings

Explanation: localStorage can only store strings. If you want to store other data types (like objects or arrays), you need to serialize them using JSON.stringify().


52. What is the maximum storage capacity of localStorage in most modern browsers?

  • A) 500KB
  • B) 1MB
  • C) 5MB
  • D) 50MB

Answer: C) 5MB

Explanation: Most modern browsers allow approximately 5MB of data to be stored in localStorage.


53. Which Web API allows you to retrieve the current geographic position of a user?

  • A) Web Storage API
  • B) Fetch API
  • C) Geolocation API
  • D) LocalStorage API

Answer: C) Geolocation API

Explanation: The Geolocation API is used to retrieve the current geographic position of the user's device.


54. Which method in the fetch() API is used to send a POST request with JSON data?

  • A) fetch().send()
  • B) fetch().post()
  • C) fetch().then()
  • D) fetch().method()

Answer: B) fetch().post()

Explanation: fetch() can be used to send a POST request by specifying the method in the options object, like so: fetch(url, { method: 'POST', body: JSON.stringify(data) }).


55. How do you store a JavaScript object in localStorage?

  • A) localStorage.setItem('user', object)
  • B) localStorage.setItem('user', JSON.stringify(object))
  • C) localStorage.addItem('user', object)
  • D) localStorage.save('user', object)

Answer: B) localStorage.setItem('user', JSON.stringify(object))

Explanation: localStorage only stores strings, so you need to convert an object into a string using JSON.stringify() before storing it.


56. What will the geolocation API return when the getCurrentPosition() call succeeds?

  • A) A JSON object containing the latitude and longitude
  • B) A response object
  • C) A Promise
  • D) An array of coordinates

Answer: A) A JSON object containing the latitude and longitude

Explanation: getCurrentPosition() returns an object with geographic data, including latitude and longitude, on success.


57. How do you handle errors in a fetch() request?

  • A) By using .catch()
  • B) By using .error()
  • C) By using .fail()
  • D) By using .finally()

Answer: A) By using .catch()

Explanation: Errors in fetch() requests are handled by chaining a .catch() block to the promise.


58. Which of the following methods is used to clear all data from sessionStorage?

  • A) sessionStorage.clear()
  • B) sessionStorage.remove()
  • C) sessionStorage.deleteAll()
  • D) sessionStorage.removeItem()

Answer: A) sessionStorage.clear()

Explanation: sessionStorage.clear() clears all data stored in the session storage.


59. In the Geolocation API, which method is used to retrieve the position once, rather than continuously?

  • A) getCurrentPosition()
  • B) watchPosition()
  • C) trackPosition()
  • D) fetchPosition()

Answer: A) getCurrentPosition()

Explanation: getCurrentPosition() retrieves the position once, whereas watchPosition() continuously tracks position updates.


60. Which of the following is true about sessionStorage?

  • A) It persists across sessions.
  • B) It persists only for the duration of the page session.
  • C) It can store more data than localStorage.
  • D) It is accessible across all tabs in the browser.

Answer: B) It persists only for the duration of the page session.

Explanation: sessionStorage stores data only for the duration of the page session, meaning it is cleared when the page is closed.


Feel free to ask for more questions if needed!

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are additional MCQs (questions 61 to 80) for the topic JavaScript and Web APIs with explanations:


61. Which of the following is used to send a GET request using the fetch() API?

  • A) fetch(url, {method: 'POST'})
  • B) fetch(url, {method: 'GET'})
  • C) fetch(url, {method: 'FETCH'})
  • D) fetch(url, {method: 'PUT'})

Answer: B) fetch(url, {method: 'GET'})

Explanation: The default method for fetch() is GET. However, specifying { method: 'GET' } explicitly defines the request type.


62. How can you handle an error when calling the fetch() API?

  • A) By chaining .catch() to the promise.
  • B) By using try-catch blocks.
  • C) By using .error().
  • D) By setting the error handler in the API.

Answer: A) By chaining .catch() to the promise.

Explanation: Errors in fetch() requests can be handled by chaining a .catch() method to the promise.


63. What does the response.json() method do in the fetch() API?

  • A) It sends a JSON response.
  • B) It parses a JSON-formatted string into a JavaScript object.
  • C) It converts a JavaScript object into a JSON string.
  • D) It writes data into the JSON file.

Answer: B) It parses a JSON-formatted string into a JavaScript object.

Explanation: response.json() is used to parse the JSON string received from the API into a JavaScript object.


64. Which of the following Web APIs can be used to store a user's key-value pairs on the browser side?

  • A) Web Storage API
  • B) Geolocation API
  • C) Notification API
  • D) WebSockets API

Answer: A) Web Storage API

Explanation: The Web Storage API, which includes localStorage and sessionStorage, allows storing data in key-value pairs on the browser.


65. How do you clear an individual item from localStorage?

  • A) localStorage.removeItem('key')
  • B) localStorage.clear('key')
  • C) localStorage.deleteItem('key')
  • D) localStorage.delete('key')

Answer: A) localStorage.removeItem('key')

Explanation: removeItem() is used to remove a specific key-value pair from localStorage.


66. Which method is used to retrieve data from sessionStorage?

  • A) sessionStorage.getItem('key')
  • B) sessionStorage.fetch('key')
  • C) sessionStorage.read('key')
  • D) sessionStorage.retrieve('key')

Answer: A) sessionStorage.getItem('key')

Explanation: getItem() is used to retrieve the value of a specific key from sessionStorage.


67. Which of the following methods in the Geolocation API is used to continuously track the user's location?

  • A) getCurrentPosition()
  • B) trackPosition()
  • C) watchPosition()
  • D) requestPosition()

Answer: C) watchPosition()

Explanation: watchPosition() continuously tracks the user's location and calls a callback function when the location changes.


68. What does the navigator.geolocation.getCurrentPosition() method do?

  • A) Returns the current position of the user in a JSON format.
  • B) Returns the current position as a latitude and longitude value.
  • C) Sets the current position in sessionStorage.
  • D) Continuously tracks the user’s location.

Answer: B) Returns the current position as a latitude and longitude value.

Explanation: getCurrentPosition() returns the current geographic position of the user as an object with latitude, longitude, and other details.


69. How can you remove all data stored in localStorage?

  • A) localStorage.clear()
  • B) localStorage.removeAll()
  • C) localStorage.delete()
  • D) localStorage.deleteAll()

Answer: A) localStorage.clear()

Explanation: localStorage.clear() is used to remove all key-value pairs from localStorage.


70. Which of the following is a valid use of the navigator.geolocation.watchPosition() method?

  • A) To request the user’s position once.
  • B) To track the user’s position and get updates on any changes.
  • C) To store the position in localStorage.
  • D) To fetch the position from sessionStorage.

Answer: B) To track the user’s position and get updates on any changes.

Explanation: watchPosition() continuously tracks the user’s position and provides updates when the location changes.


71. Which of the following APIs is used to store data that is only available for the duration of the page session?

  • A) Web Storage API
  • B) LocalStorage API
  • C) SessionStorage API
  • D) Cache Storage API

Answer: C) SessionStorage API

Explanation: sessionStorage stores data for the duration of the page session, and the data is cleared when the session ends.


72. What is the correct way to send a POST request using the fetch() API?

  • A) fetch(url, {method: 'POST', body: JSON.stringify(data)})
  • B) fetch(url, {method: 'GET', body: JSON.stringify(data)})
  • C) fetch(url, {method: 'POST'})
  • D) fetch(url, {body: JSON.stringify(data)})

Answer: A) fetch(url, {method: 'POST', body: JSON.stringify(data)})

Explanation: To send a POST request with data, you specify the method as POST and include the data as the body of the request. The data should be stringified using JSON.stringify().


73. What happens if JSON.parse() encounters malformed JSON?

  • A) It will ignore the malformed part of the JSON and continue.
  • B) It will return null.
  • C) It will throw a SyntaxError.
  • D) It will replace the malformed data with an empty string.

Answer: C) It will throw a SyntaxError.

Explanation: JSON.parse() will throw a SyntaxError if the JSON string is not correctly formatted.


74. Which Web API allows JavaScript to interact with the browser's location features?

  • A) Geolocation API
  • B) Web Storage API
  • C) Notification API
  • D) WebSocket API

Answer: A) Geolocation API

Explanation: The Geolocation API allows JavaScript to interact with the browser's location services, enabling the retrieval of the user's current geographic position.


75. What is the main difference between localStorage and sessionStorage?

  • A) localStorage is only available for the current tab, while sessionStorage is available across all tabs.
  • B) localStorage is cleared when the browser is closed, while sessionStorage is not.
  • C) sessionStorage persists data across browser sessions, while localStorage does not.
  • D) localStorage persists data across browser sessions, while sessionStorage does not.

Answer: D) localStorage persists data across browser sessions, while sessionStorage does not.

Explanation: localStorage persists data even after the browser is closed and reopened, whereas sessionStorage only persists data during the page session.


76. What is the return type of the fetch() API when it succeeds?

  • A) JSON object
  • B) Response object
  • C) String
  • D) Array

Answer: B) Response object

Explanation: The fetch() API returns a Response object, which contains methods to handle the response data (e.g., .json(), .text()).


77. Which of the following methods would you use to parse JSON data?

  • A) JSON.parse()
  • B) JSON.stringify()
  • C) JSON.decode()
  • D) JSON.toObject()

Answer: A) JSON.parse()

Explanation: JSON.parse() is used to parse a JSON string into a JavaScript object.


78. What is the purpose of the body property in the fetch() API options?

  • A) To define the headers for the request.
  • B) To specify the type of request (GET, POST, etc.).
  • C) To contain the data to be sent with the request.
  • D) To handle errors.

Answer: C) To contain the data to be sent with the request.

Explanation: The body property is used to send data with the request, such as in a POST request.


79. How can you check if geolocation is supported in the browser?

  • A) if ('geolocation' in navigator) {}
  • B) if (navigator.geolocation) {}
  • C) if ('navigator.geolocation' in window) {}
  • D) if (navigator.hasGeolocation) {}

Answer: A) if ('geolocation' in navigator) {}

Explanation: The Geolocation API is accessed through the navigator object. This check verifies if the browser supports geolocation.


80. Which of the following methods is used to remove all key-value pairs from localStorage?

  • A) localStorage.removeAll()
  • B) localStorage.clear()
  • C) localStorage.reset()
  • D) localStorage.delete()

Answer: B) localStorage.clear()

Explanation: clear() removes all items from localStorage, resetting it to an empty state.


Let me know if you'd like to continue or need further questions!

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are additional MCQs (questions 81 to 100) for the topic JavaScript and Web APIs with explanations:


81. Which of the following statements correctly describes the behavior of fetch() in JavaScript?

  • A) It only works with HTTP requests, not with APIs.
  • B) It returns a promise that resolves to the Response object.
  • C) It always returns the data immediately, without using a promise.
  • D) It can only be used to make GET requests.

Answer: B) It returns a promise that resolves to the Response object.

Explanation: The fetch() API is asynchronous and returns a promise that resolves to the Response object, which can be used to access the response data.


82. How would you send data in a POST request using fetch()?

  • A) Use the body property with JSON stringified data.
  • B) Use the data property with an array.
  • C) Use the method property with 'POST'.
  • D) All of the above.

Answer: D) All of the above.

Explanation: When sending data in a POST request with fetch(), you use the body property to send the data (usually stringified JSON) and the method property to specify the request type.


83. What does the JSON.stringify() method do?

  • A) It converts a JavaScript object to a JSON string.
  • B) It converts a JSON string into a JavaScript object.
  • C) It removes all key-value pairs from a JSON object.
  • D) It parses JSON data into a usable format.

Answer: A) It converts a JavaScript object to a JSON string.

Explanation: JSON.stringify() is used to convert a JavaScript object into a JSON-formatted string for sending over HTTP.


84. What happens if you call localStorage.getItem('key') for a non-existing key?

  • 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 specified key does not exist in localStorage, getItem() returns null.


85. Which of the following methods is used to retrieve data from localStorage?

  • A) getItem()
  • B) getData()
  • C) fetchData()
  • D) retrieveItem()

Answer: A) getItem()

Explanation: getItem() is used to retrieve the value associated with a specific key from localStorage.


86. What is the purpose of the navigator.geolocation.getCurrentPosition() method?

  • A) To retrieve the user’s location once.
  • B) To track the user’s location continuously.
  • C) To store the user’s location.
  • D) To get the user’s location from local storage.

Answer: A) To retrieve the user’s location once.

Explanation: getCurrentPosition() is used to retrieve the user's current position once, as opposed to continuously tracking it.


87. How do you handle multiple promises in JavaScript when using fetch()?

  • A) By using Promise.all().
  • B) By using Promise.wait().
  • C) By using Promise.each().
  • D) By using Promise.allSettled().

Answer: A) By using Promise.all().

Explanation: Promise.all() is used to handle multiple promises concurrently and waits for all of them to resolve.


88. Which of the following is the correct method to check if geolocation is supported by the browser?

  • A) if ('geolocation' in navigator) {}
  • B) if (navigator.hasGeolocation) {}
  • C) if (navigator.geolocation != null) {}
  • D) if (navigator.geolocation.supported) {}

Answer: A) if ('geolocation' in navigator) {}

Explanation: To check if geolocation is supported in the browser, you can check if geolocation exists in the navigator object.


89. What is the return type of fetch() when the request is successful?

  • A) A Response object.
  • B) A Promise.
  • C) A JSON object.
  • D) A plain String.

Answer: A) A Response object.

Explanation: fetch() returns a Response object when the request is successful, which contains methods to access the response data.


90. What does the sessionStorage object do?

  • A) Stores data across multiple browser sessions.
  • B) Stores data that is cleared when the page session ends.
  • C) Stores data only in the current tab.
  • D) Both B and C.

Answer: D) Both B and C.

Explanation: sessionStorage stores data that is cleared when the page session ends. It is only available within the current tab.


91. Which of the following can be used to store user preferences in the browser?

  • A) LocalStorage API
  • B) SessionStorage API
  • C) Cookie API
  • D) All of the above

Answer: D) All of the above

Explanation: All three — LocalStorage, SessionStorage, and Cookies — can be used to store user preferences, but LocalStorage is most commonly used for long-term storage.


92. How can you store an object in localStorage?

  • A) localStorage.setItem('user', userObject)
  • B) localStorage.store('user', userObject)
  • C) localStorage.setItem('user', JSON.parse(userObject))
  • D) localStorage.setItem('user', JSON.stringify(userObject))

Answer: D) localStorage.setItem('user', JSON.stringify(userObject))

Explanation: localStorage can only store strings. Therefore, you need to convert an object into a string using JSON.stringify() before storing it.


93. How would you stop tracking the user’s position using the Geolocation API?

  • A) geolocation.stopTracking()
  • B) geolocation.removeWatch()
  • C) geolocation.clearWatch()
  • D) geolocation.stopPosition()

Answer: C) geolocation.clearWatch()

Explanation: clearWatch() is used to stop watching the user's location after it has been tracked with watchPosition().


94. Which of the following methods can be used to parse a JSON string into a JavaScript object?

  • A) JSON.parse()
  • B) JSON.stringify()
  • C) JSON.decode()
  • D) JSON.objectify()

Answer: A) JSON.parse()

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


95. Which of the following HTTP methods is typically used to send data to an API?

  • A) GET
  • B) POST
  • C) PUT
  • D) PATCH

Answer: B) POST

Explanation: The POST method is used to send data to an API, while GET is used to retrieve data.


96. How can you check whether a key exists in localStorage?

  • A) localStorage.has('key')
  • B) localStorage.getItem('key') !== null
  • C) localStorage.check('key')
  • D) localStorage.exists('key')

Answer: B) localStorage.getItem('key') !== null

Explanation: getItem() returns null if the key does not exist. So, checking if the value returned is not null helps determine if the key exists.


97. What is the purpose of the watchPosition() method in the Geolocation API?

  • A) To retrieve the user’s location once.
  • B) To continuously watch for changes in the user’s position.
  • C) To set the user’s position.
  • D) To stop tracking the user’s position.

Answer: B) To continuously watch for changes in the user’s position.

Explanation: watchPosition() is used to continuously track the user's location and get updates when the position changes.


98. Which of the following methods is used to send data to an API using fetch()?

  • A) body
  • B) headers
  • C) method
  • D) allOfTheAbove

Answer: D) allOfTheAbove

Explanation: All of the options are used in fetch() to send data. method specifies the HTTP method (e.g., POST), body contains the data to send, and headers contains metadata about the request.


99. Which method is used to remove a specific key-value pair from localStorage?

  • A) localStorage.remove()
  • B) localStorage.removeItem()
  • C) localStorage.delete()
  • D) localStorage.deleteItem()

Answer: B) localStorage.removeItem()

Explanation: removeItem() is used to remove a specific key-value pair from localStorage.


100. Which of the following is true about the navigator.geolocation.getCurrentPosition() method?

  • A) It returns the user’s position asynchronously and triggers a callback.
  • B) It returns the user’s position synchronously.
  • C) It stores the user’s location in localStorage.
  • D) It only works for mobile devices.

Answer: A) It returns the user’s position asynchronously and triggers a callback.

Explanation: getCurrentPosition() is an asynchronous method that retrieves the user’s position and triggers a callback when the position is available.



Post a Comment

0Comments

Post a Comment (0)