JavaScript Questions & Answers – The Document Object Model

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “The Document Object Model”.

1. Which function among the following lets to register a function to be invoked once?
a) setTimeout()
b) setTotaltime()
c) setInterval()
d) settime()
View Answer

Answer: a
Explanation: setTimeout() and setInterval() allow you to register a function to be invoked once or repeatedly after a specified amount of time has elapsed. Both these function are used to do time manipulation in javascript.

2. Which function among the following lets to register a function to be invoked repeatedly after a certain time?
a) setTimeout()
b) setTotaltime()
c) setInterval()
d) settime()
View Answer

Answer: c
Explanation: setTimeout() and setInterval() allow you to register a function to be invoked once or repeatedly after a specified amount of time has elapsed. Both these function are used to do time manipulation in javascript.

3. Which is the handler method used to invoke when uncaught JavaScript exceptions occur?
a) Onhalt
b) Onerror
c) Both onhalt and onerror
d) Onsuspend
View Answer

Answer: b
Explanation: The onerror handler method can be registered to be invoked when uncaught JavaScript exceptions occur. The onerror event is triggered if an error occurs while loading an external file (e.g. a document or an image).
advertisement
advertisement

4. Which property is used to obtain browser vendor and version information?
a) modal
b) version
c) browser
d) navigator
View Answer

Answer: d
Explanation: The navigator property is used to obtain browser vendor and version information. Various navaigator property includes appname, appversion, geolocation, language etc.

5. Which method receives the return value of setInterval() to cancel future invocations?
a) clearInvocation()
b) cancelInvocation()
c) clearInterval()
d) clear()
View Answer

Answer: c
Explanation: Like setTimeout(), setInterval() returns a value that can be passed to clearInterval() to cancel any future invocations of the scheduled function. The ID value returned by setInterval() is used as the parameter for the clearInterval() method.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. The setTimeout() belongs to which object?
a) Element
b) Window
c) Location
d) Event
View Answer

Answer: b
Explanation: The setTimeout() method of the Window object schedules a function to run after a specified number of milliseconds elapses. setTimeout() and setInterval() are used for time manipulations in javascript.

7. Which method receives the return value of setTimeout() to cancel future invocations?
a) clearTimeout()
b) clearInterval()
c) clearSchedule()
d) cancelInvocation()
View Answer

Answer: a
Explanation: setTimeout() returns a value that can be passed to clearTimeout() to cancel the execution of the scheduled function. The ID value returned by setTimeout() is used as the parameter for the clearTimeout() method.
advertisement

8. What will happen if we call setTimeout() with a time of 0 ms?
a) Placed in stack
b) Placed in queue
c) Will run continuously
d) Will execute immediately
View Answer

Answer: b
Explanation: If you call setTimeout() with a time of 0 ms, the function you specify is not invoked right away. Instead, it is placed on a queue to be invoked “as soon as possible” after any currently pending event handlers finish running.

9. To which object does the location property belong?
a) Window
b) Position
c) Element
d) Location
View Answer

Answer: a
Explanation: The location property of the Window object refers to a Location object, which represents the current URL of the document displayed in the window. The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.
advertisement

10. What is the result of the following code snippet?

window.location === document.location

a) False
b) True
c) 0
d) 1
View Answer

Answer: b
Explanation: The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page. The Document.location read-only property returns a Location object, which contains information about the URL of the document and provides methods for changing that URL and loading another URL.

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

function getcube()
{  
    var number=document.getElementById("number").value;  
    alert(number*number*number);  
}
<form>  
     Enter No:<input type="text" id="number" value="3" name="number"/><br/>  
     <input type="button"  value="ok" onclick="getcube()"/>  
</form>

a) 9
b) 27
c) Error
d) Undefined
View Answer

Answer: b
Explanation: The document.getElementById() is used to get value of the input text. But we need to define id for the input field.

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

function totalelements()  
{  
    var allgenders=document.getElementsByName("gender");  
    alert("Total Genders:"+allgenders.length);  
}  
<form>  
     <input type="radio" name="gender" value="male">  
     <input type="radio" name="gender" value="female">  
     <input type="button" onclick="totalelements()" value="Total Genders">  
</form>

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

Answer: c
Explanation: The document.getElementsByName() method returns all the element of specified name. The above code counts the total number of output mentioned in the form.

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

function counth2()
{  
    var totalh2=document.getElementsByTagName("h2");  
    alert("totalh2.length);  
}  
<h2>hello</h2>  
<h2>hello</h2>

a) 0
b) hello
c) h2
d) 2
View Answer

Answer: d
Explanation: The document.getElementsByTagName() method returns all the element of specified tag name. The above code counts the total number of specific tags.

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

function validate() 
{  
    var msg;  
    if(document.myForm.userPass.value.length>5)
    {  
        msg="good";  
    }  
    else
    {  
         msg="poor";  
    }  
    document.getElementById('mylocation').innerText=msg;  
}  
<form name="myForm">  
<input type="password" value="rhuld"  onkeyup="validate()">  
Strength:<span id="mylocation">no strength</span>  
</form>

a) Strength: good
b) Strength: poor
c) Strength: no strength
d) Undefined
View Answer

Answer: b
Explanation: The innerText property can be used to write the dynamic text on the html document. It is used mostly in the web pages to generate dynamic content such as writing the validation message, password strength etc.

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

function showcommentform() 
{  
    var data=new text”  
    document.getElementById('mylocation').innerHTML=data;  
}  
<form name="myForm">  
<input type="button" value="comment" onclick="showcommentform()">  
<div id="mylocation"></div> 
</form>

a) Comment
b) new text
c) Error
d) Undefined
View Answer

Answer: a
Explanation: The innerHTML property can be used to write the dynamic html on the html document. It is used mostly in the web pages to generate dynamic html such as registration form, comment form, links etc.

Sanfoundry Global Education & Learning Series – Javascript Programming.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.