Array Traversals FRQ Practice

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

Question 1 of 5

Complete the missing while-loop trace cells. Then state the displayed text.

int[] tiles = {9, 4, 6};
int i = 0;
while (i < tiles.length) {
    System.out.print(tiles[i]);
    i++;
}
Body i at body start Printed i after i++ i < tiles.length next?
1 0 9
2
3

Question 2 of 5

Rewrite this indexed for loop as an equivalent enhanced for loop. Then state the displayed text.

int[] ribs = {1, 4, 4};
for (int i = 0; i < ribs.length; i++) {
    System.out.print(ribs[i]);
}

Also write an equivalent while loop that uses an index.

Question 3 of 5

A kiln shelf stores both primitive glaze times and Wick objects.

public class Wick
{
    private int mm;
    public Wick(int mm)
    {
        this.mm = mm;
    }
    public void trim(int n)
    {
        mm -= n;
    }
    public int getMm()
    {
        return mm;
    }
}

int[] times = {20, 30};
for (int t : times) {
    t = t - 5;
}
Wick[] tray = {new Wick(12), new Wick(9)};
for (Wick w : tray) {
    w.trim(2);
}

(a) State times[0] and times[1] after the first loop. (b) State tray[0].getMm() and tray[1].getMm() after the second loop. (c) Explain the difference using copies versus mutators.

Question 4 of 5

A ferry should print every value in int[] wait = {3, 5, 2}; in index order with no extra characters.

(a) Write an indexed for loop that does this. (b) Write a while loop that does this. (c) State the displayed text both loops produce.

Question 5 of 5

Two fragments traverse the same style of array. State the displayed text of each fragment. Then explain whether the enhanced for in fragment II could be rewritten as the indexed for in fragment I without changing the displayed text.

int[] a = {6, 1, 4};
for (int i = 0; i < a.length; i++) {
    System.out.print(a[i] + i);
}

int[] b = {6, 1, 4};
for (int n : b) {
    System.out.print(n);
}