Developing Algorithms Using Arrays

AP Computer Science A· difficulty 4/5

int[] a = {1, 2, 3, 4};
for (int i = 0; i < a.length - 1; i++) {
  a[i] = a[i + 1];
}
System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]);

What is printed?

  • A

    1 1 2 3

  • B

    2 3 4 0

  • C

    1 2 3 4

  • D

    2 3 4 4

    check_circle

Explanation

Each element is replaced by the one to its right; the last element is unchanged. After the loop: 2, 3, 4, 4.

Want 10 more like this — adaptive to your weak spots?

Related questions