Skip to main content

Java Topic Practice

Sorting and Efficiency

Sorting and Efficiency MCQ Practice

Question 1 of 15

Answered 0 of 15

Choose one answer.

What is printed by the following code segment?

int[] data = {4, 9, 2, 7, 5, 1};
int swaps = 0;
for (int i = 0; i < data.length - 1; i++) {
  for (int j = 0; j < data.length - 1 - i; j++) {
    if (data[j] > data[j + 1]) {
      int t = data[j];
      data[j] = data[j + 1];
      data[j + 1] = t;
      swaps++;
    }
  }
}
System.out.print(swaps);
Choose one answer.