Bird
0
0

Which of the following for loops correctly traverses the array int[] arr from start to end?

easy📝 Syntax Q3 of 15
Java - Arrays
Which of the following for loops correctly traverses the array int[] arr from start to end?
Afor(int i = 0; i < arr.length; i++) { System.out.print(arr[i]); }
Bfor(int i = 1; i <= arr.length; i++) { System.out.print(arr[i]); }
Cfor(int i = arr.length; i > 0; i--) { System.out.print(arr[i]); }
Dfor(int i = 0; i <= arr.length; i++) { System.out.print(arr[i]); }
Step-by-Step Solution
Solution:
  1. Step 1: Understand array indexing

    Array indices start at 0 and go up to length-1.
  2. Step 2: Check loop conditions

    Loop must run from 0 to less than arr.length to avoid IndexOutOfBoundsException.
  3. Final Answer:

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

    Loop uses i < arr.length, correct indexing [OK]
Quick Trick: Use i < array.length for safe traversal [OK]
Common Mistakes:
  • Using i <= arr.length causes IndexOutOfBoundsException
  • Starting loop at 1 misses first element
  • Using i > 0 without correct indexing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes