JAVASCRIPT ARRAY EXAM QUESTIONS
AND ANSWERS
What happens when you call an array index that does not exist? - Answer-It returns
undefined because it is a type of object property
How can you make a value of an array spare? - Answer-Use the delete operator to
delete a pre-existing value or specify a value at an index using a[995] = 100; So the
value of index number 995 of a array is now 100.
What happens if you set a new length to an array that is smaller than the current length
of it? - Answer-The values of index numbers larger than the new length of the array will
be deleted.
What is the value of the length property? - Answer-i + 1 because the first index number
= 0.
What is an array? - Answer-An array is an ordered collection of values.
What is a element? - Answer-An element is a value in an array.
What is an index? - Answer-The ordered position of the elements in the array.
Are arrays typed or untyped? - Answer-JS arrays are untyped, meaning any type of
data can be inserted as a value.
What does it mean that JS arrays are dynamic? - Answer-It means they grow and shrink
as needed; there is no need to declare a fixed size.
What does it mean that JS arrays are sparse? - Answer-It means that the elements do
not have contiguous indexes from zero, but that there may be gaps.
What is the length property for sparse or nonsparse arrays? How does the index
number related to the length? - Answer-For non-sparse arrays, the length equals the
number of elements in the array. For sparse arrays, the length is larger than the index of
the elements because of empty indexes.
What is the construct for an array literal? - Answer-var empty = [];
An empty array.
, What is an array constructor? How could you add to array constructors? - Answer-var a
= new array();
Constructor array with no arguments.
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
In the construct var a = new array(45); what does the 45 stand for? - Answer-The
number of indexes the array should create. Additional arguments would specify the
values of the array.
Are all indexes property names? - Answer-Yes, but not all property names are indexes.
Only numbers between 0 - 2^32-2.
Other than assigning values to new indexes, how can elements of an array be created?
- Answer-array.push(); ---> adds one or more elements to the end of the array
array.unshift() ---> pushes an array value to the beginning of an array and shifting all
other values to a larger index
What happens if you delete a value from an array index? - Answer-That index becomes
sparse.
What code in a for loop would skip null, undefined, or nonexistent elements? - Answer-if
(!a[i]) continue;
What code in a for loop would skip undefined, or nonexistent elements? - Answer-if (a[i]
=== undefined) continue;
What code in a for loop would skip nonexistent elements? - Answer-(!(i in a)) continue;
How would you test for any inherited properties an array would have from its prototype
and filter them out? - Answer-if (!a.hasOwnProperty(i)) continue;
Which skips inherited properties
What is a heterogeneous array? - Answer-An array that is comprised of different types
of values: strings, numbers, and booleans
What is a jagged array? - Answer-An array that is composed of two or more arrays that
do not have the same number of values in their respective arrays
What is a multidimensional array? - Answer-An array that has one or more arrays
nested inside of it
Define array.join() - Answer-A method that converts all elements of an array to a string
and concatenates them returning the resulting string
AND ANSWERS
What happens when you call an array index that does not exist? - Answer-It returns
undefined because it is a type of object property
How can you make a value of an array spare? - Answer-Use the delete operator to
delete a pre-existing value or specify a value at an index using a[995] = 100; So the
value of index number 995 of a array is now 100.
What happens if you set a new length to an array that is smaller than the current length
of it? - Answer-The values of index numbers larger than the new length of the array will
be deleted.
What is the value of the length property? - Answer-i + 1 because the first index number
= 0.
What is an array? - Answer-An array is an ordered collection of values.
What is a element? - Answer-An element is a value in an array.
What is an index? - Answer-The ordered position of the elements in the array.
Are arrays typed or untyped? - Answer-JS arrays are untyped, meaning any type of
data can be inserted as a value.
What does it mean that JS arrays are dynamic? - Answer-It means they grow and shrink
as needed; there is no need to declare a fixed size.
What does it mean that JS arrays are sparse? - Answer-It means that the elements do
not have contiguous indexes from zero, but that there may be gaps.
What is the length property for sparse or nonsparse arrays? How does the index
number related to the length? - Answer-For non-sparse arrays, the length equals the
number of elements in the array. For sparse arrays, the length is larger than the index of
the elements because of empty indexes.
What is the construct for an array literal? - Answer-var empty = [];
An empty array.
, What is an array constructor? How could you add to array constructors? - Answer-var a
= new array();
Constructor array with no arguments.
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
In the construct var a = new array(45); what does the 45 stand for? - Answer-The
number of indexes the array should create. Additional arguments would specify the
values of the array.
Are all indexes property names? - Answer-Yes, but not all property names are indexes.
Only numbers between 0 - 2^32-2.
Other than assigning values to new indexes, how can elements of an array be created?
- Answer-array.push(); ---> adds one or more elements to the end of the array
array.unshift() ---> pushes an array value to the beginning of an array and shifting all
other values to a larger index
What happens if you delete a value from an array index? - Answer-That index becomes
sparse.
What code in a for loop would skip null, undefined, or nonexistent elements? - Answer-if
(!a[i]) continue;
What code in a for loop would skip undefined, or nonexistent elements? - Answer-if (a[i]
=== undefined) continue;
What code in a for loop would skip nonexistent elements? - Answer-(!(i in a)) continue;
How would you test for any inherited properties an array would have from its prototype
and filter them out? - Answer-if (!a.hasOwnProperty(i)) continue;
Which skips inherited properties
What is a heterogeneous array? - Answer-An array that is comprised of different types
of values: strings, numbers, and booleans
What is a jagged array? - Answer-An array that is composed of two or more arrays that
do not have the same number of values in their respective arrays
What is a multidimensional array? - Answer-An array that has one or more arrays
nested inside of it
Define array.join() - Answer-A method that converts all elements of an array to a string
and concatenates them returning the resulting string