Question 10
Back to Exercise
What is the output of the following Java program?
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] s = {"x", "y", "z"};
System.out.println(Arrays.toString(Arrays.copyOf(s, 5)));
System.out.println(Arrays.toString(Arrays.copyOf(s, 2)));
int[] n = {4, 5, 6};
System.out.println(Arrays.toString(Arrays.copyOfRange(n, 1, 5)));
}
}
- A [x, y, z, null, null] [x, y] [5, 6, 0, 0] Correct
- B [x, y, z, , ] [x, y] [5, 6]
- C [x, y, z] [x, y] [5, 6, 0, 0]
- D ArrayIndexOutOfBoundsException
Correct Answer: Option A
Extra positions get default values; copyOfRange may extend past the end, padding with defaults too.
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!