JavaScript Questions and Answers Part-4

1. What will be the output of the following JavaScript code?
Int a=1;
if(a>10)
{
document.write(10);
}
else
{
document.write(a);
}
a) 10
b) 0
c) 1
d) Undefined

Answer: c
Explanation: The if else statement is a part of the javascript conditioning statements. The line of code inside the “if” statement is executed if the value passed to “if” is 1.

2. What will be the output of the following JavaScript code?
var grade='B';
var result;
switch(grade)
{
case 'A':
{
result+="10";
break;
}
case 'B':
{
result+=" 9";
break;
}
case 'C':
{
result+=" 8";
break;
}
default:
result+=" 0";
}
document.write(result);
a) 10
b) 9
c) 8
d) 0

Answer: b
Explanation: The above code contains a switch loop. It is used as an alternative to if else statement. One of the given condition is satisfied according to the input of the switch case and the output is decided accordingly.

3. What will be the output of the following JavaScript code?
var grade='A';
var result;
switch(grade)
{
case 'A':
result+="10";
case 'B':
result+=" 9";
case 'C':
result+=" 8";
default:
result+=" 0";
}
document.write(result);
a) 10
b) 27
c) 8
d) 0

Answer: b
Explanation: The above code does not have a break statement after the cases in the switch loop. Therefore all of the cases following “A” will get executed.

4. What will be the output of the following JavaScript code?
int a=4;
int b=1;
int c=0;
If(a==b)
document.write(a);
else if(a==c)
document.write(a);
else
document.write(c);
a) 4
b) 1
c) Error
d) 0

Answer: d
Explanation: For checking more than one condition the if else if loop is used. It is the extension of if else loop which is also sometimes known as if else ladder.

5. What will be the output of the following JavaScript code?
var grade='E';
var result;
switch(grade)
{
case 'A':
result+="10";
case 'B':
result+=" 9";
case 'C':
result+=" 8";
default:
result+=" 0";
}
document.write(result);
a) 10
b) 0
c) 18
d) 17

Answer: b
Explanation: The switch case contains a default case along with the other cases. The default case gets executed when no other case results into true.

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

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

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

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

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