94). 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
Show Answer
Correct Answer: The omitted value takes “undefined”
Notes:
Answer: a
Explanation: 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.
95). 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
Show Answer
Correct Answer: true false
Notes:
Answer: a
Explanation: 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.
96). The pop() method of the array does which of the following task?The pop() method of the array does which of the following task?
var a1 = [,,,];
var a2 = new Array(3);
0 in a1
0 in a2
[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
Show Answer
Correct Answer: decrements the total length by 1
Notes:
Answer: a
Explanation: 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.
97). 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
Show Answer
Correct Answer: Skips the null elements
Notes:
Answer: c
Explanation: 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.
98). What will happen if reverse() and join() methods are used simultaneously?
if (!a[i]) continue;
[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
Show Answer
Correct Answer: Reverses and stores in the same array
Notes:
Answer: a
Explanation: 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.
99). 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]
Show Answer
Correct Answer: Returns [1,2,3]
Notes:
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.
100). 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
Show Answer
Correct Answer: 1
Notes:
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().
101). The primary purpose of the array map() function is that it __________
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]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
Show Answer
Correct Answer: 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
Notes:
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.
102). The reduce and reduceRight methods follow a common operation called __________
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]filter and fold
[B]inject and fold
[C]finger and fold
[D]fold
Show Answer
Correct Answer: inject and fold
Notes:
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.
103). The method or operator used to identify the array is __________
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] isarrayType()
[B]==
[C]===
[D]typeof
Show Answer
Correct Answer: typeof
Notes:
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.
104). 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
Show Answer
Correct Answer: 1, 2, 3, 6, 7, 8
Notes:
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
105). 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
Show Answer
Correct Answer: 2, 3, 4
Notes:
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”.
107). 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
Show Answer
Correct Answer: 75
Notes:
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.
108). 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]two
[D]error
Show Answer
Correct Answer: one
Notes:
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.
109). 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
Show Answer
Correct Answer: 3, 2, 1
Notes:
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.