0% found this document useful (0 votes)
29 views18 pages

Cs311 Midterm File

Uploaded by

afzaljangsher8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views18 pages

Cs311 Midterm File

Uploaded by

afzaljangsher8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Made by Umaid Ali

CS311 Midterm Objective and Subjective


Preparation

1. What is XML?
o a) A programming language
o b) A markup language
o c) A database
o d) A compiler
Answer: b) A markup language
2. Which attribute is used to define the version of an XML document?
o a) xmlversion
o b) version
o c) encoding
o d) docversion
Answer: b) version
3. What is the root of an XML document called?
o a) Header
o b) Body
o c) Root element
o d) Attribute
Answer: c) Root element
4. Which of the following statements about XML is correct?
o a) XML tags are predefined.
o b) XML tags are user-defined.
o c) XML is case-insensitive.
o d) XML does not allow attributes.
Answer: b) XML tags are user-defined.
5. What does the "DOM" in XML DOM stand for?
o a) Document Object Model
o b) Data Organization Model
o c) Document Optimization Model
o d) Data Object Management
Answer: a) Document Object Model
6. What type of structure does XML DOM represent?
o a) Linear
o b) Tree
o c) Circular
o d) Graph
Answer: b) Tree
7. Which method in the XML DOM is used to retrieve an element by its ID?
o a) getElementById
o b) getNodeById
o c) querySelector
o d) getElementByName
Answer: a) getElementById
8. What does a "node" represent in the XML DOM?
o a) A single character in the XML document
o b) An individual part of the XML tree
o c) A root element
o d) A namespace
Answer: b) An individual part of the XML tree
9. What is a child node in XML DOM?
o a) The root node
o b) A node that is directly connected to a parent node
o c) The last node in the tree
o d) An attribute of the root
Answer: b) A node that is directly connected to a parent node
10. Which method is used to create a new element in XML DOM?
o a) createElement
o b) newElement
o c) addElement
o d) generateElement
Answer: a) createElement
11. How do you add a new child node to an XML document in DOM?
o a) appendChild
o b) addNode
o c) insertNode
o d) pushChild
Answer: a) appendChild
12. Which of these is an XML DOM property?
o a) nodeName
o b) addElement
o c) createNode
o d) elementType
Answer: a) nodeName
13. What is the purpose of the getElementsByTagName method in DOM?
o a) To retrieve elements by their tag name
o b) To delete elements by their tag name
o c) To rename elements in the document
o d) To replace elements by their tag name
Answer: a) To retrieve elements by their tag name
14. Which of these methods can be used to remove a node in the DOM?
o a) removeChild
o b) deleteNode
o c) dropElement
o d) removeElement
Answer: a) removeChild
15. What does the nodeType property in XML DOM specify?
o a) The parent of the node
o b) The type of the node
o c) The attributes of the node
o d) The child nodes of the node
Answer: b) The type of the node

16. What does the firstChild property return in XML DOM?


o a) The root element
o b) The first child node of the selected node
o c) The first attribute of a node
o d) The first element in the document
Answer: b) The first child node of the selected node
17. How are attributes accessed in XML DOM?
o a) Using the getAttribute method
o b) Using the nodeValue property
o c) Using the getElementById method
o d) Using the querySelector method
Answer: a) Using the getAttribute method
18. Which method is used to set a new attribute in an XML element?
o a) setAttribute
o b) createAttribute
o c) defineAttribute
o d) addAttribute
Answer: a) setAttribute
19. What is the nextSibling property used for?
o a) To retrieve the next sibling of a node
o b) To delete the current sibling node
o c) To add a new sibling node
o d) To rename the sibling node
Answer: a) To retrieve the next sibling of a node
20. Which method is used to replace a child node in the XML DOM?
o a) replaceChild
o b) updateChild
o c) switchNode
o d) replaceNode
Answer: a) replaceChild

21. Which method in the DOM is used to find a node based on its name?
o a) getNodeByName
o b) getElementsByTagName
o c) findNode
o d) querySelectorAll
Answer: b) getElementsByTagName
22. What does the parentNode property in XML DOM return?
o a) The root node
o b) The parent node of the selected node
o c) The first sibling of the node
o d) The first child of the parent node
Answer: b) The parent node of the selected node
23. Which property of the DOM node contains the actual data?
o a) nodeValue
o b) dataContent
o c) innerText
o d) textContent
Answer: a) nodeValue
24. What is the hasChildNodes method used for in the XML DOM?
o a) To check if a node has any attributes
o b) To check if a node has any child nodes
o c) To check if a node has a parent
o d) To check if a node has siblings
Answer: b) To check if a node has any child nodes
25. Which method can clone a node in the XML DOM?
o a) cloneNode
o b) copyNode
o c) duplicateNode
o d) createClone
Answer: a) cloneNode
26. What is the output of document.documentElement in the XML DOM?
o a) The first element in the document
o b) The root element of the document
o c) The last element in the document
o d) The first child of the root element
Answer: b) The root element of the document
27. Which method is used to normalize the text content of a node in the XML DOM?
o a) normalize
o b) formatText
o c) normalizeContent
o d) textNormalize
Answer: a) normalize
28. Which of the following is NOT a node type in the XML DOM?
o a) ELEMENT_NODE
o b) ATTRIBUTE_NODE
o c) STYLE_NODE
o d) TEXT_NODE
Answer: c) STYLE_NODE
29. What is a CDATA section in XML?
o a) A section where data is compressed
o b) A section where data is ignored by the parser
o c) A section where character data is not parsed by the parser
o d) A section for defining comments
Answer: c) A section where character data is not parsed by the parser
30. How are comments defined in an XML document?
o a) <!-- Comment -->
o b) // Comment
o c) /* Comment */
o d) # Comment
Answer: a) <!-- Comment -->
31. What is the use of namespaces in XML?
o a) To avoid element name conflicts
o b) To define document encoding
o c) To organize attributes
o d) To structure data hierarchically
Answer: a) To avoid element name conflicts
32. Which prefix is used for namespaces in XML?
o a) ns
o b) xmlns
o c) xml
o d) namespace
Answer: b) xmlns
33. What does the localName property return in XML DOM?
o a) The name of the local file
o b) The local part of the element's name
o c) The namespace of the element
o d) The full name of the element
Answer: b) The local part of the element's name
34. What is an attribute in XML?
o a) A special type of node that provides additional information about an
element
o b) A child node of an element
o c) The root element of an XML document
o d) A property of the document type
Answer: a) A special type of node that provides additional information about
an element
35. Which of the following methods can be used to select nodes in an XML
document?
o a) XPath
o b) CSS selectors
o c) Query API
o d) Node.js
Answer: a) XPath
36. What does textContent in XML DOM represent?
o a) The textual content of a node and all its descendants
o b) Only the text of the root node
o c) The tag names of all child nodes
o d) The name of the document
Answer: a) The textual content of a node and all its descendants
37. Which of the following XML parsers works event-based?
o a) DOM
o b) SAX
o c) StAX
o d) Schema Validator
Answer: b) SAX
38. What is the primary advantage of SAX parsing over DOM parsing?
o a) It is faster and requires less memory
o b) It provides a visual interface for XML documents
o c) It automatically validates XML documents
o d) It supports namespaces by default
Answer: a) It is faster and requires less memory
39. What does nodeList.length return in XML DOM?
o a) The number of elements in a node
o b) The number of child nodes in a node
o c) The length of the textual content of a node
o d) The number of attributes in a node
Answer: b) The number of child nodes in a node
40. What is the purpose of innerHTML in XML DOM?
o a) To get or set the content of an element, including tags
o b) To fetch the textual content only
o c) To set the namespace of an element
o d) To define the schema
Answer: a) To get or set the content of an element, including tags

• Which method is used to check if an element has attributes in XML DOM?

• a) hasAttributes()
• b) checkAttributes()
• c) getAttributes()
• d) validateAttributes()
Answer: a) hasAttributes()

• What does the lastChild property return in the XML DOM?

