Arrays as Parameters and Return Values (Bonus)

Unit 4 · Bonus Lesson · Arrays

Arrays as Parameters and Return Values

🕑 35–45 min · 10 Practice Questions · Reference Semantics · Method Design

What You'll Learn

  • 4.6.A: Pass an array as a method parameter.
  • 4.6.B: Return an array from a method.
  • Explain why passing an array passes a reference, not a copy.
  • Predict whether changes inside a method affect the original array.
  • Write methods that take arrays as input and return new arrays as output.

Key Vocabulary

Term Definition
pass by value How Java passes all arguments — a copy of the value is given to the method. For primitives, this means changes inside the method don't affect the original variable.
reference A variable that stores the memory address of an object or array, not the object itself. When you pass an array, you pass a copy of its reference — both variables point to the same array in memory.
alias Two variables that refer to the same object in memory. Modifying the object through one alias is visible through the other.

Passing an Array as a Parameter (4.6.A)

When you pass a primitive like int to a method, Java copies the value. Changes inside the method have no effect on the original variable.

public static void addFive(int x) {
    x = x + 5;  // only changes the local copy
}

int num = 10;
addFive(num);
System.out.println(num);  // still prints 10

Arrays work differently. An array variable stores a reference — a memory address pointing to the array's data. When you pass an array, Java copies the reference, not the array itself. Both the original variable and the parameter now point to the same array in memory.

public static void doubleAll(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
        arr[i] = arr[i] * 2;
    }
}

int[] scores = {10, 20, 30};
doubleAll(scores);
System.out.println(scores[0]);  // prints 20 — the original array was modified

📌 The Key Rule

Modifying elements of an array parameter (arr[i] = ...) changes the original array. Reassigning the parameter variable itself (arr = new int[5]) does not — that only changes what the local variable points to, leaving the original untouched.

Visualizing the Reference

scores [10, 20, 30]
memory address 0x4A2
arr (parameter)

Both scores and arr point to the same memory location. Any change to arr[i] inside the method is a change to scores[i] outside it.

Reassigning the Parameter Has No Effect

public static void tryToReplace(int[] arr) {
    arr = new int[]{99, 99, 99};  // only changes local variable
}

int[] data = {1, 2, 3};
tryToReplace(data);
System.out.println(data[0]);  // still prints 1

Inside tryToReplace, arr is redirected to a brand new array. But data back in the calling code still points to the original {1, 2, 3} array — the reassignment only affected the local copy of the reference.

⚠️ AP Exam Trap: Element Change vs. Reassignment

This is one of the most tested concepts involving arrays on the AP exam. The rule in two sentences: changing elements (arr[i] = x) affects the caller's array. Reassigning the parameter (arr = new int[3]) does not. Every AP question on this topic is testing whether you know the difference.

Returning an Array from a Method (4.6.B)

A method can return an array just like it returns any other value. Declare the return type with brackets: int[], String[], etc.

public static int[] makeDoubled(int[] arr) {
    int[] result = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        result[i] = arr[i] * 2;
    }
    return result;
}

Usage:

int[] original = {5, 10, 15};
int[] doubled = makeDoubled(original);
System.out.println(doubled[1]);   // prints 20
System.out.println(original[1]);  // still prints 10 — original unchanged

This pattern — create a new array inside the method, fill it, return it — is the standard way to produce a transformed version of an array without modifying the original.

✅ Example: Returning a Filtered Array

// Returns a new array containing only the positive values
public static int[] positives(int[] arr) {
    int count = 0;
    for (int x : arr) {
        if (x > 0) count++;
    }
    int[] result = new int[count];
    int idx = 0;
    for (int x : arr) {
        if (x > 0) {
            result[idx] = x;
            idx++;
        }
    }
    return result;
}

Two passes: first count how many qualify (to size the result array), then fill it. This two-pass filter pattern appears on AP FRQs.

Writing Complete Array Methods

Combining parameters and return values gives you full method design flexibility:

// Takes an array, returns a new array with each element replaced
// by the sum of itself and the next element.
// Last element wraps around to add arr[0].
public static int[] rollingSum(int[] arr) {
    int[] result = new int[arr.length];
    for (int i = 0; i < arr.length - 1; i++) {
        result[i] = arr[i] + arr[i + 1];
    }
    result[arr.length - 1] = arr[arr.length - 1] + arr[0];
    return result;
}

Summary

  • Java always passes by value. For arrays, the value passed is a reference (memory address).
  • Modifying array elements inside a method (arr[i] = x) does change the original array.
  • Reassigning the parameter variable (arr = new int[3]) does not change the original array.
  • Methods can return arrays by declaring a bracket return type: public static int[] method(...).
  • The standard pattern for a non-destructive transform: create a new array inside the method, fill it, return it.

Practice Questions

MCQ 1
What is printed?
public static void addTen(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
        arr[i] += 10;
    }
}

int[] nums = {1, 2, 3};
addTen(nums);
System.out.println(nums[0]);
Predict before reading the options.
A 1
B 11
C 10
D 3
B. The method receives a reference to the same array. Modifying arr[i] modifies nums[i] — they point to the same memory. nums[0] was 1, then += 10 makes it 11.
MCQ 2
What is printed?
public static void replace(int[] arr) {
    arr = new int[]{99, 99, 99};
}

