Practice Quiz

JavaScript and the DOM

Instructions

This practice quiz is posted to help you prepare for the final exam. The questions here are similar to those that will be on the exam. (Some of them may actually be on the exam.) Mark your selections using the radio buttons. Answers are shown by clicking the button at the bottom of the page.

  1. Which statement about the Document Object Model (DOM) is false?
    1. A tag and the HTML it encloses is one example of a DOM node.
    2. Every node in the DOM, including text nodes, may have child nodes.
    3. The DOM regards the document as a hierarchical tree of objects.
    4. Each node of the DOM is an object with attributes and methods.
  2. When scripts are placed in the head section of a document, it is common practice to place statements attaching event handlers to document elements inside an event handler for window.onload. The reason is
    1. this is the only way parameters can be passed to the event handler.
    2. this is necessary in order for the page to pass the W3C validator.
    3. although not necessary, it is considered good coding style.
    4. the document elements do not exist at the time the code is read and executed by the browser.
  3. Suppose a div element in a document has id="answerbox". If a JavaScript function sets d=document.getElementById("answerbox"), then which of the following is the preferred (best style) way to add a paragraph containing the word “Hello” as a child of that div?
    1. d.innerHTML = "<p>Hello</p>";
    2. p = createElement("p");
         p.innerHTML = "Hello";
         d.appendChild(p);
    3. answerbox.innerHTML = "Hello";
    4. d.appendChild("<p>Hello</p>");
  4. Which statement about the name and id attributes of form fields is false?
    1. the id attribute is what is sent when the form is submitted.
    2. the name attribute can be used to access the field using getElementsByName().
    3. it is customary to give form fields both attributes, with the same value if possible.
    4. either attribute may be omitted if it is unused.
  5. Which statement is false?
    1. CSS style rules can be limited to apply to an element with a specific id.
    2. Two different elements of a page may have the same name attribute.
    3. Two different elements of a page may have the same id attribute.
    4. Elements do not need to be part of a form in order to have a name attribute.
  6. Suppose an element of a document containing some text is referenced in JavaScript using a variable e. Which of the following JavaScript statements would cause the text to be bold-faced?
    1. e.style.font-weight : bold;
    2. e.font-weight : bold;
    3. e.fontWeight = "bold";
    4. e.style.fontWeight = "bold";