Courseiva
Arrays and MethodsmediumMultiple ChoiceObjective-mapped

1Z0-811 Arrays and Methods Practice Question

A method 'public static void sort(int[] arr)' sorts the array in place. After calling 'sort(data)', the original array 'data' is changed. Which design issue does this demonstrate?

⚠ Common exam trap

Many candidates confuse pass-by-value for objects with pass-by-reference, thinking that Java passes objects by reference, which would make modification expected, but the real issue is that the method's side effect violates the principle of least surprise and should be avoided by returning a new array.

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

The method should return a new array to avoid side effects.

The method modifies the original array passed as an argument, which is a side effect. In Java, arrays are objects, and when passed to a method, the reference is passed by value, allowing the method to modify the array's contents. To avoid side effects and maintain immutability, the method should return a new sorted array instead of modifying the original.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The method should use a wrapper class for the array.

    Why it's wrong here

    Wrapper classes do not solve this problem.

  • The method should be declared as void to indicate modification.

    Why it's wrong here

    void return does not prevent side effects.

  • The method should return a new array to avoid side effects.

    Why this is correct

    Changing the original array can lead to unexpected behavior; returning a new array is a common practice.

  • The method should use pass-by-value to avoid modification.

    Why it's wrong here

    Java always uses pass-by-value; the reference is copied, but the object is shared.

About these practice questions

Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.