1Z0-811 Arrays and Methods Practice Question
Consider the following code that attempts to swap the first two elements of an array using a method:
public static void swap(int[] arr) {
int temp = arr[0];arr[0] = arr[1]; arr[1] = temp;
}
public static void main(String[] args) {int[] values = {1, 2}; swap(values); System.out.println(values[0] + " " + values[1]);
}
What is the output?
⚠ Common exam trap
The 1Z0-811 exam often tests the misconception that Java passes objects by value in a way that prevents modification, leading candidates to incorrectly believe the array remains unchanged (Option C) or that the code fails to compile (Option B).
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
✓
2 1
The `swap` method receives a reference to the array, so modifications to its elements (via index assignment) affect the original array. After swapping, `values[0]` becomes 2 and `values[1]` becomes 1, producing the output "2 1".
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
2 1
Why this is correct
The method modifies the array elements, so after swap, values[0] becomes 2 and values[1] becomes 1.
- ✗
The code does not compile.
Why it's wrong here
The code compiles and runs without error.
- ✗
1 2
Why it's wrong here
This would be the output if the swap did not happen, but it does.
- ✗
A runtime exception occurs.
Why it's wrong here
No exception occurs; the array is accessed correctly.
Go deeper
Related to this question
About these practice questions
One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
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.