AP CSA Unit 4 Day 17: Array Reverse
Share
Unit 4, Data Collections • Cycle 2
Day 17 Advanced Practice • Harder Difficulty
Focus: Array Reverse
Hard
Array Reverse
Advanced Practice Question
Format: Array Reverse
What is the output?
What is the output?
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
System.out.println(Arrays.toString(arr));
Difficulty: Hard |
Topic: Array Reverse |
Cycle: 2 (Advanced)
Why This Answer?
Standard array reversal: swap first with last, second with second-to-last, etc. Only goes to length/2 to avoid double-swapping. Result: [5, 4, 3, 2, 1].
Common Mistake
Watch Out!
Looping to arr.length instead of arr.length/2, causing double-swap.
AP Exam Strategy
Reverse: loop to length/2, swap arr[i] with arr[length-1-i].
Master This Topic
This Cycle 2 HARD question tests array reverse. Review Unit 4 concepts to build mastery of data collections.
- Understanding array reverse
- Tracing code execution accurately
- Avoiding common pitfalls
Ready for More Challenges?
Cycle 2 questions prepare you for the hardest AP CSA exam questions.
Study Games Practice FRQs