Array traversal means going through each element in an array one by one to read or process them.
Click to reveal answer
beginner
How do you traverse an array using a for loop in Java?
You use a for loop with an index starting at 0 up to the array length minus one, accessing each element by its index.
Click to reveal answer
intermediate
What is the difference between for loop and for-each loop for array traversal?
A for loop uses an index to access elements, allowing control over the position. A for-each loop automatically goes through each element without using an index.
Click to reveal answer
beginner
Write a simple Java code snippet to print all elements of an integer array using a for-each loop.
int[] numbers = {1, 2, 3};
for (int num : numbers) {
System.out.println(num);
}
Click to reveal answer
beginner
Why is it important to use the array's length property in traversal loops?
Using the array's length ensures you do not go past the last element, preventing errors like ArrayIndexOutOfBoundsException.
Click to reveal answer
Which loop is best when you need the index of each element during array traversal?
Afor loop
Bfor-each loop
Cwhile loop without index
Ddo-while loop without index
✗ Incorrect
A for loop uses an index variable, so you can access the position of each element.
What happens if you try to access an array element with an index equal to the array's length?
AArrayIndexOutOfBoundsException is thrown
BYou get a default value
CYou get the last element
DThe program ignores it
✗ Incorrect
Array indices go from 0 to length-1. Accessing index equal to length causes an error.
Which of these is a valid way to traverse an array in Java?
Afor (int i = 0; i <= arr.length; i++)
Bfor (int i = arr.length; i > 0; i++)
Cfor (int i = 1; i < arr.length; i++)
Dfor (int i = 0; i < arr.length; i++)
✗ Incorrect
The loop should start at 0 and run while i is less than length to cover all elements.
What keyword is used in Java to loop through each element of an array without using an index?
Afor
Bfor-each
Cfor (type var : array)
Dforeach
✗ Incorrect
Java uses the syntax for (type var : array) to loop through elements directly.
Which of these is NOT a benefit of array traversal?
AAccessing each element to read or modify
BSkipping elements randomly
CPerforming calculations on elements
DPrinting all elements
✗ Incorrect
Traversal means going through elements one by one, not skipping randomly.
Explain how to traverse an array in Java using a for loop. Include why the loop limits are important.
Think about how array indices work and how to avoid going out of bounds.
You got /4 concepts.
Describe the difference between a for loop and a for-each loop when traversing arrays in Java.
Consider if you need the position of elements or just the values.
You got /4 concepts.
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
Step 1: Understand array traversal meaning
Array traversal means going through each element in the array one by one.
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.
Final Answer:
Visiting each element of the array one by one using a loop -> Option B
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
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.
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.
Final Answer:
for(int i = 0; i < nums.length; i++) { System.out.println(nums[i]); } -> Option A
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
Step 1: Understand the loop iteration
The loop runs from i=0 to i=2 (arr.length=3), printing arr[i] each time.
Step 2: Check printed values
Values printed are arr[0]=5, arr[1]=10, arr[2]=15, each followed by a space.
Final Answer:
5 10 15 -> Option A
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
Step 1: Analyze loop condition
The loop runs while i <= data.length, which means i can be equal to data.length (3).
Step 2: Check array index access
Accessing data[3] causes ArrayIndexOutOfBoundsException because valid indices are 0 to 2.
Final Answer:
Loop condition should be i < data.length, not <= data.length -> Option C
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
Step 1: Traverse the array correctly
Loop from i=0 to i < nums.length to visit all elements safely.
Step 2: Check condition for even numbers
Use nums[i] % 2 == 0 to find even numbers and print them.
Final Answer:
for(int i = 0; i < nums.length; i++) { if(nums[i] % 2 == 0) System.out.print(nums[i] + " "); } -> Option D