This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Embedding JavaScript in HTML”.
1. Which of the following is a way of embedding Client-side JavaScript code within HTML documents?
a) From javascript:encoding
b) External file specified by the src attribute of a “script” tag
c) By using a header tag
d) By using body tag
View Answer
Explanation: The Client-side JavaScript code is embedded within HTML documents in four ways :
- Inline, between a pair of “script” tags
- From an external file specified by the src attribute of a “script” tag
- In an HTML event handler attribute, such as onclick or onmouseover
- In a URL that uses the special javascript: protocol.
2. When does JavaScript code appear inline within an HTML file?
a) Between the “script” tag
b) Outside the “script” tag
c) Between or Outside the “script” tag
d) Between the header tag
View Answer
Explanation: JavaScript code can appear inline within an HTML file between the “script” tags. Javascript can also be included from an external file specified by the src attribute of a “script” tag.
3. Which character in JavaScript code will be interpreted as XML markup?
a) !
b) >
c) &
d) .
View Answer
Explanation: If your JavaScript code contains the < or & characters, these characters are interpreted as XML markup. Element tags must begin with the < character, and entities and character references in an XML document must begin with the & character.
4. Which is the root element in a HTML document?
a) HTML
b) HEAD
c) SCRIPT
d) BODY
View Answer
Explanation: The “html” tag is the root element of any HTML document regardless of it containing a JavaScript code or not. Body tag includes the main content that is shown on the website.
5. What is the code for getting the current time?
a) now = new Date();
b) var now = new Date();
c) var now = Date();
d) var now = new Date(current);
View Answer
Explanation: Date() is a predefined function in javascript which returns the date in string form. The above code determines the current time and stores it in the variable “now”.
6. What is the code to start displaying the time when the document loads?
a) onload = displayTime;
b) window. = displayTime;
c) window.onload = displayTime;
d) window.onload = start;
View Answer
Explanation: window.onload is used to access the screen while the page is loading. The above code starts displaying the time when the document loads.
7. One of the main advantage of using src attribute is ____________
a) It becomes self-cached
b) It makes the HTML file modular
c) It restricts manipulation in the HTML file
d) It simplifies the HTML files
View Answer
Explanation: The main advantage of using the src attribute is that it simplifies your HTML files by allowing you to remove large blocks of JavaScript code from them. Hence separate files for css and javascript files are made to make the code modular and readable.
8. What will be done if more than one page requires a file of JavaScript code?
a) Downloads that many times
b) Retrives from the browser cache
c) Must be re executed
d) Must be included in all the pages
View Answer
Explanation: If a file of JavaScript code is shared by more than one page, it only needs to be downloaded once, by the first page that uses it—subsequent pages can retrieve it from the browser cache. This makes the loading process easier and hence faster.
9. What is the default value of the type attribute?
a) text/css
b) text/javascript
c) html
d) xml
View Answer
Explanation: The default value of the type attribute is “text/javascript”. You can specify this type explicitly if you want, but it is never necessary.
10. The language is commonly used to __________________
a) Specify the user’s language
b) Specify the language going to be scripted
c) No longer in use
d) Specify the programmer’s favourable language
View Answer
Explanation: The language attribute specifies the natural language of the content of a web page. The language attribute is deprecated and should no longer be used.
11. What will be the output of the following JavaScript code?
<p id="demo"></p> <script> var numbers1 = [4, 9]; var numbers2 = numbers1.map(myFunction); document.getElementById("demo").innerHTML = numbers2; function myFunction(value, index, array) { return value * 2; } </script>
a) 8 9
b) 8 18
c) 4 9
d) Error
View Answer
Explanation: Map function creates a new array by performing a function on each array element. myFunction multiplies each value with 2.
12. What will be the output of the following JavaScript code?
<p id="demo"></p> <script> var numbers = [45, 4, 9, 16, 25]; var ans = numbers.reduce(myFunction); document.getElementById("demo").innerHTML = sum; function myFunction(total, value, index, array) { return total + value; } </script>
a) 100
b) 99
c) 0
d) error
View Answer
Explanation: Reduce function reduces the array values to a single variable. The function returns the sum of array elements.
13. What will be the output of the following JavaScript code?
<p id="demo"></p> <script> var numbers = [45, 4, 9, 16, 25]; var arr= numbers.every(myFunction); document.getElementById("demo").innerHTML =arr; function myFunction(value, index, array) { return value > 18; } </script>
a) true
b) false
c) 0
d) error
View Answer
Explanation: The every() method checks if all array values pass a test. The function tests if all the values are greater than 18 or not.
14. What will be the output of the following JavaScript code?
<p id="demo"></p> <script> var numbers = [45, 4, 9, 16, 25]; var someOver18 = numbers.some(myFunction); document.getElementById("demo").innerHTML = "Some over 18 is " + someOver18; function myFunction(value, index, array) { return value > 10; } </script>
a) True
b) False
c) Error
d) Undefined
View Answer
Explanation: The some() method checks if some array values pass a test. Since some of the values are greater than 10 the answer will be true.
15. What will be the output of the following JavaScript code?
<p id="demo"></p> <script> var arr = ["1", "1", "2", "1"]; var a = arr.lastIndexOf("1"); document.getElementById("demo").innerHTML = (a + 1); </script>
a) 2
b) 3
c) 4
d) 0
View Answer
Explanation: The lastindexof method returns the last occurrence of element in the array. The last occurrence of 1 is at index 4.
Sanfoundry Global Education & Learning Series – Javascript Programming.
- Practice MCA MCQs
- Practice Programming MCQs
- Check JavaScript Books
- Practice Information Science MCQs
- Apply for Computer Science Internship