The four traversal modes
Forward, for-each, reverse, and pairwise โ and when each is the wrong tool.
Arrays are the exam's heaviest topic (Data Collections is 30โ40% of the MCQ section). Every array question is a traversal in one of four modes โ name the mode before writing code:
Forward index scan โ the default; use when you need indexes:
for (int i = 0; i < arr.length; i++) { ... arr[i] ... }
For-each โ when you only read values, never indexes:
for (int v : arr) { ... }
Reverse scan โ when removals or right-to-left order matter:
for (int i = arr.length - 1; i >= 0; i--) { ... }
Pairwise (adjacent) scan โ neighbor logic; loop to length - 1
(and read arr[i + 1]), or start at 1 and read arr[i - 1]:
for (int i = 0; i < arr.length - 1; i++) { compare arr[i] and arr[i + 1]; }
Choosing the wrong mode is the #1 structural mistake: modifying while
forward-iterating, or needing the index inside a for-each (impossible โ
the loop variable is a copy of the element, so v = 5 changes
nothing).
Boundary drill: for {10, 20, 30} โ arr.length is 3, valid
indexes are 0..2, arr[arr.length - 1] is the last element, and
arr[arr.length] throws ArrayIndexOutOfBoundsException. Every MCQ
that "throws an exception" is testing exactly this.