0%
0 / 10 answered

ArrayList Traversals Practice Test

10 Questions
Question
1 / 10
Q1

What is the purpose of the loop in the provided code?


// Part A: Setup

ArrayList<Double> prices = new ArrayList<>();

prices.add(2.5);

prices.add(null);

prices.add(1.0);

// Part B: Sum values safely

double totalSum = 0.0;

for (Double price : prices) { // traverse list

    if (price != null) {

        totalSum += price; // add non-null values

    }

}

// Part C: Output

System.out.println(totalSum);

Question Navigator