Verityprep
Question Bank

AP® Computer Science A Multiple Choice Questions

Work through AP Computer Science A multiple-choice questions unit by unit, 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.

Questions 2,711–2,720 of 2,811
  1. 2711
  2. 2712
  3. 2713
  4. 2714
  5. 2715
  6. 2716
  7. 2717
  8. 2718
  9. 2719
  10. 2720

Question 2711

Multiple choice Unit 4: Data Collections
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 2712

Multiple choice Unit 4: Data Collections
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 2713

Multiple choice Unit 4: Data Collections
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 2714

Multiple choice Unit 4: Data Collections
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 2715

Multiple choice Unit 4: Data Collections
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 2716

Multiple choice Unit 4: Data Collections
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 2717

Multiple choice Unit 4: Data Collections
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 a header row followed by data:
Name,Grade
Alice,95
Bob,87
Carol,91
What is printed?
sc.nextLine(); // skip header
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.size() + " " + list.get(0).getName());
Pick an answer, then check it. The solution opens in a window.

Question 2718

Multiple choice Unit 4: Data Collections
Consider a class Product with constructor Product(String name, double price) and methods getName() returning the name and getPrice() returning the price. A text…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2719

Multiple choice Unit 4: Data Collections
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:
Pen,1.0
Book,12.0
Eraser,0.5
Notebook,8.0
Tape,0.75
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();
for (int i = list.size() - 1; i >= 0; i--) {
    if (list.get(i).getPrice() < 1.0) {
        list.remove(i);
    }
}
System.out.println(list.size());
Pick an answer, then check it. The solution opens in a window.

Question 2720

Multiple choice Unit 4: Data Collections
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,89
Bob,92
Carol,85
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 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 2,711 of 2,811
Page 272 of 282