Developing Algorithms Using Arrays

AP Computer Science A· difficulty 4/5

String[] names = {"Ann", "Bob", "Cy"};
int[] scores = {85, 92, 77};
for (int i = 0; i < names.length; i++) {
  System.out.println(names[i] + ":" + scores[names.length - 1 - i]);
}

What is printed first?

  • A

    Ann:85

  • B

    Cy:85

  • C

    Bob:92

  • D

    Ann:77

    check_circle

Explanation

The first iteration uses names[0]="Ann" but scores[2]=77 — an example of parallel-array misalignment when indices don't match.

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

Related questions