Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Data Collections
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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

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