JavaScript Questions and Answers Part-8

1. What will be the possible output of the following JavaScript code?
var a = [1,2,3,4,5];
a.slice(0,3);
a) Returns [1,2,3]
b) Returns [4,5]
c) Returns [1,2,3,4]
d) Returns [1,2,3,4,5]

Answer: a
Explanation: .slice() function is a predefined function in JavaScript used to keep the elements from starting point and ending point mentioned in the function argument. The elements after the ending point and before the starting point are not shown.

2. What will be the shift() output of the following JavaScript code?
var a = [];
a.unshift(1);
a.unshift(22);
a.shift();
a.unshift(3,[4,5]);
a.shift();
a.shift();
var output = a.shift();
document.writeln(output);
a) 1
b) [4,5]
c) [3,4,5]
d) Exception is thrown

Answer: a
Explanation: The unshift() and shift() methods behave much like push() and pop(), except that they insert and remove elements from the beginning of an array rather than from the end. unshift() adds an element or elements to the beginning of the array, shifts the existing array elements up to higher indexes to make room, and returns the new length of the array. shift() removes and returns the first element of the array, shifting all subsequent elements down one place to occupy the newly vacant space at the start of the array. After a series of operations, the code outputs “1” as the final result using document.writeln().

3. The primary purpose of the array map() function is that it __________
a) maps the elements of another array into itself
b) passes each element of the array and returns the necessary mapped elements
c) passes each element of the array on which it is invoked to the function you specify, and returns an array containing the values returned by that function
d) pass the elements of the array into another array

Answer: c
Explanation: map() is a predefined function in javascript used for mapping the array elements to be used for some other purpose. The map() method passes each element of the array on which it is invoked to the function you specify, and returns an array containing the values returned by that function.

4. The reduce and reduceRight methods follow a common operation called __________
a) filter and fold
b) inject and fold
c) finger and fold
d) fold

Answer: b
Explanation: The reduceRight() method reduces the array to a single value. The reduceRight() method executes a provided function for each value of the array (from right-to-left). The return value of the function is stored in an accumulator (result/total). Hence it does the operation of injecting and folding.

5. The method or operator used to identify the array is __________
a) isarrayType()
b) ==
c) ===
d) typeof

Answer: d
Explanation: The typeof operator is used to get the data type (returns a string) of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. The operator returns the data type.

6. What will be the output of the following JavaScript code?
var val1=[1,2,3];
var val2=[6,7,8];
var result=val1.concat(val2);
document.writeln(result);
a) 1, 2, 3
b) error
c) 1, 2, 3, 6, 7, 8
d) 123

Answer: c
Explanation: concat is a predefined function in the array library in Javascript. The concat function is used to combine the value of two arrays.
i.e.1, 2, 3, 6, 7, 8

7. What will be the output of the following JavaScript code?
var values=[1,2,3,4]
var ans=values.slice(1);
document.writeln(ans);
a) 1, 2, 3, 4
b) 2, 3, 4
c) 1, 3, 4
d) error

Answer: b
Explanation: The slice() function is used to extract values from an array. In this code, slice() creates a new array starting from index 1, removing the first element “1”, and returning the remaining elements “2, 3, 4”.

8. What will be the output of the following JavaScript code?
int sum=0;
var arr = [10,15,20,30];
arr.forEach(function myFunction(element)
{
sum= sum+element;
});
document.writeln(sum);
a) 70
b) 75
c) 10
d) error

Answer: b
Explanation: forEach is a predefined function in Javascript which used to traverse through the array. It works in a similar way to for loop and iterates through each value in array.

9. What will be the output of the following JavaScript code?
var values=["one","two","Three"];
var ans=values.shift();
document.writeln(ans);
a) one
b) two
c) three
d) error

Answer: a
Explanation: shift is a predefined function in array library and works like a pop function. It pops the first value of the array and returns its value. Therefore the answer will be one.

10. What will be the output of the following JavaScript code?
var arr=[1,2,3];
var rev=arr.reverse();
document.writeln(rev);
a) 1, 2, 3
b) 3, 2, 1
c) 3
d) 1

Answer: b
Explanation: reverse function is a predefined function in array library in Javascript. The function is used to reverse the element of the array.
i.e. 3, 2, 1.