JavaScript Questions and Answers – Lexical Structure

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Lexical Structure”.

1. The web development environment (JavaScript) offers which standard construct for data validation of the input entered by the user.
a) Controlled loop constructs
b) Server page access
c) Client side Event
d) Permit server-side
View Answer

Answer: a
Explanation: JavaScript provides with for, while loops and if, else, switch cases for checking the information entered by the user. Additionally, all development environments provide syntax to create and use memory variables, constants, and functions.

2. The main purpose of a “Live Wire” in NetScape is to ________
a) Create linkage between client side and server side
b) Permit server side, JavaScript code, to connect to RDBMS
c) Support only non relational database
d) To interpret JavaScript code
View Answer

Answer: b
Explanation: A Live Wire database driver also supports a number of non-relational databases.

3. The script tag must be placed in __________
a) the head tag
b) the head or body
c) the title or head
d) after the body tag
View Answer

Answer: b
Explanation: If the script tag is placed after the body tag, then, it will not be evaluated at all. Also, it is always recommended and effective to use the script snippet in the <head> tag.
advertisement
advertisement

4. A JavaScript program developed on a Unix Machine ________
a) will throw errors and exceptions
b) must be restricted to a Unix Machine only
c) will work perfectly well on a Windows Machine
d) will be displayed as a JavaScript text on the browser
View Answer

Answer: c
Explanation: JavaScript can be executed on different operating systems therefore the program developed on UNIX will work perfectly fine on windows also.

5. JavaScript is ideal to ________
a) make computations in HTML simpler
b) minimize storage requirements on the web server
c) increase the download time for the client
d) increase the loading time of the website
View Answer

Answer: b
Explanation: JavaScript helps in performing various tasks with minimum storage requirements. Therefore to minimize storage requirements, JavaScript is always a better say.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which attribute is used to specify that the script is executed when the page has finished parsing? (only for external scripts)
a) parse
b) a sync
c) defer
d) type
View Answer

Answer: c
Explanation: The defer attribute is a Boolean attribute. When present, it specifies that the script is executed when the page has finished parsing.

7. JavaScript Code can be called by using ____________
a) RMI
b) Triggering Event
c) Preprocessor
d) Function/Method
View Answer

Answer: d
Explanation: JavaScript code can be called by making a function call to the element on which JavaScript has to be run. There are many other methods like onclick, onload, and onsubmit etc.
advertisement

8. JavaScript can be written __________
a) directly into JS file and included into HTML
b) directly on the server page
c) directly into HTML pages
d) directly into the css file
View Answer

Answer: a
Explanation: JavaScript files can be saved by .JS extension and can be included in the HTML files. Script tag along with src attribute is used to include the js files.

9. Which of the following Attribute is used to include External JS code inside your HTML Document?
a) src
b) ext
c) script
d) link
View Answer

Answer: a
Explanation: Script “tag” is used to include the JavaScript code. To include external JavaScript files “src” attribute is used inside the script tag.
advertisement

10. A proper scripting language is a __________
a) High level programming language
b) Assembly level programming language
c) Machine level programming language
d) Low level programming language
View Answer

Answer: a
Explanation: JavaScript is a high-level programming language that is interpreted by another program at runtime rather than compiled by the computer’s processor. Scripting languages, which can be embedded within HTML, commonly are used to add functionality to a Web page, such as different menu styles or graphics displays or to serve dynamic advertisements.

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

<p id="demo"></p>
var txt1 = "good";
var txt2 = "day";
document.getElementById("demo").innerHTML = txt1 + txt2;

a) good day
b) goodday
c) error
d) undefined
View Answer

Answer: b
Explanation: The + operator acts as a concatenation operator when used with string. The new string does not have any space between the two added string.

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

<p id="demo"></p>
<script>
var x = 5;
var y = 2;
var z = x % y;
document.getElementById("demo").innerHTML = z;
</script>

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

Answer: b
Explanation: The % operator returns the remainder between the two numbers. It is used many times with if condition to check whether the number is divisible or not.

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

<p id="demo"></p>
<script>
var x = 10;
x *= 5;
document.getElementById("demo").innerHTML = x;
</script>

a) 5
b) 10
c) 50
d) Error
View Answer

Answer: c
Explanation: The *= operator is a shorthand expression for multiplication of a particular number. It is a combination of two operators * and = .

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

<p id="demo"></p>
<script>
txt1 = “ one”;
txt1 += “two”;
document.getElementById("demo").innerHTML = txt1;
</script>

a) onetwo
b) one two
c) error
d) undefined
View Answer

Answer: a
Explanation: The += operator acts in the same way as the concatenation operator in the string. There is no space added when two string are added together with += operator.

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

<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = typeof "John" 
</script>

a) integer
b) number
c) string
d) error
View Answer

Answer: c
Explanation: The typeof operator returns the type of the argument passed to it. The typeof operator returns number for an integer and string for a character array.

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.