Question 8
Back to Exercise
What is the output of the following Java program?
import java.util.Arrays;
class Scores {
int[] values;
Scores(int... v) { values = v; }
Scores(Scores other, boolean deep) {
values = deep ? other.values.clone() : other.values;
}
}
public class Main {
public static void main(String[] args) {
Scores a = new Scores(10, 20);
Scores shallow = new Scores(a, false), deep = new Scores(a, true);
a.values[0] = 99;
System.out.println(Arrays.toString(shallow.values) + " " + Arrays.toString(deep.values));
}
}
- A [99, 20] [10, 20] Correct
- B [10, 20] [10, 20]
- C [99, 20] [99, 20]
- D [10, 20] [99, 20]
Correct Answer: Option A
The shallow copy shares the original array; the deep copy has its own clone.
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!