Bird
Raised Fist0
Javaprogramming~10 mins

Array traversal in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array traversal
Start at index 0
↓
Check: index < array.length?
No→EXIT
Yes↓
Access array[index
↓
Process element
↓
Increment index
↩Back to Check
Start from the first element, check if the index is within array bounds, process the element, then move to the next index until all elements are visited.
Execution Sample
Java
int[] arr = {10, 20, 30};
for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
}
This code prints each element of the array one by one.
Execution Table
IterationiCondition (i < arr.length)ActionOutput
100 < 3 = truePrint arr[0] = 1010
211 < 3 = truePrint arr[1] = 2020
322 < 3 = truePrint arr[2] = 3030
433 < 3 = falseExit loop
💡 i reaches 3, condition 3 < 3 is false, loop ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
i0 (initialized)1233
arr[i]arr[0] = 10arr[1] = 20arr[2] = 30N/AN/A
Key Moments - 3 Insights
Why does the loop stop when i equals the array length?
Because array indices start at 0, the last valid index is length - 1. When i equals length, it is outside the array bounds, so the condition i < arr.length becomes false (see execution_table row 4).
What happens if we try to access arr[i] when i equals the array length?
It causes an error called ArrayIndexOutOfBoundsException because that index does not exist. The loop condition prevents this by stopping before i reaches arr.length (see execution_table rows 3 and 4).
Why do we start the index i at 0 instead of 1?
Arrays in Java start at index 0, so the first element is at arr[0]. Starting at 0 ensures we access all elements from the beginning (see execution_table row 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i during the third iteration?
A1
B2
C3
D0
💡 Hint
Check the 'i' column in execution_table row 3.
At which iteration does the loop condition become false?
AIteration 4
BIteration 3
CIteration 2
DIteration 1
💡 Hint
Look at the 'Condition' column in execution_table row 4.
If the array had 5 elements instead of 3, how many times would the loop run?
A3 times
B4 times
C5 times
D6 times
💡 Hint
The loop runs once for each element, so it runs arr.length times.
Concept Snapshot
Array traversal in Java:
- Use a for loop with index i from 0 to i < array.length
- Access elements as array[i]
- Loop stops before i reaches array.length
- Prevents out-of-bounds errors
- Prints or processes each element in order
Full Transcript
This visual execution trace shows how to traverse an array in Java using a for loop. We start with index i at 0, check if i is less than the array length, then access and print the element at arr[i]. After processing, we increment i by 1 and repeat. The loop stops when i equals the array length, preventing errors. The variable tracker shows how i and arr[i] change each iteration. Key moments clarify why the loop stops at length and why indexing starts at 0. The quiz tests understanding of loop steps and conditions.

Practice

(1/5)
1. What does array traversal mean in Java?
int[] arr = {1, 2, 3};
easy
A. Creating a new array with the same elements
B. Visiting each element of the array one by one using a loop
C. Sorting the array elements in ascending order
D. Deleting elements from the array

Solution

  1. Step 1: Understand array traversal meaning

    Array traversal means going through each element in the array one by one.
  2. Step 2: Match with given options

    Only Visiting each element of the array one by one using a loop describes visiting each element using a loop, which is traversal.
  3. Final Answer:

    Visiting each element of the array one by one using a loop -> Option B
  4. Quick Check:

    Array traversal = visiting elements one by one [OK]
Hint: Traversal means visiting all elements step-by-step [OK]
Common Mistakes:
  • Confusing traversal with sorting
  • Thinking traversal means deleting elements
  • Mixing traversal with creating new arrays
2. Which of the following is the correct syntax to traverse an array int[] nums in Java?
easy
A. for(int i = 0; i < nums.length; i++) { System.out.println(nums[i]); }
B. for(int i = 1; i <= nums.length; i++) { System.out.println(nums[i]); }
C. for(int i = 0; i <= nums.length; i++) { System.out.println(nums[i]); }
D. for(int i = 0; i < nums.length - 1; i++) { System.out.println(nums[i]); }

Solution

  1. Step 1: Check loop start and end conditions

    The loop should start at 0 and run while i is less than nums.length to cover all indices.
  2. Step 2: Validate array access inside loop

    Accessing nums[i] is correct if i is within 0 to length-1. for(int i = 0; i < nums.length; i++) { System.out.println(nums[i]); } satisfies this.
  3. Final Answer:

    for(int i = 0; i < nums.length; i++) { System.out.println(nums[i]); } -> Option A
  4. Quick Check:

    Loop from 0 to length-1 = correct traversal [OK]
Hint: Loop from 0 to less than length for full traversal [OK]
Common Mistakes:
  • Starting loop at 1 instead of 0
  • Using <= length causing index out of bounds
  • Stopping loop early at length - 1
3. What will be the output of the following code?
int[] arr = {5, 10, 15};
for(int i = 0; i < arr.length; i++) {
  System.out.print(arr[i] + " ");
}
medium
A. 5 10 15
B. 15 10 5
C. 5 10 15 0
D. Error: ArrayIndexOutOfBoundsException

Solution

  1. Step 1: Understand the loop iteration

    The loop runs from i=0 to i=2 (arr.length=3), printing arr[i] each time.
  2. Step 2: Check printed values

    Values printed are arr[0]=5, arr[1]=10, arr[2]=15, each followed by a space.
  3. Final Answer:

    5 10 15 -> Option A
  4. Quick Check:

    Print elements in order = 5 10 15 [OK]
Hint: Prints elements in order from index 0 to length-1 [OK]
Common Mistakes:
  • Assuming reverse order output
  • Expecting extra zero at end
  • Thinking loop causes index error
4. Identify the error in this array traversal code:
int[] data = {2, 4, 6};
for(int i = 0; i <= data.length; i++) {
  System.out.println(data[i]);
}
medium
A. System.out.println should be outside the loop
B. Array declaration is incorrect
C. Loop condition should be i < data.length, not <= data.length
D. No error, code runs fine

Solution

  1. Step 1: Analyze loop condition

    The loop runs while i <= data.length, which means i can be equal to data.length (3).
  2. Step 2: Check array index access

    Accessing data[3] causes ArrayIndexOutOfBoundsException because valid indices are 0 to 2.
  3. Final Answer:

    Loop condition should be i < data.length, not <= data.length -> Option C
  4. Quick Check:

    Index must be less than length to avoid error [OK]
Hint: Use i < length, not i <= length in loops [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Ignoring array index limits
  • Thinking loop runs without error
5. Given an array int[] nums = {3, 6, 9, 12, 15};, write code to print only the even numbers by traversing the array.
hard
A. for(int i = 0; i < nums.length - 1; i++) { System.out.print(nums[i] + " "); }
B. for(int i = 1; i <= nums.length; i++) { if(nums[i] % 2 == 0) System.out.print(nums[i] + " "); }
C. for(int i = 0; i < nums.length; i++) { if(nums[i] % 2 != 0) System.out.print(nums[i] + " "); }
D. for(int i = 0; i < nums.length; i++) { if(nums[i] % 2 == 0) System.out.print(nums[i] + " "); }

Solution

  1. Step 1: Traverse the array correctly

    Loop from i=0 to i < nums.length to visit all elements safely.
  2. Step 2: Check condition for even numbers

    Use nums[i] % 2 == 0 to find even numbers and print them.
  3. Final Answer:

    for(int i = 0; i < nums.length; i++) { if(nums[i] % 2 == 0) System.out.print(nums[i] + " "); } -> Option D
  4. Quick Check:

    Traverse all and print only even numbers [OK]
Hint: Check nums[i] % 2 == 0 inside loop to print evens [OK]
Common Mistakes:
  • Using wrong loop bounds causing errors
  • Printing odd numbers instead of even
  • Missing condition inside loop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard