JavaScript MCQ With Answers
Javascript is one of the most popular programming languages in the world currently, and it is basically the language of the web. It is a lightweight, cross-platform, and interpreted programming language, primarily used for web development and backend development. Javascript is a language that is used widely on both the client and server sides of development. Javascript offers a vast standard library that has a wide variety of functions and methods available to help in the process of development, making the entire process easier and hassle-free.
Javascript can be added to our HTML code in mostly 2 ways:
- Internal Javascript: Javascript in this case is directly added to the HTML code by adding the JS code inside the <script> tag, which can be placed either inside the <head> or the <body> tags based on the requirement of the user.
- External Javascript: In this case, the Javascript code is written inside an external file with the extension file_name.js, and then the file is linked to the HTML file inside the <head> tag.
Features of Javascript
- Javascript is a lightweight, cross-platform, and interpreted language.
- It was developed with the main intention of DOM manipulation, bringing forth the era of dynamic websites.
- Javascript functions are objects and can be passed in other functions as parameters.
- Can handle date and time manipulation.
- Can perform Form Validation.
- A compiler is not needed.
Useful Resources
- JavaScript Books
- JavaScript Projects
- JavaScript Features
- JavaScript Frameworks
- JavaScript IDE
- JavaScript Libraries
- Difference between Typescript and Javascript
JavaScript MCQ
What will be the output of the following code snippet?
const obj1 = {first: 20, second: 30, first: 50};
console.log(obj1);
What will be the output of the following code snippet?
let a = [1, 2, 3, 4, 5, 6];
var left = 0, right = 5;
var found = false;
var target = 5;
while(left <= right) {
var mid = Math.floor((left + right) / 2);
if(a[mid] == target) {
found = true;
break;
}
else if(a[mid] < target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
if(found) {
print("YES");
}
else {
print("NO");
}
What will be the output of the following code snippet?
print(typeof(NaN));
What will be the output of the following code snippet?
let s = "00000001111111";
let l = 0, r = s.length - 1, ans = -1;
while(l <= r) {
var mid = Math.floor((l + r) / 2);
if(s[mid] == '1') {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
print(ans);
What will be the output of the following code snippet?
const example = ({ a, b, c }) => {
console.log(a, b, c);
};
example(0, 1, 2);
Learn via our Video Courses
What will be the output of the following code snippet?
function solve(arr, rotations){
if(rotations == 0) return arr;
for(let i = 0; i < rotations; i++){
let element = arr.pop();
arr.unshift(element);
}
return arr;
}
// solve([44, 1, 22, 111], 5);
What will be the output of the following code snippet?
var a = Math.max();
var b = Math.min();
print(a);
print(b);
What will be the output of the following code snippet?
const set = new Set();
set.add(5);
set.add('Hello');
set.add({ name: 'Scaler' });
for (let item of set) {
console.log(item + 6);
}
What will be the output of the following code snippet?
var a = "hello";
var sum = 0;
for(var i = 0; i < a.length; i++) {
sum += (a[i] - 'a');
}
print(sum);
What will be the output of the following code snippet?
function test(...args) {
console.log(typeof args);
}
test(12);
What will be the output of the following code snippet?
var a = true + true + true * 3;
print(a)
What will be the output of the following code snippet?
a = [1, 2, 3, 4, 5];
print(a.slice(2, 4));
What will be the output of the following code snippet?
var a = 1;
var b = 0;
while (a <= 3)
{
a++;
b += a * 2;
print(b);
}
What will be the output of the following code snippet?
(function(){
setTimeout(()=> console.log(1),2000);
console.log(2);
setTimeout(()=> console.log(3),0);
console.log(4);
})();
What will be the output of the following code snippet?
<script type="text/javascript" language="javascript">
var a = "Scaler";
var result = a.substring(2, 4);
document.write(result);
</script>
When an operator’s value is NULL, the typeof returned by the unary operator is:
When the switch statement matches the expression with the given labels, how is the comparison done?
Which function is used to serialize an object into a JSON string in Javascript?
Which object in Javascript doesn’t have a prototype?
Which of the following are closures in Javascript?
Which of the following are not server-side Javascript objects?
Which of the following is not a Javascript framework?
Which of the following keywords is used to define a variable in Javascript?
Which of the following methods can be used to display data in some form using Javascript?
Which of the following methods is used to access HTML elements using Javascript?
What is the use of the <noscript> tag in Javascript?
How can a datatype be declared to be a constant type?
How do we write a comment in javascript?
How to stop an interval timer in Javascript?
Javascript is an _______ language?
The 3 basic object attributes in Javascript are:
The process in which an object or data structure is translated into a format suitable for transferral over a network, or storage is called?
Upon encountering empty statements, what does the Javascript Interpreter do?
What does the Javascript “debugger” statement do?
What does the ‘toLocateString()’ method do in JS?
What does … operator do in JS?
What happens when we run this code?
function dog() {
print("I am a dog.");
}
dog.sound = "Bark";
What is the output of the following code snippet?
print(NaN === NaN);
How are objects compared when they are checked with the strict equality operator?
What keyword is used to check whether a given property is valid or not?
What keyword is used to declare an asynchronous function in Javascript?
What will be the output for the following code snippet?
<p id="example"></p>
<script>
function Func()
{
document.getElementById("example").innerHTML=Math.sqrt(81);
}
</script>
What will be the output of the following code snippet?
(function(a){
return (function(){
console.log(a);
a = 6;
})()
})(21);
What will be the output of the following code snippet?
let n = 24;
let l = 0, r = 100, ans = n;
while(l <= r) {
let mid = Math.floor((l + r) / 2);
if(mid * mid <= n) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
print(ans);
What will be the output of the following code snippet?
let sum = 0;
const a = [1, 2, 3];
a.forEach(getSum);
print(sum);
function getSum(ele) {
sum += ele;
}
What will be the output of the following code snippet?
print(parseInt("123Hello"));
print(parseInt("Hello123"));
What will be the output of the following code snippet?
var a = Math.max() < Math.min();
var b = Math.max() > Math.min();
print(a);
print(b);
What will be the output of the following code snippet?
<script type="text/javascript" language="javascript">
var x=12;
var y=8;
var res=eval("x+y");
document.write(res);
</script>
What will be the output of the following code snippet?
<script type="text/javascript">
a = 5 + "9";
document.write(a);
</script>
What will be the output of the following code snippet?
const obj1 = {Name: "Hello", Age: 16};
const obj2 = {Name: "Hello", Age: 16};
print(obj1 === obj2);