True or false: Replacing an existing element in an array changes the array's length.
6 Arrays and Collections Online Quiz Questions
Use this free practice quiz with 20 questions to review 6 Arrays and Collections, test your knowledge, and prepare for your next test or exam.
An array has length 5. Which index is outside the valid range?
- A
0
- B
4
- C
5
- D
6
Using the linear-search convention described in the material, what integer should linearSearch([14, 7, 22, 9], 11) return?
Complete the rule: For an array of length n, the valid indexes range from 0 through .
Before applying binary search to an array, which condition is essential?
- A
The array must contain only positive values.
- B
The array must already be sorted using the search ordering.
- C
The array must have a fixed length of exactly n elements.
- D
The array must contain no duplicate values.
True or false: To find a maximum reliably, an algorithm should initialize its best-so-far value from the first array element rather than assuming that 0 is a suitable starting value.
- A
True
- B
False
Select all situations for which the material specifically recommends using an indexed loop.
- A
Use an indexed loop when changing elements in place.
- B
Use a collection-style loop when comparing neighboring positions.
- C
Use an indexed loop when the index is needed.
- D
Use a collection-style loop when the array must be sorted first.
What is the average of the array [72, 88, 61, 95]?
Complete the statement: A traversal that visits every element of an array with n elements has running time approximately .
Select all statements that correctly describe the set-based approach to detecting duplicates.
- A
A set can record values already seen.
- B
The set-based approach always uses O(1) extra space.
- C
The set-based approach can improve the time to approximately O(n).
- D
The set-based approach requires the input to be sorted first.
Which statement correctly describes JavaScript's sort() method according to the material?
- A
JavaScript's
sort()always leaves the original array unchanged. - B
JavaScript's
sort()changes the array on which it is called. - C
JavaScript's
sort()is valid only for already sorted arrays. - D
JavaScript's
sort()returns a separate result whiletoSorted()mutates the original.
Which strategy best implements containsDuplicate(values) when improving on the nested-loop solution is important?
- A
It reverses the array and checks whether the first and last values match.
- B
It sorts the array and returns true only if every value is unique.
- C
It records previously visited values and returns true when a value is encountered again.
- D
It uses binary search on the original array without sorting it.
True or false: Choosing a dynamic list eliminates the need to consider the cost of shifting later elements when inserting or removing in the middle.
- A
True
- B
False
You are designing a program that stores a sequence of values. Explain when you would choose an array instead of a dynamic list, and when you would choose a dynamic list instead. Include the performance or usage trade-offs that support your decision.
An array has a length of 5. Which range contains all of its valid zero-based indexes?
- A
0 through 5
- B
0 through 4
- C
1 through 5
- D
1 through 4
Given the array [12, 7, 9, 4], a program executes values[1] = 15. What is the resulting array?
- A
[12, 7, 9]
- B
[12, 7, 9, 4, 0]
- C
[12, 15, 9, 4]
- D
[15, 7, 9, 4]
What does linearSearch([14, 7, 22, 9], 11) return when the function returns the first matching index and returns -1 if the target is absent?
- A
0
- B
1
- C
2
- D
-1
A program is about to use binary search on an array. Which condition is essential for the search to provide valid results?
- A
The array must be sorted according to the search ordering.
- B
The array must contain only distinct values.
- C
The array must have an even length.
- D
The target must be located at an endpoint.
In a zero-based array, what index contains the element at human-readable position 3?
What is the time complexity of traversing an array with n elements once, visiting each element approximately one time?