Question 171
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);
Main Solution
Let's trace through the code step by step.
- Reading Alice's data: The line
Alice,10,25.0is split intop[0]="Alice",p[1]="10",p[2]="25.0". A newEmployee("Alice", 10, 25.0)is created and added aslist.get(0). - Reading Bob's data: The line
Bob,8,30.0is split intop[0]="Bob",p[1]="8",p[2]="30.0". A newEmployee("Bob", 8, 30.0)is created and added aslist.get(1). - Computing pay:
getPay()returnshours * rate.- Alice: \(10 \times 25.0 = 250.0\)
- Bob: \(8 \times 30.0 = 240.0\)
- Computing diff:
diff = list.get(1).getPay() - list.get(0).getPay()= Bob's pay − Alice's pay = \(240.0 - 250.0 = -10.0\)
The program prints -10.0, which is answer choice C.
Why the other options are wrong:
- A (250.0): This is Alice's pay alone, not a difference.
- B (240.0): This is Bob's pay alone, not a difference.
- D (10.0): This would be the result if the subtraction were reversed: Alice's pay minus Bob's pay. The code subtracts index 0 (Alice) from index 1 (Bob), not the other way around.
Teacher Explanation
This question tests two key concepts in AP Computer Science A:
- ArrayList indexing: Objects are stored in the order they are added. The first line read from the file (
Alice,10,25.0) goes into index 0, and the second line (Bob,8,30.0) goes into index 1. Students must correctly map file order to ArrayList indices. - Order of operands in subtraction: The expression
list.get(1).getPay() - list.get(0).getPay()computes Bob's pay minus Alice's pay. Since Bob earns less (\(240.0\)) than Alice (\(250.0\)), the result is negative. Subtraction is not commutative, so the order ofget(1)andget(0)is critical.
The getPay() method simply multiplies hours by rate, so the arithmetic is straightforward once you correctly identify which employee is at which index.
Education Coach Advice
For questions involving file reading into ArrayLists and object method calls, follow this strategy:
- Build a trace table: Write down each index and the object stored there as you simulate the loop. For example: index 0 → Alice (hours=10, rate=25.0), index 1 → Bob (hours=8, rate=30.0).
- Compute method return values explicitly: Don't try to do it all in your head. Write out each
getPay()result before evaluating the final expression. - Pay close attention to operand order in subtraction: Circle or underline which index comes first in the subtraction. The sign of the result depends entirely on this order.
- Practice file-parsing patterns: The pattern of reading a line, splitting by a delimiter, and parsing each token is very common on the AP exam. Get comfortable with
Integer.parseInt()andDouble.parseDouble().
Traps
Students commonly fall into these traps on this question:
- Reversing the subtraction order (choosing D = 10.0): This is the most common mistake. Students may assume the subtraction is Alice minus Bob (index 0 minus index 1), but the code clearly states
list.get(1) - list.get(0), which is Bob minus Alice, yielding a negative result. - Choosing Alice's pay (A = 250.0) or Bob's pay (B = 240.0): Students who don't carefully read the subtraction expression might just compute one employee's pay and stop there.
- Mixing up which employee is at which index: Since Alice appears first in the file, she is at index 0. Bob is at index 1. Confusing this mapping leads to incorrect pay calculations.
How to avoid these traps: Always trace through the code in order — identify what goes into each index first, compute each pay value separately, then carefully apply the subtraction in the exact order written in the code.
Answer images

