JavaScript Questions & Answers – Array and Related Methods:

1. What is the observation made in the following JavaScript code?

var count = [1,,3];

a) The omitted value takes “undefined”
b) This results in an error
c) This results in an exception
d) The omitted value takes an integer value

Answer: a
Array is defined with a null value when no value is mentioned. If you omit a value from an array literal, the omitted element is given an undefined value.

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

var a1 = [,,,]; 
var a2 = new Array(3); 
0 in a1 
0 in a2

a) true false
b) false true
c) true true
d) false true

Answer: a
: Array a1 is defined with null values. Therefore we can access the indexes 0, 1 and 2. But array a2 is only defined not declared. Therefore we cannot access index 0.

3. The pop() method of the array does which of the following task?

a) decrements the total length by 1
b) increments the total length by 1
c) prints the first element but no effect on the length
d) updates the element

Answer: a
pop() function pops out that is delete the last element from the array. Hence pop() method (it works with push()) reduces the length of an array by 1.

4. What is the observation made in the following JavaScript code?

if (!a[i]) continue;

a) Skips the defined elements
b) Skips the existent elements
c) Skips the null elements
d) Skips the defined & existent elements

Answer: c
The if loop in the above code checks whether the value of a[i] exists or not. For undefined, non existent and null values the if loop returns true.

5. What will happen if reverse() and join() methods are used simultaneously?

a) Reverses and stores in the same array
b) Reverses and concatenates the elements of the array
c) Reverses
d) Stores the elements of an array in normal order

Answer: a
The array.join() method is an inbuilt function in JavaScript which is used to join the elements of an array into a string. The reverse() followed by a join() will reverse the respective array and will store the reversed array in the memory

6. What will be the possible output of the following JavaScript code?

var a = [1,2,3,4,5];

a) Returns [1,2,3]
b) Returns [4,5]
c) Returns [1,2,3,4]
d) Returns [1,2,3,4,5]

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

7. What will be the shift() output of the following JavaScript code?

var a = []; 
var output = a.shift();

a) 1
b) [4,5]
c) [3,4,5]
d) Exception is thrown

Answer: a
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().

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

9. The method or operator used to identify the array is __________

a) isarrayType()
b) ==
c) ===
d) typeof

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

10. 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);  

a) 1, 2, 3
b) Error
c) 1, 2, 3, 6, 7, 8
d) 123

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

thank you……………..

Leave a Reply

Your email address will not be published. Required fields are marked *