• a) The last sibling of a node


• b) The last child of a node
• c) The root element of the document
• d) The previous node in the DOM tree
Answer: b) The last child of a node

• What is the purpose of the createTextNode() method in XML DOM?

• a) To create a new text node


• b) To update existing text nodes
• c) To delete text nodes
• d) To replace text nodes with elements
Answer: a) To create a new text node

• Which property is used to retrieve all child nodes of a given node in XML DOM?

• a) childNodes
• b) nodeChildren
• c) children
• d) getChildNodes()
Answer: a) childNodes

• What does the ownerDocument property return in XML DOM?

• a) The parent of the current document


• b) The entire XML document to which the node belongs
• c) The root element of the document
• d) The next sibling of the node
Answer: b) The entire XML document to which the node belongs

• Which method is used to retrieve an attribute node in XML DOM?

• a) getAttributeNode()
• b) retrieveAttribute()
• c) attributeNode()
• d) fetchAttribute()
Answer: a) getAttributeNode()

• What is the difference between innerHTML and outerHTML in XML DOM?

• a) innerHTML includes the element, outerHTML does not


• b) outerHTML includes the element, innerHTML does not
• c) Both are identical
• d) Both exclude the content of the element
Answer: b) outerHTML includes the element, innerHTML does not

• What does the document.createElement() method return?

• a) A new element node


• b) An existing element node
• c) A text node
• d) A comment node
Answer: a) A new element node

• What does the document.getElementsByTagName() method return?

• a) A NodeList of matching elements


• b) A single matching element
• c) An array of text nodes
• d) The root node of the document
Answer: a) A NodeList of matching elements

• What is the purpose of removeAttribute() in XML DOM?

• a) To delete an attribute from an element


• b) To remove an attribute node
• c) To remove a child node
• d) To replace an attribute with a new value
Answer: a) To delete an attribute from an element

• What is the primary role of textContent in XML DOM?

• a) To set or get the text content of an element


• b) To add a new attribute to a node
• c) To retrieve child nodes
• d) To define namespaces
Answer: a) To set or get the text content of an element

• Which method is used to remove a child node from the parent node?

• a) removeChild()
• b) deleteNode()
• c) eraseChild()
• d) clearNode()
Answer: a) removeChild()
• What does the nodeType property of a DOM node return?

• a) The name of the node


• b) The type of the node (e.g., element, text)
• c) The value of the node
• d) The parent node
Answer: b) The type of the node (e.g., element, text)

• What is the purpose of the setAttribute() method?

• a) To create a new attribute


• b) To assign or update the value of an attribute
• c) To delete an attribute
• d) To retrieve the value of an attribute
Answer: b) To assign or update the value of an attribute

• What happens if you call cloneNode(true)?

• a) The node is cloned without any child nodes


• b) The node is cloned with all its child nodes
• c) Only the attributes of the node are cloned
• d) The node is not cloned at all
Answer: b) The node is cloned with all its child nodes

• What does document.documentElement refer to in an XML document?

• a) The root element of the document


• b) The first child element of the document
• c) The last child element of the document
• d) The namespace of the document
Answer: a) The root element of the document

• What is the main use of the appendChild() method in XML DOM?

• a) To add a node as the last child of a parent node


• b) To remove the last child node
• c) To update the value of a node
• d) To replace an existing child node
Answer: a) To add a node as the last child of a parent node

• What does the previousSibling property return in XML DOM?

• a) The parent node of the current node


• b) The sibling node that precedes the current node
• c) The first child node
• d) The next sibling of the node
Answer: b) The sibling node that precedes the current node
• What does the getAttribute() method return if the attribute does not exist?

• a) An error
• b) null
• c) An empty string
• d) A default value
Answer: b) null

• Which of these is a valid XML DOM node type?

• a) COMMENT_NODE
• b) ATTRIBUTE_TYPE
• c) STYLE_NODE
• d) ELEMENT_TYPE
Answer: a) COMMENT_NODE

• What does the childNodes property return in XML DOM?

• a) A NodeList of all child nodes


