Array Traversals MCQ Practice

Question 1 of 10

A clerk prints lock codes from last index to first. What text does this Java code display?

int[] codes = {2, 8, 5};
for (int i = codes.length - 1; i >= 0; i--) {
    System.out.print(codes[i]);
}
  1. A. 285
  2. B. 258
  3. C. 82
  4. D. 582

Question 2 of 10

A bakery totals bag counts with a while loop. What text does this Java code display?

int[] bags = {4, 4, 7};
int i = 0;
int s = 0;
while (i < bags.length) {
    s += bags[i];
    i++;
}
System.out.print(s);
  1. A. 8
  2. B. 15
  3. C. 3
  4. D. 11

Question 3 of 10

A hopscotch board prints tile numbers with an enhanced for loop. What text does this Java code display?

int[] hops = {8, 3, 5};
for (int h : hops) {
    System.out.print(h);
}
  1. A. 835
  2. B. 538
  3. C. 83
  4. D. 012

Question 4 of 10

A jar counter assigns into the enhanced-for copy. What text does this Java code display?

int[] lids = {2, 9, 2};
for (int n : lids) {
    n = n + 10;
}
System.out.print(lids[1]);
  1. A. 19
  2. B. 12
  3. C. 9
  4. D. 2

Question 5 of 10

Lanterns on a shelf can be dimmed. What text does this Java code display?

public class Lantern
{
    private int watts;
    public Lantern(int watts)
    {
        this.watts = watts;
    }
    public void dim(int n)
    {
        watts -= n;
    }
    public int getWatts()
    {
        return watts;
    }
}

Lantern[] shelf = {new Lantern(10), new Lantern(6)};
for (Lantern lamp : shelf) {
    lamp.dim(1);
}
System.out.print(shelf[0].getWatts() + shelf[1].getWatts());
  1. A. 16
  2. B. 14
  3. C. 10
  4. D. 6

Question 6 of 10

A trail map lists loop facts. Which row is entirely correct?

Row Loop Claim
1 for (int n : temps) Assigning n = 0 updates temps
2 for (int i = 0; i < temps.length; i++) Assigning temps[i] = 0 updates temps
3 for (Beacon b : lamps) b.brighter(1) cannot change any Beacon
  1. A. Row 1 only
  2. B. Row 3 only
  3. C. Rows 1 and 2
  4. D. Row 2 only

Question 7 of 10

A zipper factory counts how many times an indexed body runs. What text does this Java code display?

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

Question 8 of 10

A catalog uses for (String s : labels). Which statement is true?

  1. A. s stores the current index, so labels[s] is the element
  2. B. Assigning s = "x" changes the current array slot
  3. C. s is a copy of the current element
  4. D. The loop can assign labels.length a new size

Question 9 of 10

A meter prints only positive readings while traversing by index. What text does this Java code display?

int[] m = {7, 0, 4};
int k = 0;
while (k < m.length) {
    if (m[k] > 0) {
        System.out.print(m[k]);
    }
    k++;
}
  1. A. 704
  2. B. 74
  3. C. 7
  4. D. 047

Question 10 of 10

Markers are used, then the loop variable is retargeted. What text does this Java code display?

public class Marker
{
    private int ink;
    public Marker(int ink)
    {
        this.ink = ink;
    }
    public void use(int n)
    {
        ink -= n;
    }
    public int getInk()
    {
        return ink;
    }
}

Marker[] kit = {new Marker(20), new Marker(15)};
for (Marker mk : kit) {
    mk.use(5);
    mk = new Marker(0);
}
System.out.print(kit[0].getInk() + " " + kit[1].getInk());
  1. A. 15 10
  2. B. 0 0
  3. C. 20 15
  4. D. 15 15