JavaScript Multiple-Choice Questions (MCQS) and Answers
JavaScript Multiple-Choice Questions (MCQS) and Answers
Questions (MCQs)
»
1. Desktop
2. Mobile
3. Web
4. Server
Answer: C) Web
Explanation:
1. Object-oriented
2. Object-based
3. Functional programming
4. All of the above
Answer: B) Object-based
Explanation:
Explanation:
The correct statement about the JavaScript programming language is "It is
a scripting language used to make the website interactive".
1. <javascript>...</javascript>
2. <js>...</js>
3. <script>...</script>
4. <css>...</css>
Answer: C) <script>...</script>
Explanation:
Explanation:
1. Comma (,)
2. Colon (:)
:
3. Hyphen (_)
4. Semicolon (;)
Explanation:
7. JavaScript ignores?
1. newlines
2. tabs
3. spaces
4. All of the above
Explanation:
JavaScript ignores spaces, tabs, and newlines written in the code, we can
use them for the alignment and separate the sections to give a perfect look
at our code.
1. <script src="jsfile.js"></script>
2. <script href=" jsfile.js"></script>
3. <import src=" jsfile.js"></import>
4. <script link=" jsfile.js"></script>
The correct syntax to call an external JavaScript file in the current HTML
document is:
<script src="jsfile.js"></script>
1. getElementById()
2. getElement(id)
3. getElementById(id)
4. elementById(id)
Answer: C) getElementById(id)
Explanation:
1. innerText
2. innerContent
3. elementText
4. innerHTML
Answer: D) innerHTML
Explanation:
1. document.write()
2. document.output()
3. console.log()
4. document.writeHTML()
Answer: A) document.write()
Explanation:
1. console.write()
2. console.output()
3. console.log()
4. console.writeHTML()
Answer: C) console.log()
Explanation:
1. window.alertHTML()
:
2. window.alert()
3. window.alertBox()
4. window.alertContent()
Answer: B) window.alert()
Explanation:
1. alert("Hello Boss!");
2. alert('Hello Boss!');
3. alert(Text:'Hello Boss!');
4. Both A. and B.
Explanation:
The both of statement are correct to display "Hello Boss!" into an alert box:
window.alert("Hello Boss!");
window.alert('Hello Boss!');
1. getElementById("result").innerHTML = 10+20;
2. getElementById("result").innerHTML = "10+20";
:
3. getElementById("#result").innerHTML = 10+20;
4. All of the above
Explanation:
document.getElementById("result").innerHTML = 10+20;
<button onclick="window.print()">Submit</button>
Explanation:
1. #
2. /*
3. $
4. //
:
Answer: D) //
Explanation:
18. In JavaScript, multi-line comments start with __ and end with ___.
1. /* and */
2. <!—and -->
3. ## and ##
4. // and //
Answer: A) /* and */
Explanation:
1. Var
2. var
3. Let
4. All of the above
Answer: B) var
Explanation:
Answer: C) 3
Explanation:
var
let
const
21. What is the main difference between var and let keywords in
JavaScript?
Answer: B) var defined function scoped variable while let define block
scoped variable
Explanation:
The var and let keywords are both used for variable declaration in
JavaScript. But, the main difference between them is that var defines
function scoped variable while let defines block-scoped variable.
:
22. The const keyword is used to define a ______.
Answer: C) Constant
Explanation:
1. const constant_name;
2. constant_name const;
3. constant_name const = value;
4. const constant_name = value;
Explanation:
Example:
const PI = 3.14;
<script>
const VALUE = 10;
VALUE = 20;
</script>
1. 10
2. 20
3. ValueError
4. TypeError
Answer: D) TypeError
Explanation:
We cannot change the value of a constant, thus the above code will
generate a TypeError – "TypeError: Assignment to constant variable"
1. 0
2. undefined
3. null
4. NaN
Answer: B) undefined
Explanation:
<script>
var a;
document.getElementById("demo").innerHTML = a+1;
</script>
1. 0
2. undefined
3. 1
4. NaN
Answer: D) NaN
Explanation:
1. Yes
2. No
Answer: A) Yes
Explanation:
Copy
<script>
var name = "Alex" + " " + "Alvin";
:
document.getElementById("demo").innerHTML = name;
</script>
1. Alex Alvin
2. AlexAlvin
3. TypeError
4. ValueError
Explanation:
Copy
<script>
var a = 10 + 20 + "5";
document.getElementById("demo").innerHTML = a;
</script>
1. 35
2. 305
3. TypeError
4. ValueError
Answer: B) 305
Explanation:
1. Yes
2. No
Answer: B) No
Explanation:
Copy
<script>
let a = 10;
let a = 0;
</script>
1. 10
2. 0
3. SyntaxError
4. TypeError
Answer: C) SyntaxError
Explanation:
The output of the above JavaScript code is: "SyntaxError: 'a' has already
been declared".
Answer: C) **
Explanation:
1. Yes
2. No
Answer: A) Yes
Explanation:
Example:
Copy
<script>
var x = 5;
document.getElementById("test").innerHTML = ++x;
</script>
:
34. What will be the output of the following JavaScript code?
Copy
<script>
var x = 5;
document.getElementById("demo").innerHTML = x--;
</script>
1. 5
2. 4
3. TypeError
4. ValueError
Answer: B) 4
Explanation:
Copy
<script>
var x = 10 + 20 * 5;
document.getElementById("tes").innerHTML = x;
</script>
1. 110
2. 150
:
3. TypeError
4. ValueError
Answer: A) 110
Explanation:
Copy
<script>
var x = (10 + 20) * 5;
document.getElementById("tes").innerHTML = x;
</script>
1. 110
2. 150
3. TypeError
4. ValueError
Answer: B) 150
Explanation:
In the above code, the expression is (10 + 20) * 5. The precedence of () are
higher than any other operators This (10 + 20) will evaluate first.
:
37. JavaScript types are _____.
1. Static
2. Dynamic
Answer: B) Dynamic
Explanation:
JavaScript types are dynamic, which means the same variable can be used
to store the different types of values.
1. round brackets ()
2. curly brackets {}
3. double quotes ""
4. square brackets []
Explanation:
1. round brackets ()
2. curly brackets {}
3. double quotes ""
4. square brackets []
1. typeof
2. TypeOf
3. typeOf
4. sizeof
Answer: A) typeof
Explanation:
1. typeof variable/value
2. typeof(variable/value)
3. Both A. and B.
4. None of the above
Explanation:
<script>
var x = 12.34;
document.getElementById("test").innerHTML = typeof
</script>
1. int
2. float
3. long
4. number
Answer: D) number
Explanation:
1. module
2. fun
3. func
4. function
Answer: D) function
Explanation:
Explanation:
Copy
<script>
function addition(a, b) {
return a+b;
}
document.getElementById("test").innerHTML = addition;
</script>
1. SyntaxError
2. ValueError
:
3. 0
4. function addition(a, b) { return a+b; }
Explanation:
1. Yes
2. No
Answer: A) Yes
Explanation:
1. One value
2. Two values
3. Three values
4. Many values
Explanation:
1. objectName:propertyName
2. propertyName
3. objectName["propertyName"]
4. Both B. and C.
Explanation:
1. strlen
2. len
3. length
4. Length
Answer: C) length
Explanation:
Copy
<script>
:
let str = "IncludeHelp";
document.getElementById("test").innerHTML = str.length;
</script>
1. 11
2. 12
3. ValueError
4. SyntaxError
Answer: A) 11
Explanation:
The output of the above statement will be the length of the string. That is
11.
51. Which character is used to break up a code line within a text string
in JavaScript?
Explanation:
The Single backslash (\) is used to break up a code line within a text string
in JavaScript.
Example:
document.getElementById("test").innerHTML = "Hello \
:
IncludeHelp!";
Copy
<script>
document.getElementById("test").innerHTML = \
"Hello, IncludeHelp!";
</script>
1. Yes
2. No
Answer: B) No
Explanation:
No, the above code will not work. Because, we cannot breakup a JavaScript
code line with single backslash (\).
Explanation:
The strings can also be defined as an object using the new keyword. The
correct JavaScript statement to define a string as an object is:
:
var s = new String("IncludeHelp!");
Copy
<script>
let str1 = new String("IncludeHelp!");
let str2 = new String("IncludeHelp!");
document.getElementById("test").innerHTML = (str1==
</script>
1. true
2. false
3. True
4. False
Answer: B) false
Explanation:
In the above code, str1 and str2 are the objects. And. In the JavaScript,
comparison of two objects returns false.
55. Which is/are the valid JavaScript method(s) to extract string parts?
1. slice(start, end)
2. substring(start, end)
3. substr(start, length)
4. All of the above
The all of the above JavaScript methods can be used to extract string parts.
Copy
<script>
let x = "Hello, IncludeHelp!";
document.getElementById("test").innerHTML = x.slice(-13
</script>
1. IncludeHelp!
2. IncludeHelp
3. ValueError
4. Hello,
Answer: B) IncludeHelp
Explanation:
The negative value counts from the end of the string. Thus, the output will
be "IncludeHelp".
57. In JavaScript, the string template literals use ____ rather than the
quotes ("") to define a string?
In JavaScript, the string template literals use back-ticks (``) rather than the
quotes ("") to define a string.
1. Yes
2. No
Answer: A) Yes
Explanation:
1. toString()
2. intToString()
3. parseInteger()
4. All of the above
Answer: A) toString()
Explanation:
Copy
<script>
const myArray = ['h', 'e', 'l', 'l', 'o'];
document.write(myArray[0]);
document.write(myArray[1]);
</script>
1. he
2. undefinedh
3. ValueError
4. TypeError
Answer: A) he
Explanation:
In JavaScript, the array indexing starts with 0. Thus, the above statement
with print "h" and "e".
Copy
<script>
let cars = ['Honda', 'Hyundai'];
cars.push('Mahindra');
document.write(typeof cars + " " + cars);
</script>
1. array Honda,Hyundai,Mahindra
2. string Honda,Hyundai,Mahindra
:
3. object Honda,Hyundai,Mahindra
4. object "Honda", "Hyundai", "Mahindra"
Explanation:
The push() method pushes an element at the end of the array. And, typeof
returns the type of the object. Here, cars is an array.
Copy
<script>
let cars1 = ['Honda', 'Hyundai'];
let cars2 = cars1;
cars1.push('Mahinda');
1. Honda,Hyundai,Mahinda---Honda,Hyundai
2. Honda,Hyundai,Mahinda---Honda,Hyundai,Mahinda
3. Honda,Hyundai ---Honda,Hyundai
4. [Honda,Hyundai,Mahinda]---[Honda,Hyundai,Mahinda]
Answer: B) Honda,Hyundai,Mahinda---Honda,Hyundai,Mahinda
Explanation:
In the JavaScript, the arrays are objects, and the array elements are stored
by reference. Hence, when an array value is copied, any change in the
copied array will also reflect in the original array. Thus, the values of cars1
:
and cars2 are the same.
Copy
<script>
var msgs=new Array("Hello","Hey","Morning!");
for (i=0;i<msgs.length;i++){
document.write(msgs[i] + " | ");
}
</script>
Explanation:
In the above JavaScript code, the array is declared using the new operator
and all elements are printing using the loop. Thus, the output would be
"Hello | Hey | Morning! |".
Copy
<script>
var values = [10, 20, 30, 40];
:
var result = values.reduceRight(function(x,y){
return (x + y);
});
1. Result: 40
2. Result: 70
3. Result: 90
4. Result: 100
Explanation:
Copy
<script>
var cars = ["Honda","Hyundai","Mahindra"];
Explanation:
In the above JavaScript code, we used the shift() method which is used to
remove the first element of the given array and return that element. This
method changes the length of the original array. Thus, the output would
be "Result: Hyundai,Mahindra".
Copy
<script>
var cars = ["Honda","Hyundai","Mahindra"];
1. [5] Toyota,Tata,Honda,Hyundai,Mahindra
2. [5]Honda,Hyundai,Mahindra,Toyota,Tata
3. [2] Toyota,Tata
4. [5] Honda,Hyundai,Toyota,Tata,Mahindra
Explanation:
:
In the above JavaScript code, we used unshift() method which is used to
add one or more elements in the beginning of the given array and returns
the updated array. This method changes the length of the original array.
Thus, the output would be "[5] Toyota,Tata,Honda,Hyundai,Mahindra".
1. for()
2. traverse()
3. forEach()
4. foreach()
Answer: C) forEach()
Explanation:
Copy
<script>
const arr = [10, 20, 30];
let result = 0;
arr.forEach(myFunction);
1. Result: 60
2. Result: 102030
3. Result: 10,20,30
4. ValueError
Answer: A) Result: 60
Explanation:
In the above JavaScript code, we used the forEach() method which is used
to call a function (a callback function) once for each array element, and in
the callback function, we are adding the elements of the array. Thus, the
output would be "Result: 60".
Copy
<script>
const values = [10, 20, 30];
const result = values.map(myFunction);
1. Result: 10,20,30
2. Result: 10*10,20*20,30*30
3. Result: 100,400,900
:
4. ValueError
Explanation:
In the above JavaScript code, we used the map() method which is used to
create a new array by performing a function on each array element, and in
the myFunction() we are multiplying the elements with the same value.
Thus, the output would be "Result: 100,400,900".
70. Which JavaScript method is used to create a new array with array
elements that passes a test?
1. forEach()
2. map()
3. forMap()
4. filter()
Answer: D) filter()
Explanation:
The JavaScript method filter() is used to create a new array with array
elements that pass a test.
1. Date
2. DateTime
3. date
4. dateTime
Answer: A) Date
:
Explanation:
1. new Date()
2. new Date(year, month, day, hours, minutes, seconds, milliseconds)
3. new Date(milliseconds)
4. new Date(date string)
5. All of the above
Explanation:
All of the above statements are correct to create Date objects with new
Date() constructor.
Copy
<script>
const curr = new Date();
document.write(curr);
</script>
Explanation:
The above JavaScript code will print the current date & time in the format
of Tue Dec 21 2021 13:04:36 GMT+0530 (India Standard Time).
1. toUTCString()
2. toUtcString()
3. utcString()
4. toutcstring()
Answer: A) toUTCString()
Explanation:
1. January 1, 1972
2. January 1, 1947
3. January 1, 1980
4. January 1, 1970
Explanation:
Explanation:
1. getYear()
2. fullYear()
3. getFullYear()
4. getfullyear()
Answer: C) getFullYear()
Explanation:
Copy
<script>
:
document.write(Math.round(107.5))
</script>
1. 107.5
2. 107
3. 108
4. 107.00
Answer: C) 108
Explanation:
Copy
<script>
try{
const cars = {
company: 'Honda'
};
delete cars.company;
document.write(cars.company);
}
catch (err){
document.write(err.message);
}
</script>
1. undefined
:
2. Honda
3. ValueError
4. TypeError
Answer: A) undefined
Explanation:
Copy
<script>
try{
const cars = {
company: 'Honda'
};
Object.seal(cars);
delete cars.company;
document.write(cars.company);
}
catch (err){
document.write(err.message);
}
</script>
1. undefined
2. Honda
3. ValueError
4. TypeError
:
Answer: B) Honda
Explanation:
In the above JavaScript code, we have sealed the object and the seal
property does not allow the object to be deleted. Hence, the property
company will not be deleted.
Copy
<script>
let x = "10";
let y = + x;
document.write(typeof y);
</script>
1. string
2. object
3. undefined
4. number
Answer: D) number
Explanation:
<script>
let x = 10;
1. number , string
2. number , number
3. object , string
4. object , object
Explanation:
In the above JavaScript code, we are using the String() method which is a
global method to convert numbers to string. Thus, the statement typeof
String(x) will return string.
Copy
<script>
let x = 10;
1. 10 , 10
2. 10 , undefined
3. 10 , [object Undefined]
:
4. None of the above
Explanation:
In the above JavaScript code, the statement toString(x) will not convert
number to string because toString() is not a global method, it is a Number
method and the correct way is to call this function is x.toString().
Preparation
ADVERTISEMENT
ADVERTISEMENT
: