Question: Consider the following methods which appear within the same class. public static void mystery(int[] data) { for (int k = 0; k < data.length -

Consider the following methods which appear within the same class.

public static void mystery(int[] data) { for (int k = 0; k < data.length - 1; k += 2) { int t = data[k]; data[k] = data[k + 1]; data[k + 1] = t; } } public static String toString(int[] data) { String str = ""; for (int d : data) { str = str + d + " "; } return str; }

The following code segment appears in the main method of the same class.

int[] nums = {1, 2, 7, 3, 5}; mystery(nums); System.out.println(toString(nums));

What is printed as a result of executing the code segment?

2 1 3 7 5

1 2 7 3 5

1 7 2 5 3

Nothing is printed because an ArrayIndexOutOfBoundsException is thrown during the execution of the method mystery.

2 7 3 5 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!