Free Practice Quiz Question List

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.

20 questions
01
Choose one
1 point

Which expression gives the final valid index of a nonempty Java array?

  1. A

    The final valid index is array.length.

  2. B

    The final valid index is array.length - 1.

  3. C

    The final valid index is array.length + 1.

  4. D

    The final valid index is always 5.

02
Choose all
1 point

Which two Java statements create an array object rather than merely declaring an array variable? Select all that apply.

  1. A

    int[] values;

  2. B

    int[] values = new int[4];

  3. C

    int[] values = {2, 4, 6, 8};

  4. D

    int values = new int[4];

03
Choose one
1 point

Given int[] values = {3, 6, 9, 12};, which expression correctly obtains the number of elements?

  1. A

    values.size()

  2. B

    values.length

  3. C

    values.length()

  4. D

    values.count()

04
Written response
1 point

Given int[] scores = {82, 91, 76, 88, 95};, what index contains the value 95?

05
Fill in the blank
1 point

Complete the statement: In Java, the final valid array index is array..

06
True or false
1 point

True or false: In Java, assigning a new value to the variable in for (int score : scores) updates the corresponding element of scores.

  1. A

    True

  2. B

    False

07
Choose one
1 point

Which loop correctly increases every element of an existing int[] scores array by 5?

  1. A

    for (int score : scores) { score += 5; }

  2. B

    for (int score : scores) { scores = score + 5; }

  3. C

    for (int i = 0; i < scores.length; i++) { scores[i] += 5; }

  4. D

    for (int i = 1; i <= scores.length; i++) { scores[i] += 5; }

08
Choose all
1 point

Which two statements about searching Java arrays are correct? Select all that apply.

  1. A

    A linear search can be used on an unsorted array.

  2. B

    A binary search is valid on any unsorted array.

  3. C

    A linear search can stop after finding the target.

  4. D

    A for-each loop by itself automatically returns the target index.

09
Written response
1 point

In the linear-search pattern from the material, what integer sentinel represents that the target was not found?

10
True or false
1 point

True or false: An ArrayList is generally a better choice than an array when a program frequently adds and removes elements.

  1. A

    True

  2. B

    False

11
Choose one
1 point

Given int[] scores = {82, 91, 76, 88, 95};, which expression accesses the first element of the array?

  1. A

    scores[1]

  2. B

    scores[0]

  3. C

    scores[-1]

  4. D

    scores[scores.length]

12
Choose one
1 point

What values are initially stored in the elements of int[] values = new int[4];?

  1. A

    The array cannot be used until each position is assigned.

  2. B

    Each element contains null.

  3. C

    Each element contains 0.

  4. D

    Each element contains an unspecified random integer.

13
Choose one
1 point

Which expression correctly obtains the number of elements in int[] temperatures = new int[7];?

  1. A

    temperatures.length

  2. B

    temperatures.length()

  3. C

    temperatures.size

  4. D

    temperatures.size()

14
Choose one
1 point

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?

  1. A

    Binary search, because it works on every array

  2. B

    Linear search, because it works whether or not the array is sorted

  3. C

    Binary search, because it always examines every element

  4. D

    Neither search, because Java arrays cannot be searched

15
True or false
1 point

True or false: Arrays.binarySearch can reliably search an unsorted array without any additional conditions.

  1. A

    True

  2. B

    False

16
Choose one
1 point

Why is (double) sum / scores.length used when computing the average of an int array?

  1. A

    The result is always rounded up.

  2. B

    Casting sum to double makes the division floating-point, preserving any fractional part of the average.

  3. C

    The result is automatically precise because the variable receiving it is a double.

  4. D

    The program will not compile because arrays cannot be averaged.

17
Written response
1 point

What Java collection class is illustrated as a suitable choice when a program frequently adds or removes elements?

18
Written response
1 point

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?

19
Fill in the blank
1 point

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 .

20
Open ended
1 point

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.