JavaScript Questions & Answers – JavaScript in Web Browsers – II

This set of JavaScript Interview Questions and Answers focuses on “JavaScript in Web Browsers – II”.

1. What is the property to access the first child of a node?
a) timestamp.Child1
b) timestamp.Child(1)
c) timestamp.Child(0)
d) timestamp.firstChild
View Answer

Answer: d
Explanation: The first child of a node can be accessed using the firstChild property. firstChild returns the first child node as an element node, a text node or a comment node (depending on which one’s first).

2. What are the properties supporting CSS styles for a document element?
a) style and font
b) style and className
c) size and style
d) className and font
View Answer

Answer: b
Explanation: Each Element object has style and className properties that allow scripts to specify CSS styles for a document element or to alter the CSS class names that apply to the element. firstChild returns the first child node as an element node, a text node or a comment node (depending on which one’s first).

3. Which of the following object belongs to the style property?
a) Element
b) Window
c) Location
d) Dynamic
View Answer

Answer: a
Explanation: Each Element object has style and className properties that allow scripts to specify CSS styles for a document element or to alter the CSS class names that apply to the element.
advertisement
advertisement

4. What is the purpose of the event handlers in the JavaScript?
a) Adds innerHTML page to the code
b) Performs handling of exceptions and occurrences
c) Allows JavaScript code to alter the behaviour of windows
d) Change the server location
View Answer

Answer: c
Explanation: Event handlers allow JavaScript code to alter the behavior of windows, of documents, and of the elements that make up those documents.

5. Which handler is triggered when the content of the document in the window is stable and ready for manipulation?
a) onload
b) manipulate
c) create
d) onkeypress
View Answer

Answer: a
Explanation: One of the most important event handlers is the onload handler of the Window object. It is triggered when the content of the document displayed in the window is stable and ready to be manipulated. JavaScript code is commonly wrapped within an onload event handler.

6. When a program contains extensive use of event handlers, which of the following is necessary?
a) Modular functions
b) Nested functions
c) Split up programs
d) Global variables
View Answer

Answer: b
Explanation: Nested functions are those functions in which one function is defined inside another function. Nested functions are common in client-side JavaScript, because of its extensive use of event handlers.

7. What is the JavaScript code snippet to find all container elements with class “reveal”?
a) var elements = document.getElementsByClassName(“reveal”);
b) var elements = document.getElementByClassName(“reveal”);
c) var elements = document.getElementByName(“reveal”);
d) var elements = document.getElementsClassName(“reveal”);
View Answer

Answer: a
Explanation: The getElementsByClassName() method returns a collection of all elements in the document with the specified class name, as a NodeList object. The above code snippet finds all container elements with class “reveal”.
advertisement

8. What is the JavaScript code snippet to update the content of the timestamp element when the user clicks on it?
a) timestamp.onLoad = function() { this.innerHTML = new Date().toString(); }
b) timestamp.onclick = function() { this.innerHTML = new Date().toString(); }
c) timestamp.onload = function() { this.innerHTML = new Date().toString(); }
d) timestamp.onclick = function() { innerHTML = new Date().toString(); }
View Answer

Answer: b
Explanation: onclick() function is used to handle events when the user clicks on the mouse. The above code snippet updates the content of the timestamp element when the user clicks on it.

9. Which of the following is not an object?
a) Element
b) Location
c) Position
d) Window
View Answer

Answer: c
Explanation: There is no object called Position. Whereas elements, location and window are a predefined object in JavaScript.
advertisement

10. What is the JavaScript code snippet to change the class and let the stylesheet specify the details?
a) timestamp.className = “highlight”;
b) timestamp.className = “change”;
c) timestamp.className = “specify”;
d) timestamp.className = “move”;
View Answer

Answer: a
Explanation: Each Element object has style and className properties that allow scripts to specify CSS styles for a document element or to alter the CSS class names that apply to the element. The above code snippet changes the class and lets the stylesheet specify the details.

Sanfoundry Global Education & Learning Series – Javascript Programming.

To practice all areas of JavaScript for Interviews, here is complete set of 1000+ Multiple Choice Questions and Answers on Javascript.

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.