Question: Problem 1: Understanding code that uses an array individual-only See the section above for guidelines that you should follow to create the file containing your

Problem 1: Understanding code that uses an array

individual-only

See the section above for guidelines that you should follow to create the file containing your answers to Part I.

  1. Consider the following static method, which operates on an array of integers:

    public static void mystery(int[] arr) { for (int i = 0; i < arr.length - 2; i++) { int val1 = arr[i + 1]; int val2 = arr[i + 2]; arr[i] = val1 + val2; // What values do the variables have here, // for each value of the loop variable `i`? } } 

    Consider the following code fragment, which calls this method:

    int[] values = {1, 3, 5, 7, 9, 11, 13}; mystery(values); 

    In section 1-1 of ps2_partI, weve provided a table that you should complete to illustrate the execution of the above method call.

    We have started the table for you. The first row shows what the array looks like before the start of the for loop. For each value of the loop variable i, include a row in the table that shows the values of the variables at the end of the iteration of the loop for that value of i. You may not need all of the rows of the table.

    (Note: You should write the value of arr as an array literal that shows the current contents of the array, even though the variable itself actually holds a reference to the array.)

  2. After running the code fragment from part 1, we then execute the following statement:

    System.out.println(Arrays.toString(values)); 

    Do we see the original array, or do we see the changes made by the call to the mystery()method? Explain briefly why your answer makes sense.

    (Note: If you want to check your answer, youll need to import the java.util package at the top of your test class so that you can use the Arrays.toString() method.)

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!