Consider the following code segment.
int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int k = 3; k < arr.length - 1; k++) arr[k] = arr[k + 1]; Which of the following represents the contents of arr as a result of executing the code segment?
a. {1, 2, 3, 5, 6, 7, 8}
b. {1, 2, 3, 4, 5, 6, 7}
c. {1, 2, 3, 5, 6, 7}
d. {1, 2, 3, 5, 6, 7, 7}
e. {2, 3, 4, 5, 6, 7, 7}
Answer: d. {1, 2, 3, 5, 6, 7, 7}
Code:
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5,
6, 7};
for (int k = 3; k <
arr.length - 1; k++) arr[k] = arr[k + 1];
System.out.println(Arrays.toString(arr));
}
}
Result:
Get Answers For Free
Most questions answered within 1 hours.