Verityprep
Question Bank · Unit 4

AP® Computer Science A Data Collections Multiple Choice Questions

Work through AP Computer Science A multiple-choice 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

Questions 151–160 of 181
  1. 151
  2. 152
  3. 153
  4. 154
  5. 155
  6. 156
  7. 157
  8. 158
  9. 159
  10. 160

Question 151

Multiple choice Using Text Files
Using the Student class and the same file as the previous question. What is printed?
// (list built as in previous question: Alice 80, Bob 90, Carol 70)
int sum = 0;
for (int i = 0; i < list.size(); i++) {
    sum = sum + list.get(i).getGrade();
}
System.out.println(sum / list.size());
Pick an answer, then check it. The solution opens in a window.

Question 152

Multiple choice Using Text Files
Consider a class Student with constructor Student(String name, int grade) and methods getName() returning the name and getGrade() returning the grade. A text file contains:
Alice,85
Bob,70
Carol,92
Dave,60
What 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 count = 0;
for (int i = 0; i < list.size(); i++) {
    if (list.get(i).getGrade() >= 80) {
        count++;
    }
}
System.out.println(count);
Pick an answer, then check it. The solution opens in a window.

Question 153

Multiple choice Using Text Files
Consider a class Student with constructor Student(String name, int grade) and methods getName() returning the name and getGrade() returning the grade. A text file contains:
Alice,75
Bob,92
Carol,88
What 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 maxIdx = 0;
for (int i = 1; i < list.size(); i++) {
    if (list.get(i).getGrade() > list.get(maxIdx).getGrade()) {
        maxIdx = i;
    }
}
System.out.println(list.get(maxIdx).getName());
Pick an answer, then check it. The solution opens in a window.

Question 154

Multiple choice Using Text Files
Consider the following class.
public class Product {
    private String name;
    private double price;
    public Product(String name, double price) { this.name = name; this.price = price; }
    public String getName() { return name; }
    public double getPrice() { return price; }
}
A text file contains:
Milk,2.5
Bread,3.0
Eggs,4.5
What is printed?
ArrayList<Product> list = new ArrayList<Product>();
while (sc.hasNext()) {
    String[] parts = sc.nextLine().split(",");
    list.add(new Product(parts[0], Double.parseDouble(parts[1])));
}
sc.close();
double total = 0;
for (int i = 0; i < list.size(); i++) {
    total = total + list.get(i).getPrice();
}
System.out.println(total);
Pick an answer, then check it. The solution opens in a window.

Question 155

Multiple choice Using Text Files
Using the Product class and the same file as the previous question. How many products have a price less than 4.0?
// (list built as in previous question: Milk 2.5, Bread 3.0, Eggs 4.5)
int count = 0;
for (int i = 0; i < list.size(); i++) {
    if (list.get(i).getPrice() < 4.0) {
        count++;
    }
}
System.out.println(count);
Pick an answer, then check it. The solution opens in a window.

Question 156

Multiple choice Using Text Files
Consider a class Student with constructor Student(String name, int grade) and methods getName() returning the name and getGrade() returning the grade. A text file contains:
Alice,50
Bob,80
Carol,40
Dave,90
What 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();
for (int i = list.size() - 1; i >= 0; i--) {
    if (list.get(i).getGrade() < 60) {
        list.remove(i);
    }
}
System.out.println(list.size());
Pick an answer, then check it. The solution opens in a window.

Question 157

Multiple choice Using Text Files
A text file contains five lines, each with a single integer:
10
20
10
10
30
After reading all values into an ArrayList<Integer>, the following code attempts to remove all occurrences of 10 using forward traversal. What is the size of the list after the loop?
for (int i = 0; i < list.size(); i++) {
    if (list.get(i).intValue() == 10) {
        list.remove(i);
    }
}
System.out.println(list.size());
Pick an answer, then check it. The solution opens in a window.

Question 158

Multiple choice Using Text Files
Consider the following class.
public class Employee {
    private String name;
    private int hours;
    private double rate;
    public Employee(String n, int h, double r) { name=n; hours=h; rate=r; }
    public String getName() { return name; }
    public double getPay() { return hours * rate; }
}
A text file contains:
Alice,40,15.0
Bob,35,20.0
What is printed?
ArrayList<Employee> list = new ArrayList<Employee>();
while (sc.hasNext()) {
    String[] p = sc.nextLine().split(",");
    list.add(new Employee(p[0], Integer.parseInt(p[1]), Double.parseDouble(p[2])));
}
sc.close();
double total = 0;
for (int i = 0; i < list.size(); i++) {
    total = total + list.get(i).getPay();
}
System.out.println(total);
Pick an answer, then check it. The solution opens in a window.

Question 159

Multiple choice Using Text Files
Consider a class Student with constructor Student(String name, int grade) and methods getName() returning the name and getGrade() returning the grade. A text file contains:
Alice,90
Bob,85
Carol,92
What 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();
String names = "";
for (int i = 0; i < list.size(); i++) {
    if (list.get(i).getGrade() > 88) {
        names = names + list.get(i).getName() + " ";
    }
}
System.out.println(names.length());
Pick an answer, then check it. The solution opens in a window.

Question 160

Multiple choice Using Text Files
Consider a class Product with constructor Product(String name, double price) and methods getName() returning the name and getPrice() returning the price. A text file contains:
Apple,1.5
Banana,0.75
Cherry,3.0
Date,2.5
What is printed?
ArrayList<Product> list = new ArrayList<Product>();
while (sc.hasNext()) {
    String[] parts = sc.nextLine().split(",");
    list.add(new Product(parts[0], Double.parseDouble(parts[1])));
}
sc.close();
int count = 0;
for (int i = 0; i < list.size(); i++) {
    double p = list.get(i).getPrice();
    if (p >= 1.0 && p <= 2.5) {
        count++;
    }
}
System.out.println(count);
Pick an answer, then check it. The solution opens in a window.
Question 151 of 181
Page 16 of 19