Verityprep
Question Bank · Unit 4

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.

Questions 1,071–1,080 of 1,222
  1. 1071
  2. 1072
  3. 1073
  4. 1074
  5. 1075
  6. 1076
  7. 1077
  8. 1078
  9. 1079
  10. 1080

Question 1071

Multiple choice Using Text Files
A text file contains three lines: a,b,c,d e,f,g h,i What is printed? int total = 0; while (sc.hasNext()) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1072

Multiple choice Using Text Files
Consider the following class. public class Student { private String name; private int grade; public Student(String name, int grade) { this.name = name;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1073

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,90
Bob,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();
System.out.println(list.get(1).getName());
Pick an answer, then check it. The solution opens in a window.

Question 1074

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,80
Bob,90
Carol,70
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);
Pick an answer, then check it. The solution opens in a window.

Question 1075

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 1076

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 1077

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 1078

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 1079

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 1080

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 1,071 of 1,222
Page 108 of 123