JavaScript MCQs – Browser APIs and Web Development

Browser APIs play a crucial role in creating dynamic, user-friendly web applications in JavaScript. They provide built-in tools to interact with the browser, manipulate the DOM, handle events, work with multimedia, and use advanced features like geolocation and local storage. Understanding these APIs is essential for modern web development and interview preparation, as questions often focus on their usage and behavior.

1.) What does the document.querySelector() method do?

A) Selects the first matching element.
B) Selects all matching elements.
C) Creates a new DOM element.
D) Deletes an element from the DOM.

Answer: Option A

Explanation: The querySelector() method selects the first element that matches the specified CSS selector.

2.) Which method is used to store data in the browser’s local storage?

A) sessionStorage.setItem()
B) storeData()
C) browserData.set()
D) localStorage.setItem()

Answer: Option A

Explanation: The localStorage.setItem() method is used to store data in the browser’s local storage, which persists even after the browser is closed.

3.) How can you detect when the DOM is fully loaded?

A) Using the load event on the window object.
B) Using the DOMContentLoaded event on the document object.
C) Using the onready event.
D) Using the readyState property.

Answer: Option B

Explanation: The DOMContentLoaded event triggers when the DOM is fully loaded, without waiting for stylesheets and images to load.

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

A) Retrieves the user’s current location.
B) Sets the user’s location.
C) Fetches the current time.
D) Updates the user’s address.

Answer: Option A

Explanation: This method retrieves the user’s current geographic position using the Geolocation API.

5.) What is the purpose of the fetch() API?

A) To fetch local files.
B) To make HTTP requests.
C) To fetch browser settings.
D) To fetch cookies.

Answer: Option B

Explanation: The fetch() API is used to make HTTP requests and handle responses.

6.) Which method is used to add an event listener to an element?

A) element.onEvent()
B) element.attachEvent()
C) element.addEventListener()
D) element.bindEvent()

Answer: Option C

Explanation: The addEventListener() method attaches an event handler to an element.

7.) How can you remove an item from localStorage?

A) localStorage.removeItem()
B) localStorage.clearItem()
C) localStorage.delete()
D) localStorage.eraseItem()

Answer: Option A

Explanation: The removeItem() method deletes a specific item from localStorage.

8.) What does the history.pushState() method do?

A) Redirects to a new URL.
B) Fetches the current page title.
C) Clears the browser history.
D) Modifies the browser history without reloading the page.

Answer: Option D

Explanation: The pushState() method adds a new entry to the browser’s history stack without causing a page reload.

9.) Which method is used to parse JSON data in JavaScript?

A) JSON.stringify()
B) JSON.parse()
C) JSON.read()
D) JSON.convert()

Answer: Option B

Explanation: The JSON.parse() method converts a JSON string into a JavaScript object.

10.) How can you detect if cookies are enabled in the browser?

A) Check the navigator.cookieEnabled property.
B) Use the document.cookies property.
C) Call checkCookies() method.
D) Access the cookieStore API.

Answer: Option A

Explanation: The navigator.cookieEnabled property returns true if cookies are enabled.

Leave a Reply

Your email address will not be published. Required fields are marked *