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.

Subtopic: Using Text Files Clear filter

Questions 161–170 of 181
  1. 161
  2. 162
  3. 163
  4. 164
  5. 165
  6. 166
  7. 167
  8. 168
  9. 169
  10. 170

Question 161

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 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 162

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…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 163

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:
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 164

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,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 165

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,95
Bob,60
Carol,82
Dave,45
Eve,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();
ArrayList<Student> honor = new ArrayList<Student>();
for (int i = 0; i < list.size(); i++) {
    if (list.get(i).getGrade() >= 80) {
        honor.add(list.get(i));
    }
}
System.out.println(honor.size());
Pick an answer, then check it. The solution opens in a window.

Question 166

Multiple choice Using Text Files

Consider the following Employee 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,20,12.0
Bob,40,10.0
Carol,30,15.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();
String bestName = list.get(0).getName();
double bestPay = list.get(0).getPay();
for (int i = 1; i < list.size(); i++) {
    if (list.get(i).getPay() > bestPay) {
        bestPay = list.get(i).getPay();
        bestName = list.get(i).getName();
    }
}
System.out.println(bestName + " " + bestPay);
Pick an answer, then check it. The solution opens in a window.

Question 167

Multiple choice Using Text Files
A text file contains:
Widget,5,3.50
Gadget,10,2.25
Each line has a name, quantity, and unit price. What is printed?
double total = 0;
while (sc.hasNext()) {
    String[] parts = sc.nextLine().split(",");
    int qty = Integer.parseInt(parts[1]);
    double price = Double.parseDouble(parts[2]);
    total = total + qty * price;
}
sc.close();
System.out.println(total);
Pick an answer, then check it. The solution opens in a window.

Question 168

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,70
Bob,90
Carol,65
Dave,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();
for (int i = list.size() - 1; i >= 0; i--) {
    if (list.get(i).getGrade() < 75) {
        list.remove(i);
    }
}
System.out.println(list.get(0).getName() + " " + list.get(1).getName());
Pick an answer, then check it. The solution opens in a window.

Question 169

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
Dave,85
Eve,95
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++) {
    if (list.get(i).getGrade() >= 85) {
        sum = sum + list.get(i).getGrade();
    }
}
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 170

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…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 161 of 181
Page 17 of 19