• b) The first child node
• c) The total number of child nodes
• d) An array of child elements
Answer: a) A NodeList of all child nodes

• What is the difference between nodeValue and textContent?

• a) nodeValue is for attributes, textContent is for elements


• b) nodeValue contains raw data, textContent contains all textual content of a node
• c) Both are identical
• d) nodeValue returns HTML content, textContent returns text content
Answer: b) nodeValue contains raw data, textContent contains all textual content
of a node

• Which method is used to replace an existing node with a new node in XML DOM?

• a) replaceChild()
• b) swapNode()
• c) updateChild()
• d) changeNode()
Answer: a) replaceChild()

• What does firstElementChild return in XML DOM?

• a) The first child element node


• b) The first text node
• c) The root element
• d) The first sibling element
Answer: a) The first child element node
• Which method in XML DOM is used to select nodes by their namespace?

• a) getElementsByTagNameNS()
• b) querySelector()
• c) getNodesByNamespace()
• d) selectByNamespace()
Answer: a) getElementsByTagNameNS()

• What does ownerElement in an XML DOM attribute node represent?

• a) The element to which the attribute belongs


• b) The document owner of the attribute
• c) The parent of the attribute
• d) The root element of the document
Answer: a) The element to which the attribute belongs

• Which property of a node provides a live list of all attributes?

• a) attributes
• b) getAttributes
• c) attributeList
• d) nodeAttributes
Answer: a) attributes

• Which of the following is NOT a method of the XML DOM?

• a) getElementById()
• b) getElementsByTagName()
• c) setElementType()
• d) createElement()
Answer: c) setElementType()

• Which property is used to access the first sibling of a node in XML DOM?

• a) firstSibling
• b) previousSibling
• c) parentNode
• d) nextSibling
Answer: b) previousSibling

• What does the nodeType property return for an element node?

• a) 1
• b) 2
• c) 3
• d) 4
Answer: a) 1
• What does document.createAttribute() do in XML DOM?

• a) Creates a new attribute node


• b) Updates an existing attribute
• c) Deletes an attribute
• d) Retrieves an attribute value
Answer: a) Creates a new attribute node

• What happens when normalize() is called on an element in XML DOM?

• a) Merges adjacent text nodes into one


• b) Deletes empty nodes
• c) Converts all elements to lower case
• d) Removes all attributes from a node
Answer: a) Merges adjacent text nodes into one

• Which method removes all child nodes from a parent node in XML DOM?

• a) removeChildNodes()
• b) clearChildren()
• c) removeAllChildren()
• d) There is no direct method; it must be done manually
Answer: d) There is no direct method; it must be done manually

• What does the namespaceURI property return?

• a) The URI of the namespace of a node


• b) The name of the root element
• c) The ID of the document
• d) The schema definition of the node
Answer: a) The URI of the namespace of a node

• Which method retrieves a node by its name and namespace?

• a) getNodeByNameNS()
• b) getElementsByTagNameNS()
• c) querySelectorNS()
• d) selectByNamespace()
Answer: b) getElementsByTagNameNS()

• What is returned by document.childNodes[0]?

• a) The first child node of the document


• b) The last child node of the document
• c) The root element of the document
• d) An array of all child nodes
Answer: a) The first child node of the document
• What does the appendChild() method do?

• a) Adds a node as the last child of a parent node


• b) Replaces the last child of a node
• c) Deletes all child nodes
• d) Updates the value of a child node
Answer: a) Adds a node as the last child of a parent node

• Which property of the node returns the type of its children?

• a) nodeType
• b) childType
• c) childNodes
• d) nodeChildrenType
Answer: a) nodeType

• Which method removes an attribute node from an element?

• a) removeAttributeNode()
• b) deleteAttribute()
• c) clearAttribute()
• d) removeNode()
Answer: a) removeAttributeNode()

• Which property returns the name of a node in XML DOM?

• a) nodeName
• b) nodeType
• c) nodeValue
• d) nodeContent
Answer: a) nodeName

• What does the attributes property of a node return?

