Question 2701
Multiple choice
Unit 4: Data Collections
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.
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,85ArrayList<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());
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,70ArrayList<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);
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());
Student with constructor Student(String name, int grade) and methods getName() returning the name and getGrade() returning the grade. A text file contains:Alice,85Bob,70Carol,92Dave,60ArrayList<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);
Student with constructor Student(String name, int grade) and methods getName() returning the name and getGrade() returning the grade. A text file contains:Alice,75Bob,92Carol,88ArrayList<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());
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.5Bread,3.0Eggs,4.5ArrayList<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);
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.