Q. Which DOM method selects an element by its ID?

  • (A) document.getElementById()
  • (B) document.querySelectorAll()
  • (C) document.getElementByClass()
  • (D) document.findById()
πŸ’¬ Discuss
βœ… Correct Answer: (A) document.getElementById()
Explanation: getElementById returns a reference to the element with specified ID.

Q. Which DOM method returns the first element that matches a selector?

  • (A) querySelector()
  • (B) querySelectorAll()
  • (C) getElementById()
  • (D) getElementsByTagName()
πŸ’¬ Discuss
βœ… Correct Answer: (A) querySelector()
Explanation: querySelector returns the first element matching a CSS selector.

Q. Which object is used to manipulate the HTML document?

  • (A) document
  • (B) window
  • (C) location
  • (D) navigator
πŸ’¬ Discuss
βœ… Correct Answer: (A) document
Explanation: The document object represents the HTML DOM and allows manipulation.

Q. What does the window object represent?

  • (A) The browser window
  • (B) The HTML page
  • (C) The document object
  • (D) The JavaScript engine
πŸ’¬ Discuss
βœ… Correct Answer: (A) The browser window
Explanation: The window object represents the browser's window containing a DOM document.

Q. Which method can be used to change the content of an HTML element?

  • (A) innerHTML
  • (B) setAttribute
  • (C) value
  • (D) classList
πŸ’¬ Discuss
βœ… Correct Answer: (A) innerHTML
Explanation: innerHTML allows you to get or set the HTML content of an element.

Q. Which method adds a new class to an element?

  • (A) classList.add()
  • (B) className.append()
  • (C) setClass()
  • (D) addClass()
πŸ’¬ Discuss
βœ… Correct Answer: (A) classList.add()
Explanation: classList.add() adds one or more classes to an element.

Q. Which method creates a new DOM element?

  • (A) document.createElement()
  • (B) document.newElement()
  • (C) element.create()
  • (D) create()
πŸ’¬ Discuss
βœ… Correct Answer: (A) document.createElement()
Explanation: document.createElement() creates an HTML element dynamically in JavaScript.

Q. Which of the following is a correct way to declare a variable in JavaScript?

  • (A) var myVar;
  • (B) int myVar;
  • (C) let: myVar;
  • (D) declare myVar;
πŸ’¬ Discuss
βœ… Correct Answer: (A) var myVar;
Explanation: The correct syntax to declare a variable is using var, let, or const. 'var myVar;' is correct.

Q. What will be the output of: console.log(typeof null);

  • (A) null
  • (B) undefined
  • (C) object
  • (D) boolean
πŸ’¬ Discuss
βœ… Correct Answer: (C) object
Explanation: In JavaScript, typeof null returns 'object' due to a legacy bug.

Q. Which of the following methods converts JSON data to a JavaScript object?

  • (A) JSON.parse()
  • (B) JSON.stringify()
  • (C) JSON.toObject()
  • (D) JSON.convert()
πŸ’¬ Discuss
βœ… Correct Answer: (A) JSON.parse()
Explanation: JSON.parse() is used to parse JSON strings and convert them into JavaScript objects.