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
medium

ArrayList Iteration and Conditionals

Mark
< Prev
Next >

Consider the following SurveyAnalyzer class and a code segment that uses it. What is printed as a result of executing the code segment?

public class SurveyAnalyzer {
    private ArrayList<Integer> ages;
    private ArrayList<String> responses;
    private int validResponses;
    
    public SurveyAnalyzer(ArrayList<Integer> ageData, ArrayList<String> responseData) {
        ages = ageData;
        responses = responseData;
        validResponses = 0;
    }
    
    public void validateAndCount() {
        for (int i = 0; i < Math.min(ages.size(), responses.size()); i++) {
            int age = ages.get(i);
            String response = responses.get(i);
            
            if (age >= 18 && age <= 100 && !response.equals("")) {
                validResponses++;
                System.out.println("Valid: Age " + age + ", Response: " + response);
            } else {
                System.out.println("Invalid data at index " + i);
            }
        }
        System.out.println("Total valid responses: " + validResponses);
    }
}
ArrayList<Integer> surveyAges = new ArrayList<>();
surveyAges.add(25);
surveyAges.add(150);
surveyAges.add(19);
surveyAges.add(16);

ArrayList<String> surveyResponses = new ArrayList<>();
surveyResponses.add("Satisfied");
surveyResponses.add("Very satisfied");
surveyResponses.add("");

SurveyAnalyzer analyzer = new SurveyAnalyzer(surveyAges, surveyResponses);
analyzer.validateAndCount();
A

Valid: Age 25, Response: Satisfied
Invalid data at index 1
Invalid data at index 2
Total valid responses: 1

B

Valid: Age 25, Response: Satisfied
Invalid data at index 1
Valid: Age 19, Response:
Total valid responses: 2

C

Valid: Age 25, Response: Satisfied
Invalid data at index 1
Invalid data at index 2
Invalid data at index 3
Total valid responses: 1

D

Valid: Age 25, Response: Satisfied
Valid: Age 150, Response: Very satisfied
Invalid data at index 2
Total valid responses: 2

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|