Question 327 of 481
1Z0-811 Arrays and Methods Practice Question
A new developer writes a method that accepts an array and intends to swap the first and last elements. The code is: public static void swapEnds(int[] arr) { int temp = arr[0]; arr[0] = arr[arr.length-1]; arr[arr.length-1] = temp; } What is a potential issue with this method if the array is empty?
⚠ Common exam trap
Oracle often tests the distinction between a `null` reference and an empty array, leading candidates to mistakenly think an empty array causes a `NullPointerException` instead of an `ArrayIndexOutOfBoundsException`.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
It will throw an ArrayIndexOutOfBoundsException.
When the array is empty, `arr.length` is 0, so `arr.length - 1` evaluates to -1. Accessing `arr[-1]` throws an `ArrayIndexOutOfBoundsException` because array indices must be non-negative and less than the array length. The method does not check for an empty array before attempting the swap.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
It will throw a NullPointerException.
Why it's wrong here
NullPointerException occurs if arr is null, not for empty array.
- ✓
It will throw an ArrayIndexOutOfBoundsException.
Why this is correct
Accessing index 0 and -1 on an empty array throws this exception.
- ✗
It will produce incorrect result because arr.length-1 is negative.
Why it's wrong here
It throws an exception, not an incorrect result.
- ✗
It will compile and run correctly.
Why it's wrong here
It will throw an exception at runtime.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jun 30, 2026
This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.