JavaScript Questions & Answers – Loops in JavaScript

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Loops in JavaScript”.

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”
View Answer

Answer: a
Explanation: The do/while statement creates a loop that executes a block of code once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. Hence the iterator traverses through the array and print them in normal order.
advertisement
advertisement

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
View Answer

Answer: b
Explanation: In a for loop, the initialization, the test, and the update are the three crucial manipulations of a loop variable. Firstly the loop initialiases the variable then test the condition and then after executing the statement increments its value.

3. What will the following JavaScript code snippet work? If not, what will be the error?

Note: Join free Sanfoundry classes at Telegram or Youtube
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”
View Answer

Answer: c
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.
advertisement

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

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]);
View Answer
Answer: a
Explanation: The in variable does the same task of traversing the array starting from the 0 index. The for/in loop makes it easy to do the same that we do using a for.
 
 

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
View Answer

Answer: a
Explanation: Interpreter translates the source code into machine code line by line, and stops when it encounters an error. Before each iteration, the interpreter evaluates the variable expression and assigns the name of the property (a string value) to it.

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
View Answer

Answer: c
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.

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
View Answer

Answer: c
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. 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
View Answer

Answer: c
Explanation: Instead of exiting a loop like the break keyword, the continue keyword moves to the next iteration from the place encountered. While the break statement breaks out of the loop.

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
View Answer

Answer: a
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
View Answer

Answer: d
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 statement has appeared.

11. What will be the output of the following JavaScript code?

function range(int length)
{
	var a=5;
	for(var i=0;i<length;i++)
	{
		console.log(a);
	} 
}
range(3);

a) 5
b) 555
c) 3
d) error
View Answer

Answer: b
Explanation: for loop first initializes the variable and later on checks for the condition expression and after that execute the line of statements. The value of iterator i increase until it reaches the value of length.

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
View Answer

Answer: d
Explanation: do while loop first executes the statements (at least once) and after that checks for the condition. In this case variable a is incremented first, it will become 11, and after that the condition check will become false and the loop will terminate.

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) 136
b) 123
c) 013
d) 01
View Answer

Answer: a
Explanation: A while loops checks for the condition first before executing the loop statements. In this case, the variable a is incremented by 1 in every iteration till the loop breaks i.e., once the variable a becomes 3.
For a=1, b will be 1.
For a=2, b will be 3.
For a=2, b will be 6.
Hence, the output will be 136.

14. What will be the output of the following JavaScript code?

let size = 5;
let a = 5;
for (let j = size; j >= 0; j=a) {
    document.write(a);
    a = a - 2;
}

a) 5555
b) 5321
c) 531
d) Infinite loop
View Answer

Answer: c
Explanation: The value of a will decrease by 2 at every iteration. The for loop will iterate 3 times till the time the value of j (j=a) becomes negative.

15. What will be the output of the following JavaScript code?

let a = 0;
for (a; a < 5; a++);
console.log(a);

a) 0
b) error
c) 4
d) 5
View Answer

Answer: d
Explanation: The output will be 5 because a increments from 0 to 5 within the loop before the loop condition fails. Remember, there is a semicolon immediately after the for loop, hence the variable a will simply increment up to 5.

Sanfoundry Global Education & Learning Series – Javascript Programming.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.