JavaScript Questions & Answers – The Window Object

«
»

This set of JavaScript Multiple Choice Questions & Answers (MCQs) focuses on “The Window Object”.

1. The URL property belongs to which of the following object?
a) Document
b) Element
c) Location
d) Event
View Answer

Answer: a
Explanation: The Document object has a URL property, which is a static string that holds the URL of the document when it was first loaded. If you want to access any element in an HTML page, you always start with accessing the document object.
advertisement

2. What does the location property represent?
a) Current DOM object
b) Current URL
c) Both DOM object and URL
d) Document
View Answer

Answer: b
Explanation: The location property of a window is a reference to a Location object; it represents the current URL of the document being displayed in that window.

3. Which among the following is not a property of the Location object?
a) protocol
b) host
c) hostee
d) hostname
View Answer

Answer: c
Explanation: The location object is part of the window object and is accessed through the window.location property. The various properties of the location object are the protocol, host, hostname, port, search, and hash.
advertisement
advertisement

4. What is the return type of the hash property?
a) Query string
b) Packets
c) String
d) Fragment identifier
View Answer

Answer: d
Explanation: The hash property sets or returns the anchor part of a URL. The hash property returns the “fragment identifier” portion of the URL if there is one a hash mark (#) followed by an element ID. It is accessed by using the statement location.hash.

5. What is the function used to extract arguments from the search property of a URL?
a) urlArgs()
b) url()
c) hash()
d) geturl()
View Answer

Answer: a
Explanation: The urgArgs() function can be used to extract arguments from the search property of a URL. Search property can be accessed through the location object.
advertisement

6. The decodeURIComponent() is defined by _____________
a) Server-side JavaScript
b) Client-side JavaScript
c) Both Server-side and Client-side JavaScript
d) Service side JavaScript
View Answer

Answer: b
Explanation: The decodeURIComponent() is a global function defined by client-side JavaScript. The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.

7. Which is the method that removes the current document from the browsing history before loading the new document?
a) modify()
b) assign()
c) replace()
d) remove()
View Answer

Answer: c
Explanation: The replace() removes the current document from the browsing history before loading the new document. The difference between assign method and replace(), is that replace() removes the URL of the current document from the document history, meaning that it is not possible to use the “back” button to navigate back to the original document.
advertisement

8. Why is the replace() method better than the assign() method?
a) Reliable
b) Highly manageable
c) More efficient
d) Handles unconditional loading
View Answer

Answer: d
Explanation: The difference between assign method and replace(), is that replace() removes the URL of the current document from the document history, meaning that it is not possible to use the “back” button to navigate back to the original document. When a script unconditionally loads a new document, the replace() method is often a better choice than assign().

9. What is the purpose of the assign() method?
a) Only loading
b) Loading of window and display
c) Displays already present window
d) Unloading of window
View Answer

Answer: b
Explanation: The assign() method of the Location object makes the window load and display the document at the URL you specify. The difference between this method and replace(), is that replace() removes the URL of the current document from the document history, meaning that it is not possible to use the “back” button to navigate back to the original document.
advertisement

10.The history property belongs to which object?
a) Element
b) Window
c) History
d) Location
View Answer

Answer: c
Explanation: The history property of the Window object refers to the History object for the window. The history object is part of the window object and is accessed through the window.history property.

11. What will be the output of the following JavaScript code?

<p>1</p>
<p>2</p>
<p>3</p>
function myFunction() 
{
    var x = document.getElementsByTagName("P").item(0);
    alert(x.innerHTML);
}

a) 1
b) 2
c) 3
d) Error
View Answer

Answer: a
Explanation: Item object Returns the element at the specified index in an HTMLCollection. The function gets the HTML content of the first <p> element of the document.

12. What will be the output of the following JavaScript code?

<p>1</p>
<p>2</p>
<p>3</p>
function myFunction() 
{
   var l = document.getElementsByTagName("P").length;
   alert(l);
}

a) 1
b) 2
c) 3
d) Error
View Answer

Answer: c
Explanation: The length property returns the number of elements in a HTMLCollection. This element is useful when you want to loop through a HTMLCollection.

13. What will be the output of the following JavaScript code?

<p id="Element">Head</p>
function myFunction() 
{
   var x = document.getElementsByTagName("P").namedItem("Element");
   alert(x.innerHTML);
}

a) Head
b) Element
c) Error
d) Undefined
View Answer

Answer: a
Explanation: The namedItem() method returns the element with the specified ID, or name, in an HTMLCollection. A shorthand method can also be used, and will produce the same result.

14. What will be the output of the following JavaScript code?

<p id="demo">head</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() 
{
   var x = document.getElementById("demo");
   x.innerHTML = x.attributes[0].isId;
}
</script>

a) head
b) true
c) false
d) 0
View Answer

Answer: b
Explanation: The isId property returns true if the attribute is of type ID, otherwise it returns false. It is found in the DOM attribute object.

15. What will be the output of the following JavaScript code?

<head id="myHead">
  <title>My title</title>
</head>
<p id="demo"></p>
<script>
function myFunction() 
{
   var x = document.head.id;
   document.getElementById("demo").innerHTML = x;
}
</script>

a) demo
b) myHead
c) error
d) undefined
View Answer

Answer: a
Explanation: The head property returns the <head> element of the current document. The id property returns the id of the head.

Sanfoundry Global Education & Learning Series – Javascript Programming.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter