for Loops

AP Computer Science A· difficulty 3/5

int[] arr = {3, 7, 2, 8, 5};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
  if (arr[i] > max) {
    max = arr[i];
  }
}
System.out.println(max);

What is printed?

  • A

    8

    check_circle
  • B

    7

  • C

    3

  • D

    5

Explanation

The loop tracks the largest value seen. It updates max to 7 (vs 3), then keeps 7 (vs 2), then to 8 (vs 7), then keeps 8 (vs 5). Final max is 8.

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

Related questions