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.

Questions 991–1,000 of 1,079
  1. 991
  2. 992
  3. 993
  4. 994
  5. 995
  6. 996
  7. 997
  8. 998
  9. 999
  10. 1000

Question 991

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 992

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 993

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 994

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 995

Multiple choice Using Text Files
Using the Employee class defined above.
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,10,25.0
Bob,8,30.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 diff = list.get(1).getPay() - list.get(0).getPay();
System.out.println(diff);
Pick an answer, then check it. The solution opens in a window.

Question 996

Multiple choice Using Text Files
Consider the following class. public class Record { private String subject; private String name; private int score; public Record(String sub, String n, int s) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 997

Multiple choice Arrays
Consider the following code: public static int countDrops(int[] temperatures) { int drops = 0; for (int i = 1;i < temperatures.length;i++) { if (temperatures[i]…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 998

Multiple choice Arrays
Consider the following code: public static void rearrange(int[] arr) { int last = arr[arr.length - 1]; for (int i = arr.length - 1;i > 0;i--) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 999

Multiple choice Arrays
Consider the following code: public static void doubleAll(int[] numbers) { for (int val : numbers) { val = val * 2; } }…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1000

Multiple choice Arraylists
public static ArrayList<Integer> buildList(int[] arr) { ArrayList<Integer> result = new ArrayList<Integer>(); for (int val : arr) { if (val % 2 == 0)…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 991 of 1,079
Page 100 of 108