Developing Algorithms Using Arrays

AP Computer Science A· difficulty 3/5

int[] a = {1, 2, 3, 4};
int temp = a[0];
a[0] = a[3];
a[3] = temp;
System.out.println(a[0] + " " + a[3]);

What is printed?

  • A

    4 1

    check_circle
  • B

    1 4

  • C

    1 1

  • D

    4 4

Explanation

Standard swap pattern: save a[0] in temp, copy a[3] into a[0], then copy temp into a[3]. Result: a[0]=4, a[3]=1.

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

Related questions