JavaScript Questions & Answers – Loops in JavaScript | JavaScript MCQs
Questions
1
What will be the output of the following JavaScript code?
function printArray(a)
{
var len = a.length, i = 0;
if (len == 0)
console.log("Empty Array");
else
{
do
{
console.log(a[i]);
} while (++i < len);
}
}
Answer: Prints the numbers in the array in order
2
What are the three important manipulations done in a for loop on a loop variable?
Answer: Initialization,Testing, Updation
⭐ Make GKSeries Your Preferred Source on Google
Add GKSeries as Preferred Source
3
What will the following JavaScript code snippet work? If not, what will be the error?
function tail(o)
{
for (; o.next; o = o.next) ;
return o;
}
Answer: Yes, this will work
4
What will be the equivalent code of the following JavaScript code?
for(var p in o) console.log(o[p]);
Answer: for (var i = 0;i < a.length;i++)
console.log(a[i]);
5
One of the special features of an interpreter in reference with the for loop is that ___________
Answer: Before each iteration, the interpreter evaluates the variable expression and assigns the name of the property
6
What will happen if the body of a for/in loop deletes a property that has not yet been enumerated?
Answer: That property will not be enumerated
7
What will be the step of the interpreter in a jump statement when an exception is thrown?
Answer: The interpreter jumps to the nearest enclosing exception handler
8
What will be the role of the continue keyword in the following JavaScript code snippet?
while (a != 0)
{
if (a == 1)
continue;
else
a++;
}
Answer: The continue keyword skips the rest of the statements in that iteration
9
What could be the task of the statement debugger in the following JavaScript code?
function f(o)
{
if (o === undefined) debugger;
}
Answer: It does nothing but a simple breakpoint
11
What will be the output of the following JavaScript code?
function range(int length)
{
int a=5;
for(int i=0;i< length;i++)
{
console.log(a);
}
}
range(3);
Answer: 555
12
What will be the output of the following JavaScript code?
var a = 10;
do {
a += 1;
console.log(a);
} while (a < 5);
Answer: 11
13
What will be the output of the following JavaScript code?
var a= 0;
var b = 0;
while (a < 3)
{
a++;
b += a;
console.log(b);
}
Answer: 135
14
What will be the output of the following JavaScript code?
int size=5;
int a=5;
int size=4;
for(int j=size;j>=0;j--)
{
console.log(a);
a=a-2;
}
Answer: 531-1
15
What will be the output of the following JavaScript code?
int a=0;
for(a;a<5;a++);
console.log(a);
Answer: 5