JavaScript Questions and Answers Part-5

1. 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: 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.

2. 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: 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.

3. 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: 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.

4. 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: 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.

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

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.

6. 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: 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.

7. 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: d
Explanation: dowhile loop first executes the statements and after that checks for the condition. Therefore dowhile loop will be executed and after that the condition will become false and the loop will terminate.

8. 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: a
Explanation: A while loops checks for the condition first before executing the looping statements. A while loop increments the value at the end of the loop whereas for executes the statement at the starting of the loop.

9. 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: c
Explanation: The value of a will decrease by 2 at every iteration. The for loop will iterate four times till the value of j will decrease to 0.

10. 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: d
Explanation: The value of a will increase until it will become equal to 5 after that the cursor will come out the loop. There are no statements for the for loop therefore only the value of a will increase. Hence the output will be five.