Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Iteration
Start Practice TestPractice Test
About Exam
hard Solved by 1 students

Nested While Loops with an ArrayList

Mark
< Prev
Next >

What is printed as a result of executing the following code segment?

import java.util.ArrayList;
public class NestedLoops {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            list.add(i);
        }
        int sum = 0;
        int i = 0;
        while(i < list.size()) {
            int j = i;
            while(j < list.size() && list.get(j) % 2 != 0) {
                sum += list.get(j);
                j++;
            }
            i = j + 1;
        }
        System.out.println(sum);
    }
}
A

9

B

7

C

15

D

8

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|