Array Traversals FRQ Practice

Responses receive AI feedback against a rubric after submission. Scores are practice estimates.

Question 1 of 5

Complete the missing trace cells for this indexed loop. Then state the final value of total.

int[] packs = {4, 1, 6};
int total = 0;
for (int i = 0; i < packs.length; i++) {
    total += packs[i];
}
Body i packs[i] total after body
1 0 4
2
3

Question 2 of 5

Rewrite this enhanced for loop as an equivalent indexed for loop that uses the same array name. Keep the same print.

int[] stamps = {5, 8, 2};
for (int n : stamps) {
    System.out.print(n);
}

Then state the displayed text and the values taken by the index variable.

Question 3 of 5

A loading dock uses Crate objects.

public class Crate
{
    private int count;
    public Crate(int count)
    {
        this.count = count;
    }
    public void add(int n)
    {
        count += n;
    }
    public int getCount()
    {
        return count;
    }
}

Crate[] bay = {new Crate(4), new Crate(7)};
for (Crate c : bay) {
    c.add(3);
    c = new Crate(0);
}

(a) Explain whether c.add(3) changes the Crate objects stored in bay. (b) Explain whether c = new Crate(0) replaces the array elements. (c) State bay[0].getCount() and bay[1].getCount() after the loop.

Question 4 of 5

Rewrite this indexed for loop as an equivalent while loop. Keep the same array name and the same body work.

int[] coils = {3, 3, 8};
int shown = 0;
for (int i = 0; i < coils.length; i++) {
    shown += coils[i];
}

Then state the final shown and explain where i++ belongs relative to using coils[i].

Question 5 of 5

Compare these two fragments. For each, state the values in nums after the loop. Then explain the difference using the idea that an enhanced-for variable is a copy.

int[] nums = {2, 2, 2};
for (int n : nums) {
    n = n + 5;
}

int[] also = {2, 2, 2};
for (int i = 0; i < also.length; i++) {
    also[i] = also[i] + 5;
}