• a) A NamedNodeMap of all attributes of the node


• b) A list of child nodes
• c) A single attribute of the node
• d) The value of the first attribute
Answer: a) A NamedNodeMap of all attributes of the node

• Which of the following is NOT a valid node property in XML DOM?

• a) parentNode
• b) nodeChildren
• c) childNodes
• d) firstChild
Answer: b) nodeChildren
• What is the purpose of getElementsByClassName() in XML DOM?

• a) To retrieve elements by their class name


• b) To retrieve elements by their tag name
• c) To retrieve elements by their ID
• d) To create elements with a class
Answer: a) To retrieve elements by their class name

• Which node type does nodeType return for a text node?

• a) 1
• b) 2
• c) 3
• d) 4
Answer: c) 3

• Which property in XML DOM provides access to the last child node?

• a) lastChild
• b) endNode
• c) tailNode
• d) finalChild
Answer: a) lastChild

• What does the document object represent in XML DOM?

• a) The entire XML document


• b) The root node of the document
• c) The last node of the document
• d) The parent node of all elements
Answer: a) The entire XML document

• What does the cloneNode(false) method do?

• a) Clones the node without its child nodes


• b) Clones the node with all its attributes
• c) Deletes the cloned node
• d) Clones the node and its child nodes
Answer: a) Clones the node without its child nodes

• Which of the following is a property of a NamedNodeMap?

• a) length
• b) content
• c) attributes
• d) nodeList
Answer: a) length
• What does the document.createTextNode() method do?

• a) Creates a new text node


• b) Updates the text of an existing node
• c) Deletes text nodes
• d) Converts an element to a text node
Answer: a) Creates a new text node

• Which property of a node contains the name of its tag?

• a) nodeName
• b) tagName
• c) elementName
• d) name
Answer: a) nodeName

• Which of the following is the method to remove an attribute from an element?

• a) removeAttribute()
• b) deleteAttribute()
• c) clearAttribute()
• d) removeNodeAttribute()
Answer: a) removeAttribute()

• What is returned by the attributes property of a node?

• a) A NamedNodeMap of the node's attributes


• b) A list of child nodes
• c) A string of concatenated attribute values
• d) The first attribute of the node
Answer: a) A NamedNodeMap of the node's attributes

• What does the normalize() method do to a node?

• a) Combines adjacent text nodes


• b) Removes whitespace from the document
• c) Formats the document with proper indentation
• d) Converts text nodes to element nodes
Answer: a) Combines adjacent text nodes

• What is the difference between childNodes and children properties?

• a) childNodes includes all node types, children only includes element nodes
• b) children includes all node types, childNodes only includes element nodes
• c) Both are identical
• d) children is used for attributes, childNodes is used for elements
Answer: a) childNodes includes all node types, children only includes element
nodes
• Which method is used to check if a node has any child nodes?

• a) hasChildNodes()
• b) checkChildNodes()
• c) getChildNodes()
• d) validateChildren()
Answer: a) hasChildNodes()

• What does getElementsByTagName() return?

• a) A NodeList of matching elements


• b) A single matching element
• c) An array of tag names
• d) The first matching element
Answer: a) A NodeList of matching elements

• What does nextSibling property return?

• a) The next sibling node of the current node


• b) The parent node of the current node
• c) The last child node of the current node
• d) The root element of the document
Answer: a) The next sibling node of the current node

• What happens when you call removeChild() on a parent node?

• a) Removes a specified child node from the parent node


• b) Deletes all child nodes of the parent node
• c) Replaces the specified child node with a new one
• d) Removes the parent node itself
Answer: a) Removes a specified child node from the parent node

• What does outerHTML property return in XML DOM?

• a) The HTML including the element itself and its content


• b) Only the textual content of the element
• c) The attributes of the element
• d) The tag name of the element
Answer: a) The HTML including the element itself and its content

• What is the purpose of the getElementById() method in XML DOM?


- a) To retrieve an element by its ID
- b) To retrieve all elements by their ID
- c) To set the ID of an element
- d) To update the value of an element by its ID
Answer: a) To retrieve an element by its ID
Long Questions

