GkSeries.com

JavaScript Questions & Answers – Loops in JavaScript | JavaScript MCQs

(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);
     }
}
[A] Prints the numbers in the array in order
[B] Prints the numbers in the array in the reverse order
[C] Prints 0 to the length of the array
[D] Prints “Empty Array”
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?
[A] Updation, Incrementation, Initialization
[B] Initialization,Testing, Updation
[C] Testing, Updation, Testing
[D] Initialization,Testing, Incrementation
Answer: Initialization,Testing, Updation

DOWNLOAD CURRENT AFFAIRS PDF FROM APP

(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;
}
[A] No, this will throw an exception as only numerics can be used in a for loop
[B] No, this will not iterate
[C] Yes, this will work
[D] No, this will result in a runtime error with the message “Cannot use Linked List”
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]);
[A]
for (var i = 0;i < a.length;i++)
         console.log(a[i]);
[B]
for (int i = 0;i < a.length;i++)
         console.log(a[i]);

[C]
for (var i = 0;i <= a.length;i++)
     	console.log(a[i]);
[D]
for (var i = 1;i < a.length;i++)
     	console.log(a[i]);
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 ___________
[A] Before each iteration, the interpreter evaluates the variable expression and assigns the name of the property
[B] The iterations can be infinite when an interpreter is used
[C] The body of the loop is executed only once
[D] the iteration is finite when an interpreter is used
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?
[A] The property will be stored in a cache
[B] The loop will not run
[C] That property will not be enumerated
[D] The property will be 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?
[A] The interpreter stops its work
[B] The interpreter throws another exception
[C] The interpreter jumps to the nearest enclosing exception handler
[D] The interpreter throws an error
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++;
}
[A] The continue keyword restarts the loop
[B] The continue keyword skips the next iteration
[C] The continue keyword skips the rest of the statements in that iteration
[D] The continue keyword breaks out of the loop
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;
}
[A] It does nothing but a simple breakpoint
[B] It debugs the error in that statement and restarts the statement's execution
[C] It is used as a keyword that debugs the entire program at once
[D] It is used to find error in the statement
Answer: It does nothing but a simple breakpoint
(10) Among the keywords below, which one is not a statement?
[A] debugger
[B] with
[C] if
[D] use strict
Answer: use strict
(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);

[A] 5
[B] 555
[C] 3
[D] error
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);

[A] 11121314
[B] 1112
[C] 12345
[D] 11
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);

}

[A] 135
[B] 123
[C] 013
[D] 01
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;

}

[A] 5555
[B] 5321
[C] 531-1
[D] 531
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);

[A] 0
[B] error
[C] 4
[D] 5
Answer: 5

Please share this page

Click Here to Read more questions

Teacher Eligibility Test