Which expression gives the final valid index of a nonempty Java array?
07 Arrays and Collections Online Quiz Questions
Use this free practice quiz with 20 questions to review 07 Arrays and Collections, test your knowledge, and prepare for your next test or exam.
Which two Java statements create an array object rather than merely declaring an array variable? Select all that apply.
- A
int[] values;
- B
int[] values = new int[4];
- C
int[] values = {2, 4, 6, 8};
- D
int values = new int[4];
Given int[] values = {3, 6, 9, 12};, which expression correctly obtains the number of elements?
- A
values.size()
- B
values.length
- C
values.length()
- D
values.count()
Given int[] scores = {82, 91, 76, 88, 95};, what index contains the value 95?
Complete the statement: In Java, the final valid array index is array..
True or false: In Java, assigning a new value to the variable in for (int score : scores) updates the corresponding element of scores.
- A
True
- B
False
Which loop correctly increases every element of an existing int[] scores array by 5?
- A
for (int score : scores) { score += 5; }
- B
for (int score : scores) { scores = score + 5; }
- C
for (int i = 0; i < scores.length; i++) { scores[i] += 5; }
- D
for (int i = 1; i <= scores.length; i++) { scores[i] += 5; }
Which two statements about searching Java arrays are correct? Select all that apply.
- A
A linear search can be used on an unsorted array.
- B
A binary search is valid on any unsorted array.
- C
A linear search can stop after finding the target.
- D
A for-each loop by itself automatically returns the target index.
In the linear-search pattern from the material, what integer sentinel represents that the target was not found?
True or false: An ArrayList is generally a better choice than an array when a program frequently adds and removes elements.
- A
True
- B
False
Given int[] scores = {82, 91, 76, 88, 95};, which expression accesses the first element of the array?
- A
scores[1]
- B
scores[0]
- C
scores[-1]
- D
scores[scores.length]
What values are initially stored in the elements of int[] values = new int[4];?
- A
The array cannot be used until each position is assigned.
- B
Each element contains
null. - C
Each element contains
0. - D
Each element contains an unspecified random integer.
Which expression correctly obtains the number of elements in int[] temperatures = new int[7];?
- A
temperatures.length - B
temperatures.length() - C
temperatures.size - D
temperatures.size()
A program must find a target value in an array whose elements are not sorted. Which search strategy is appropriate without sorting the array first?
- A
Binary search, because it works on every array
- B
Linear search, because it works whether or not the array is sorted
- C
Binary search, because it always examines every element
- D
Neither search, because Java arrays cannot be searched
True or false: Arrays.binarySearch can reliably search an unsorted array without any additional conditions.
- A
True
- B
False
Why is (double) sum / scores.length used when computing the average of an int array?
- A
The result is always rounded up.
- B
Casting
sumtodoublemakes the division floating-point, preserving any fractional part of the average. - C
The result is automatically precise because the variable receiving it is a
double. - D
The program will not compile because arrays cannot be averaged.
What Java collection class is illustrated as a suitable choice when a program frequently adds or removes elements?
A method must increase every value in an array but must leave the caller's original array unchanged. What operation should it perform before updating the values?
When counting the scores in an array that meet a condition, a variable initialized to 0 and incremented once for each qualifying score serves as a .
A method receives a double[] parameter and increases every element by a percentage. Explain why calling the method can change the caller's original array, and describe how to implement the operation so that the original remains unchanged.