AP® Computer Science A Data Collections Practice Questions
Work through AP Computer Science A practice questions on Data Collections, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Subtopic: Using Text Files Clear filter
Multiple choice
Using Text Files
Question 142
Multiple choice
Using Text Files
Question 143
Multiple choice
Using Text Files
Question 144
Multiple choice
Using Text Files
Question 145
Multiple choice
Using Text Files
Question 146
Multiple choice
Using Text Files
Question 147
Multiple choice
Using Text Files
Question 148
Multiple choice
Using Text Files
Question 149
Multiple choice
Using Text Files
Consider the following Student class.
public class Student {
private String name;
private int grade;
public Student(String name, int grade) { this.name = name; this.grade = grade; }
public String getName() { return name; }
public int getGrade() { return grade; }
} A text file contains:Alice,90Bob,85What is printed?
ArrayList<Student> list = new ArrayList<Student>();
while (sc.hasNext()) {
String[] parts = sc.nextLine().split(",");
list.add(new Student(parts[0], Integer.parseInt(parts[1])));
}
sc.close();
System.out.println(list.get(1).getName());
Pick an answer, then check it. The solution opens in a window.
BCorrect answer
Answer images
Question 150
Multiple choice
Using Text Files
Consider a class
What is printed?
Student with constructor Student(String name, int grade) and methods getName() returning the name and getGrade() returning the grade. A text file contains:Alice,80Bob,90Carol,70What is printed?
ArrayList<Student> list = new ArrayList<Student>();
while (sc.hasNext()) {
String[] parts = sc.nextLine().split(",");
list.add(new Student(parts[0], Integer.parseInt(parts[1])));
}
sc.close();
int sum = 0;
for (int i = 0; i < list.size(); i++) {
sum = sum + list.get(i).getGrade();
}
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.
CCorrect answer
Answer images
Question 141 of 181
Page 15 of 19
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

