Array Traversals MCQ Practice

Question 1 of 10

An orchard totals pear counts with an indexed for loop. What text does this Java code display?

int[] pears = {2, 5, 3};
int total = 0;
for (int i = 0; i < pears.length; i++) {
    total += pears[i];
}
System.out.print(total);
  1. A. 7
  2. B. 15
  3. C. 10
  4. D. 8

Question 2 of 10

A booth prints pin codes in index order. What text does this Java code display?

int[] pins = {4, 8, 1};
for (int i = 0; i < pins.length; i++) {
    System.out.print(pins[i]);
}
  1. A. 481
  2. B. 184
  3. C. 4813
  4. D. 48

Question 3 of 10

A harbor prints buoy IDs with a while loop. What text does this Java code display?

int[] buoys = {6, 1, 9};
int i = 0;
while (i < buoys.length) {
    System.out.print(buoys[i]);
    i++;
}
  1. A. 6190
  2. B. 19
  3. C. 61
  4. D. 619

Question 4 of 10

A pantry lists tin sizes with an enhanced for loop. What text does this Java code display?

int[] tins = {3, 7, 2};
for (int n : tins) {
    System.out.print(n);
}
  1. A. 3723
  2. B. 372
  3. C. 273
  4. D. 37

Question 5 of 10

A clerk tries to clear scores by assigning to the enhanced-for variable. What text does this Java code display?

int[] scores = {5, 4, 1};
for (int s : scores) {
    s = 0;
}
System.out.print(scores[0] + scores[1] + scores[2]);
  1. A. 10
  2. B. 0
  3. C. 9
  4. D. 5

Question 6 of 10

Harbor beacons can be brightened. Beacon is shown, then a row is traversed. What text does this Java code display?

public class Beacon
{
    private int glow;
    public Beacon(int glow)
    {
        this.glow = glow;
    }
    public void brighter(int n)
    {
        glow += n;
    }
    public int getGlow()
    {
        return glow;
    }
}

Beacon[] row = {new Beacon(3), new Beacon(5)};
for (Beacon b : row) {
    b.brighter(2);
}
System.out.print(row[0].getGlow() + " " + row[1].getGlow());
  1. A. 3 5
  2. B. 2 2
  3. C. 5 7
  4. D. 3 7

Question 7 of 10

A stencil printer uses this enhanced for loop.

int[] data = {9, 2, 6};
for (int v : data) {
    System.out.print(v);
}

Which indexed for loop is equivalent?

  1. A. for (int i = 0; i <= data.length; i++) { System.out.print(data[i]); }
  2. B. for (int i = 0; i < data.length; i++) { System.out.print(data[i]); }
  3. C. for (int i = 1; i < data.length; i++) { System.out.print(data[i]); }
  4. D. for (int i = 0; i < data.length; i++) { System.out.print(i); }

Question 8 of 10

A jeweler totals rings after skipping the first tray. What text does this Java code display?

int[] rings = {9, 2, 6, 4};
int sum = 0;
for (int i = 1; i < rings.length; i++) {
    sum += rings[i];
}
System.out.print(sum);
  1. A. 21
  2. B. 17
  3. C. 9
  4. D. 12

Question 9 of 10

A lumber yard tries to relabel woods by assigning to the enhanced-for variable. What text does this Java code display?

String[] woods = {"oak", "elm"};
for (String w : woods) {
    w = "fig";
}
System.out.print(woods[0]);
  1. A. oak
  2. B. fig
  3. C. elm
  4. D. null

Question 10 of 10

A lab doubles each voltage using an indexed loop. What text does this Java code display?

int[] volts = {1, 2, 3};
for (int i = 0; i < volts.length; i++) {
    volts[i] = volts[i] * 2;
}
System.out.print(volts[2]);
  1. A. 3
  2. B. 4
  3. C. 6
  4. D. 2