Free Practice Quiz Question List

Java One-Dimensional Arrays: Traversal and Algorithms Online Quiz Questions

Use this free practice quiz with 30 questions to review Java One-Dimensional Arrays: Traversal and Algorithms, test your knowledge, and prepare for your next test or exam.

30 questions
01
Choose one
1 point

A Java array has length 5. Which range contains all and only its valid indices?

  1. A

    0 through 5

  2. B

    0 through 4

  3. C

    1 through 5

  4. D

    1 through 4

02
True or false
1 point

True or false: After executing for (int value : values) { value = value * 2; }, the elements of the int[] values array have been doubled.

  1. A

    True

  2. B

    False

03
Fill in the blank
1 point

Complete the statement with the Java keyword that creates an array object: int[] scores = int[5];

04
Choose one
1 point

What does countEven return for the array {12, 7, 19, 4, 10}?

  1. A

    2

  2. B

    4

  3. C

    3

  4. D

    5

05
Choose all
1 point

Select all statements that are correct about empty Java arrays.

  1. A

    A loop using index < empty.length performs zero iterations.

  2. B

    empty[0] is a valid way to inspect an empty array.

  3. C

    A maximum algorithm initialized with values[0] requires a nonempty array.

  4. D

    An empty array can be created with new int[0].

06
True or false
1 point

True or false: A method that searches for a target index can return -1 to indicate that the target does not occur in the array.

  1. A

    True

  2. B

    False

07
Written response
1 point

What is the average of the elements in {12, 7, 19, 4, 10}? Enter the exact decimal value; no rounding tolerance is allowed.

08
Fill in the blank
1 point

Complete the typical initial values: initialize a counter with and initialize a product accumulator with .

09
Choose one
1 point

Which initialization is the safest starting point for finding the maximum of a known nonempty int[] values array?

  1. A

    Set max to 0 before the loop.

  2. B

    Set max to the last index of the array.

  3. C

    Set max to values[0] and compare the remaining elements.

  4. D

    Set max to values.length before the loop.

10
Choose all
1 point

Select all tasks for which an indexed for loop is an appropriate choice when processing a one-dimensional array.

  1. A

    Replace every element with a transformed value.

  2. B

    Use the position of each element in a calculation.

  3. C

    Read every element without changing it when no index is needed.

  4. D

    Stop as soon as a condition is met using return or break.

11
Open ended
1 point

Explain how this method works and determine its return value for {3, -2, 5, 0}:

public static int sumPositive(int[] values) { int total = 0; for (int value : values) { if (value > 0) { total += value; } } return total; }

12
Choose one
1 point

For int[] values = new int[4];, which array access is invalid?

  1. A

    values[0]

  2. B

    values[3]

  3. C

    values[-1]

  4. D

    values[2]

13
True or false
1 point

True or false: If values is an int[], then the expression values[2] has type int and refers to one element rather than the entire array.

  1. A

    True

  2. B

    False

14
Choose one
1 point

An array is created with length 7. Which range contains exactly all of its valid indices?

  1. A

    0 through 7

  2. B

    0 through 6

  3. C

    1 through 7

  4. D

    1 through 6

15
Choose one
1 point

What happens when Java executes the declaration int[] scores; by itself?

  1. A

    It creates an array with one element.

  2. B

    It creates an array whose length is inferred later.

  3. C

    It only declares a reference variable; no array object is created.

  4. D

    It creates an array initialized with zeros.

16
True or false
1 point

True or false: In a loop that accesses values[index], using index <= values.length is safe for traversing every element of the array.

  1. A

    True

  2. B

    False

17
True or false
1 point

True or false: A linear search that returns true immediately when it finds the target can avoid examining elements after the first match.

  1. A

    True

  2. B

    False

18
Written response
1 point

What exact Java exception class name is caused by evaluating values[values.length]?

19
Choose one
1 point

Which statement correctly declares and creates an integer array with exactly four elements?

  1. A

    int[] scores;

  2. B

    int[] scores = new int[4];

  3. C

    int scores = new int[4];

  4. D

    int[] scores = int[4];

20
Choose one
1 point

A method is guaranteed to receive a nonempty int[] values and must return its maximum. Which initialization is the most generally reliable before traversing the remaining elements?

  1. A

    Start with max = 0, because zero is always a valid integer.

  2. B

    Start with max = values[values.length], because that is the final position.

  3. C

    Start with max = values[0], because it is an actual element and the array is nonempty.

  4. D

    Start with max = Integer.MIN_VALUE, because arbitrary constants are always required.

21
Written response
1 point

What Java attribute gives the number of elements in an array?

22
Fill in the blank
1 point

Complete the two missing parts of this minimum-algorithm description for a nonempty array named values: initialize the candidate with , then begin comparing the remaining elements at index .

23
Choose one
1 point

What is printed by this Java code?

int[] values = {1, 2, 3, 4};
int product = 1;
for (int value : values) {
product *= value;
}
System.out.println(product);

  1. A

    6

  2. B

    24

  3. C

    10

  4. D

    12

24
Choose one
1 point

After this code executes, what is the contents of values?

int[] values = {5, 6, 7, 8};
for (int index = 0; index < values.length; index++) {
values[index] = values[index] + 2 * index;
}

  1. A

    [5, 6, 7, 8]

  2. B

    [5, 7, 9, 10]

  3. C

    [5, 7, 9, 11]

  4. D

    [5, 8, 11, 14]

25
Choose one
1 point

What value does total have when this code finishes?

int[] values = {3, 5, 7, 9, 11};
int index = 0;
int total = 0;
while (index < values.length) {
total += values[index];
index += 2;
}

  1. A

    21

  2. B

    25

  3. C

    30

  4. D

    36

26
Choose one
1 point

What is the contents of values after this code executes?

int[] values = {1, 2, 3, 4};
for (int index = 0; index < values.length; index += 2) {
int temporary = values[index];
values[index] = values[index + 1];
values[index + 1] = temporary;
}

  1. A

    [1, 2, 3, 4]

  2. B

    [2, 1, 3, 4]

  3. C

    [1, 2, 4, 3]

  4. D

    [2, 1, 4, 3]

27
Choose all
1 point

Select all statements that are true about this code.

int[] values = {3, 2, 4};
for (int index = 0; index < values.length; index++) {
if (index > 0) {
values[index] = values[index] + values[index - 1];
}
}

  1. A

    The final array is [4, 6, 10].

  2. B

    The final array is [3, 5, 7].

  3. C

    The assignment at index 1 uses the already updated value at index 0.

  4. D

    The loop executes exactly three times.

28
Written response
1 point

What integer does this method return? Enter the sum as a decimal number.

public static int total(int[] values) {
int result = 0;
for (int index = 0; index < values.length; index++) {
result += values[index];
}
return result;
}

For values = {6, 8, 10, 6}, enter the exact integer result.

29
Written response
1 point

What integer does this method return? Enter the exact product as a decimal number.

public static int multiply(int[] values) {
int result = 1;
for (int value : values) {
result *= value;
}
return result;
}

For values = {2, 3, 1, 8}, enter the exact integer result.

30
Written response
1 point

After this code executes, what integer is stored at values[3]? Enter the final element as an integer.

int[] values = {2, 4, 6, 5};
for (int index = 0; index < values.length; index++) {
values[index] = values[index] + 4 * index;
}