LearnFrenzy
Sign in Take a free test

Arrays in Java · Predict the Output · Question 3

A method that modifies, then reassigns, its array Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    static void change(int[] arr) {
        arr[0] = 99;
        arr = new int[]{7, 7, 7};
        arr[1] = 55;
    }

    public static void main(String[] args) {
        int[] data = {1, 2, 3};
        change(data);
        System.out.println(Arrays.toString(data));
    }
}
  • A [99, 2, 3] Correct
  • B [7, 55, 7]
  • C [1, 2, 3]
  • D [99, 55, 3]
Correct Answer: Option A

The first change goes to the caller's array; after arr is re-pointed, changes affect only the new local array.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links