Developing Algorithms Using Arrays

AP Computer Science A· difficulty 2/5

int[] a = {10, 20, 30, 40};
for (int i = a.length - 1; i >= 0; i--) {
  System.out.print(a[i] + " ");
}

What is printed?

  • A

    10 20 30 40

  • B

    40 30 20 10

    check_circle
  • C

    ArrayIndexOutOfBoundsException

  • D

    40 30 20 10

Explanation

Iterating from i = a.length-1 down to 0 prints elements in reverse order.

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

Related questions