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
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
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
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).
4. Which property is used to obtain browser vendor and version information?
a) modal
b) version
c) browser
d) navigator
View Answer
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
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.
6. The setTimeout() belongs to which object?
a) Element
b) Window
c) Location
d) Event
View Answer
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
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.
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
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
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.
10. What is the result of the following code snippet?
window.location === document.location
a) False
b) True
c) 0
d) 1
View Answer
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
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
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
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
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
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]
- Check Programming Books
- Practice Programming MCQs
- Practice MCA MCQs
- Practice Information Science MCQs
- Apply for Computer Science Internship