1. Explain the structure of an XML document.


Answer:
o Prolog: Contains XML declaration (e.g., <?xml version="1.0"
encoding="UTF-8"?>).
o Root Element: The top-level element that contains all other elements.
o Elements: Defined by start and end tags, can have attributes.
o Text Nodes: Represent textual content.
o Comments: Written as <!-- Comment -->.
o CDATA Sections: Used for unparsed character data.
2. What are the advantages of using XML over other formats?
Answer:
o Platform-independent.
o Readable by both humans and machines.
o Supports custom-defined tags.
o Hierarchical data representation.
o Extensible and integrates well with other technologies.
3. Describe the purpose of DTDs and XML Schemas in XML.
Answer:
o DTD (Document Type Definition): Defines the structure and allowed
elements/attributes in an XML document.
o XML Schema: A more powerful alternative to DTD, supports data types and
namespaces.
4. Explain the steps to validate an XML document using an XML Schema.
Answer:
o Create an XML Schema file defining the structure and data types.
o Reference the schema in the XML file using the xsi:schemaLocation
attribute.
o Use an XML parser that supports schema validation to validate the document.
5. What is the difference between getElementById() and
getElementsByTagName()?
Answer:
o getElementById(): Returns a single element with a specific ID.
o getElementsByTagName(): Returns a NodeList of all elements with the
specified tag name.
6. Describe the role of setAttribute() and getAttribute() methods in XML
DOM.
Answer:
o setAttribute(): Adds or updates an attribute of an element.
o getAttribute(): Retrieves the value of an attribute.
7. Explain the importance of namespaces in XML.
Answer:
Namespaces prevent naming conflicts in documents that combine elements from
different vocabularies by qualifying element names with unique URIs.
8. What is the difference between DOM and SAX parsers?
Answer:
o DOM: Loads the entire document into memory, represents it as a tree.
o SAX: Processes the document sequentially, does not store it in memory.
9. How are child nodes manipulated in XML DOM?
Answer:
Use methods like appendChild(), removeChild(), replaceChild(), and properties
like childNodes.
10. Describe the role of nodeType, nodeName, and nodeValue properties in XML
DOM.
Answer:
o nodeType: Indicates the type of the node (e.g., element, attribute).
o nodeName: Provides the name of the node.
o nodeValue: Holds the value of a node (for text and attributes).
11. What are the different node types in XML DOM, and how are they used?
Answer:
o Element Node, Attribute Node, Text Node, Comment Node, Document Node,
etc.
Each node type has specific properties and methods for manipulation.
12. Explain the concept of NamedNodeMap in XML DOM.
Answer:
A collection of attribute nodes, accessed using properties like length and methods
like getNamedItem().
13. What is the purpose of cloneNode() in XML DOM?
Answer:
Creates a copy of a node. The deep parameter decides whether child nodes are also
cloned.
14. How does XML handle special characters like < and &?
Answer:
Special characters must be escaped:
o < as &lt;
o > as &gt;
o & as &amp;.
15. What are the benefits of using XML in web development?
Answer:
o Standardized data exchange.
o Seamless integration with web technologies like HTML and JavaScript.
o Flexible and extensible format for APIs.
16. Explain the purpose of the ownerDocument property in XML DOM.
Answer:
Refers to the document object that a node belongs to, allowing access to the entire
document structure.
17. Describe how to add a new attribute to an XML element using DOM.
Answer:
Use createAttribute() to create the attribute, set its value, and attach it to the
element using setAttributeNode().
18. What is the significance of innerHTML in XML DOM?
Answer:
Allows direct manipulation of the content inside an element, including child nodes
and HTML tags.
19. How does XML ensure data integrity during data transmission?
Answer:
By using validation mechanisms like DTDs and XML Schemas, ensuring the data
adheres to the defined structure.
20. What is the role of appendChild() in building XML documents dynamically?
Answer:
Adds a new child node to a parent node, allowing dynamic creation and extension of
XML structures.

You might also like