JavaScript Questions & Answers – Loops in JavaScript

We provide quiz questions on Javascript Loops where you can learn lots of objective questions with answers. JavaScript Questions & Answers – Loops in JavaScript

This set of Java script Multiple Choice Questions & Answers (MCQ) focuses on “Loops in JavaScript”.

1. Consider the following code snippet

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);
}
}

What does the above code result?
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”

Explanation : The do/while loop is less commonly used when compared to the while loop. Here, it prints from the array in the given order.
. 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. All of the mentioned

Explanation : If the body of a for/in loop deletes a property that has not yet been enumerated, that property will not be enumerated. If the
body of the loop defines new properties on the object, those properties will generally not be enumerated.

3. Consider the following code snippet
function tail(o)
{
for (; o.next; o = o.next) ;
return o;
}

Will the above code snippet work? If not, what will be the error?
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”

Explanation : The above code uses a for loop to traverse a linked list data structure and return the last object in the list. This will perfectly work.

4. Consider the following code snippet

for(var p in o)
console.log(o[p]);

The above code is equivalent to which code?

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]);

Explanation : The for/in loop makes it easy to do the same that we do using a for.

5. One of the special feature 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. All of the mentioned

Explanation : Before each iteration, the interpreter evaluates the variable expression and assigns the name of the property (a string value) to it.
6. 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

Explanation : In a for loop, the initialization, the test, and the update are the three crucial manipulations of a loop variable.

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. None of the mentioned

Explanation : When an exception is thrown in a jump statement, the interpreter jumps to the nearest enclosing exception handler, which may be in the same function or up the call stack in an invoking function.

8. Consider the following code snippet

while (a != 0)
{
if (a == 1)
continue;
else
a++;
}

What will be the role of the continue keyword in the above code snippet?
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. None of the mentioned

Explanation : Instead of exiting a loop like the break keyword, the continue keyword moves to the next iteration from the place encountered.

9. Consider the following code snippet

function f(o)
{
if (o === undefined) debugger;
}

What could be the task of the statement 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. All of the mentioned

Explanation : The debugger statement normally does nothing. If, however, a debugger program is available and is running, then an implementation may (but is not required to) perform some kind of debugging action. In practice, this statement acts like a breakpoint: execution of JavaScript code stops and you can use the debugger to print variable’s values.

10. Among the keywords below, which one is not a statement?
a. debugger
b. with
c. if
d. use strict

Explanation : use strict is a directive introduced in ECMAScript5. Directives are not statements because it does not include any language keywords. Also, it can appear only at the start of a script or at the start of a function body, before any real statemenst have appeared.

Sanfoundry Global Education & Learning Series – Javascript Programming.

Leave a Reply

Menu