A Java array has length 5. Which range contains all and only its valid indices?
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.
True or false: After executing for (int value : values) { value = value * 2; }, the elements of the int[] values array have been doubled.
- A
True
- B
False
Complete the statement with the Java keyword that creates an array object: int[] scores = int[5];
What does countEven return for the array {12, 7, 19, 4, 10}?
- A
2
- B
4
- C
3
- D
5
Select all statements that are correct about empty Java arrays.
- A
A loop using
index < empty.lengthperforms zero iterations. - B
empty[0]is a valid way to inspect an empty array. - C
A maximum algorithm initialized with
values[0]requires a nonempty array. - D
An empty array can be created with
new int[0].
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.
- A
True
- B
False
What is the average of the elements in {12, 7, 19, 4, 10}? Enter the exact decimal value; no rounding tolerance is allowed.
Complete the typical initial values: initialize a counter with and initialize a product accumulator with .
Which initialization is the safest starting point for finding the maximum of a known nonempty int[] values array?
- A
Set
maxto 0 before the loop. - B
Set
maxto the last index of the array. - C
Set
maxtovalues[0]and compare the remaining elements. - D
Set
maxtovalues.lengthbefore the loop.
Select all tasks for which an indexed for loop is an appropriate choice when processing a one-dimensional array.
- A
Replace every element with a transformed value.
- B
Use the position of each element in a calculation.
- C
Read every element without changing it when no index is needed.
- D
Stop as soon as a condition is met using
returnorbreak.
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;
}
For int[] values = new int[4];, which array access is invalid?
- A
values[0] - B
values[3] - C
values[-1] - D
values[2]
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.
- A
True
- B
False
An array is created with length 7. Which range contains exactly all of its valid indices?
- A
0 through 7
- B
0 through 6
- C
1 through 7
- D
1 through 6
What happens when Java executes the declaration int[] scores; by itself?
- A
It creates an array with one element.
- B
It creates an array whose length is inferred later.
- C
It only declares a reference variable; no array object is created.
- D
It creates an array initialized with zeros.
True or false: In a loop that accesses values[index], using index <= values.length is safe for traversing every element of the array.
- A
True
- B
False
True or false: A linear search that returns true immediately when it finds the target can avoid examining elements after the first match.
- A
True
- B
False
What exact Java exception class name is caused by evaluating values[values.length]?
Which statement correctly declares and creates an integer array with exactly four elements?
- A
int[] scores; - B
int[] scores = new int[4]; - C
int scores = new int[4]; - D
int[] scores = int[4];
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?
- A
Start with
max = 0, because zero is always a valid integer. - B
Start with
max = values[values.length], because that is the final position. - C
Start with
max = values[0], because it is an actual element and the array is nonempty. - D
Start with
max = Integer.MIN_VALUE, because arbitrary constants are always required.
What Java attribute gives the number of elements in an array?
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 .
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);
- A
6
- B
24
- C
10
- D
12
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;}
- A
[5, 6, 7, 8]
- B
[5, 7, 9, 10]
- C
[5, 7, 9, 11]
- D
[5, 8, 11, 14]
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;}
- A
21
- B
25
- C
30
- D
36
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;}
- A
[1, 2, 3, 4]
- B
[2, 1, 3, 4]
- C
[1, 2, 4, 3]
- D
[2, 1, 4, 3]
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]; }}
- A
The final array is [4, 6, 10].
- B
The final array is [3, 5, 7].
- C
The assignment at index 1 uses the already updated value at index 0.
- D
The loop executes exactly three times.
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.
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.
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;}
Related study packs and tools
1. Computing, Culture, and Society
A progressive guide to how computing reshapes culture, economic life, public institutions, and individual opportunities, with frameworks for evaluating digital equity, bias, cybersecurity, encryption, and collective participation.
01. Introduction to Programming and Algorithmic Problem Solving
A medium-difficulty assessment of programming fundamentals, algorithmic problem solving, control flow, data structures, testing, debugging, complexity, and decidability. Questions progress from foundational understanding to applied reasoning.
3. Risks and Unintended Consequences
A 14-question quiz on the social, economic, privacy, informational, health, and environmental risks of computing innovations, including strategies for responsible design and oversight.
6. Cybersecurity and Digital Safety
A practical guide to recognizing cybersecurity threats, protecting accounts and devices, and responding safely when information or systems may be compromised.
7. Encryption, Privacy, and Trust
A guided introduction to how encryption, authentication, hashing, secure protocols, and key management establish confidentiality, integrity, privacy, and trust while creating practical and legal trade-offs.
08 — Discrete Mathematics for Computer Science
A focused review of propositional logic, quantifiers, sets, relations, functions, and foundational proof methods in discrete mathematics for computer science.
9. Evaluating the Social Impact of Computing
A practical guide to evaluating computing innovations by examining their benefits, risks, equity, security, participation, and long-term effects on people and communities.
12 — Foundations Capstone
A medium-difficulty quiz on integrating problem definition, representation, algorithms, complexity analysis, testing, correctness, ethical evaluation, and documentation in a computer science capstone.
Fault Tolerance and Redundant Systems
A progressive guide to designing, operating, and testing systems that continue useful service despite component faults, overload, data loss, and recovery challenges.
How the Internet Works
A medium-difficulty quiz on how independently operated networks, protocols, addresses, naming systems, transport services, and routing mechanisms work together to form the Internet.
Packets, Links, and Network Performance
A progressive guide to how packet-switched networks encapsulate data, move it across links, and manage delay, throughput, queues, and congestion.
Parallel and Distributed Computing
A structured guide to designing parallel and distributed systems through decomposition, communication, coordination, state management, scalability, and fault tolerance.