JavaScript Questions and Answers Part-1

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

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.

2. What will be the output of the following JavaScript code?
<p id="demo"></p>
var x = 10;
x *= 5;
document.getElementById("demo").innerHTML = x;
a) 10
b) 5
c) 50
d) Error

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

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

Answer: c
Explanation: JavaScript helps in performing various tasks with minimum storage requirements. Therefore to minimize storage requirements, JavaScript is always a better say.

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

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.

5. What will be the output of the following JavaScript code?
<p id="demo"></p>
txt1 = “ one”;
txt1 += “two”;
document.getElementById("demo").innerHTML = txt1;
a) onetwo
b) error
c) one two
d) undefined

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.

6. JavaScript Code can be called by using ___________
a) Triggering Event
b) Preprocessor
c) RMI
d) Function/Method

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.

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

Answer: c
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.

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

Answer: d
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.

9. Which of the following is not considered as an error in JavaScript?
a) Missing of Bracket
b) Syntax error
c) Division by zero
d) Missing of semicolons

Answer: c
Explanation: Division by zero is not an error in JavaScript: it simply returns infinity or negative infinity.

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

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