int[] vals = {1, 2, 3};
replace(vals);
System.out.println(vals[0]);
Think carefully — this is the reassignment trap.
A 1
B 99
C 3
D 0
A. arr = new int[]{99, 99, 99} reassigns the local parameter variable — it now points to a new array. But vals in the calling code still points to the original {1, 2, 3}. The original is never touched. prints 1.
MCQ 3
What is the return type of a method that takes an int array and returns a new int array of the same size?
A int
B void
C int[]
D int[][]
C. To return an array of ints, the return type is int[]. The brackets are part of the type declaration. int would return a single integer. void returns nothing. int[][] is a 2D array (Lesson 4.11).
MCQ 4
What is printed?
public static int[] doubled(int[] arr) {
    int[] result = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        result[i] = arr[i] * 2;
    }
    return result;
}

int[] original = {3, 6, 9};
int[] copy = doubled(original);
System.out.println(original[1] + " " + copy[1]);
Predict both values before reading the options.
A 12 12
B 6 6
C 12 6
D 6 12
D. The method creates a brand new array result — it never modifies original. original[1] stays 6. copy[1] is 6 * 2 = 12. Output: 6 12.
MCQ 5
Which statement correctly describes what happens when an array is passed to a method?
A A full copy of the array's elements is made and given to the method.
B A copy of the reference (memory address) is passed; both variables point to the same array.
C The method receives the array directly; the original variable is temporarily suspended.
D Nothing is passed — arrays are always accessed globally.
B. Java always passes by value — but for arrays, the value is a reference (memory address). The method gets its own copy of that reference, but both copies point to the same underlying array. That's why element modifications affect the caller but reassignment does not.
Tier 3 · AP Mastery

Mastery: Arrays as Parameters and Return Values

MCQ 6
What is printed?
public static void mystery(int[] arr) {
    arr[0] = arr[0] + arr[arr.length - 1];
    arr = new int[]{0, 0, 0};
}

int[] data = {4, 8, 12};
mystery(data);
System.out.println(data[0] + " " + data[1]);
Two things happen in this method — trace both carefully.
A 0 0
B 4 8
C 16 8
D 0 8
C. Two things happen: first, arr[0] = 4 + 12 = 16 — this modifies the original array because it's an element change. Then arr = new int[]{0, 0, 0} — this reassigns the local parameter, so it has no effect on data. Final state of data: {16, 8, 12}. Output: 16 8.
MCQ 7
Which method correctly returns a new array that is a reversed copy of the input array?
Predict before reading the options.
A
public static int[] reverse(int[] arr) {
    int[] result = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        result[i] = arr[arr.length - 1 - i];
    }
    return result;
}
B
public static int[] reverse(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
        arr[i] = arr[arr.length - 1 - i];
    }
    return arr;
}
C
public static void reverse(int[] arr) {
    int[] result = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        result[i] = arr[arr.length - 1 - i];
    }
}
D
public static int[] reverse(int[] arr) {
    int[] result = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        result[i] = arr[i];
    }
    return result;
}
A. A creates a new array and fills it in reverse order correctly. B overwrites the original array in-place, but corrupts it — once arr[0] is overwritten, the original value is lost for filling result[arr.length-1]. C returns void — the result is lost. D copies in the same order — not reversed.
MCQ 8
What is printed?
public static int[] shiftLeft(int[] arr) {
    int[] result = new int[arr.length];
    for (int i = 0; i < arr.length - 1; i++) {
        result[i] = arr[i + 1];
    }
    result[arr.length - 1] = arr[0];
    return result;
}

int[] a = {10, 20, 30, 40};
int[] b = shiftLeft(a);
System.out.println(b[0] + " " + b[3]);
Trace the shift carefully before selecting.
A 10 40
B 40 10
C 20 40
D 20 10
D. shiftLeft shifts everything one position left and wraps the first element to the end. result[0] = arr[1] = 20. result[1] = arr[2] = 30. result[2] = arr[3] = 40. result[3] = arr[0] = 10. So b = {20, 30, 40, 10}. b[0]=20, b[3]=10. Output: 20 10.
MCQ 9
A method should modify the original array by setting every element to zero. Which implementation is correct?
A
public static void zeroOut(int[] arr) {
    arr = new int[arr.length];
}
B
public static void zeroOut(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
        arr[i] = 0;
    }
}
C
public static int[] zeroOut(int[] arr) {
    return new int[arr.length];
}
D
public static void zeroOut(int[] arr) {
    for (int x : arr) {
        x = 0;
    }
}
B. A reassigns the parameter — doesn't change the original. C returns a new zeroed array but doesn't modify the original (and the caller would have to use the return value). D uses for-each — assigning to the loop variable doesn't change the array. B directly sets each element via index — the only approach that modifies the original.
MCQ 10
What is printed?
public static int[] add(int[] a, int[] b) {
    int[] result = new int[a.length];
    for (int i = 0; i < a.length; i++) {
        result[i] = a[i] + b[i];
    }
    return result;
}

int[] x = {1, 2, 3};
int[] y = {4, 5, 6};
int[] z = add(x, y);
System.out.println(z[1] + " " + x[1]);
Predict both values before reading the options.
A 7 7
B 2 7
C 7 2
D 5 2
C. add creates a new array result — neither x nor y is modified. z[1] = x[1] + y[1] = 2 + 5 = 7. x[1] is still 2. Output: 7 2.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]