AP CSA 2016 FRQ 2 Systemlog
AP CSA 2016 FRQ 2: SystemLog
Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF
| Question Type | ArrayList Manipulation |
| Key Skills | Implement the LogMessage class (parse and store a log string's machine name and description) and two SystemLog methods: |
| Difficulty | Medium-Hard |
| AP CSA Units | Unit 4: Data Collections (ArrayList traversal, backward removal, building result list) and Unit 3: Class Creation (LogMessage class). |
What This Problem Asks
Implement the LogMessage class (parse and store a log string's machine name and description) and two SystemLog methods: removeEarlierThan() (remove all messages with timestamps before a given time using backward traversal) and getLastHackerMessages() (return all 'hacker' level messages in reverse order).
Official Question & PDF
PDF cannot display here. Open PDF directly →
Provided Code
public class LogMessage
{
public LogMessage(String str) { /* to be implemented in part (a) */ }
public String getMachineName() { /* to be implemented in part (a) */ }
public boolean isAfter(int time) {
/* provided */
}
public String toString() {
/* provided */
}
}
public class SystemLog
{
private ArrayList messageList;
// constructor and other methods provided
public void removeEarlierThan(int time) { /* to be implemented in part (b) */ }
public ArrayList getLastHackerMessages() { /* to be implemented in part (c) */ }
}
Part A — LogMessage class (4 Points)
Write the LogMessage constructor (parse the log string to extract machine name and description) and getMachineName() accessor.
Scoring Rubric
| +1 | Identifies machine name as text before first ':' |
| +1 | Stores machine name in an instance variable |
| +1 | Stores the rest of the string (description) in an instance variable |
| +1 | getMachineName() returns the stored machine name |
Solution
public class LogMessage
{
private String machineName;
private String description;
public LogMessage(String str)
{
int colonIndex = str.indexOf(":");
machineName = str.substring(0, colonIndex);
description = str.substring(colonIndex + 1);
}
public String getMachineName()
{
return machineName;
}
}
Part B — removeEarlierThan (3 Points)
Write removeEarlierThan(). Remove all LogMessage objects from messageList whose timestamp is earlier than (i.e., not after) the given time.
Scoring Rubric
| +1 | Iterates over messageList |
| +1 | Removes messages where isAfter(time) is false |
| +1 | Correctly handles removal without skipping elements (backward loop or index adjustment) |
Solution
public void removeEarlierThan(int time)
{
for (int i = messageList.size() - 1; i >= 0; i--)
{
if (!messageList.get(i).isAfter(time))
{
messageList.remove(i);
}
}
}
Part C — getLastHackerMessages (2 Points)
Write getLastHackerMessages(). Return a new ArrayList containing all messages from machine 'hacker' in the reverse order they appear in messageList (most recent first).
Scoring Rubric
| +1 | Collects messages where getMachineName() equals 'hacker' |
| +1 | Returns them in reverse order (most recent first) |
Solution
public ArrayListgetLastHackerMessages() { ArrayList result = new ArrayList (); for (int i = messageList.size() - 1; i >= 0; i--) { if (messageList.get(i).getMachineName().equals("hacker")) { result.add(messageList.get(i)); } } return result; }
Common Mistakes to Avoid
Wrong
for (int i = 0; i < messageList.size(); i++) { if (...) messageList.remove(i); }
Correct
for (int i = messageList.size()-1; i >= 0; i--) { if (...) messageList.remove(i); }
Removing element i in a forward loop shifts element i+1 into position i, which is then skipped. Backward traversal avoids this: removing index i does not affect any index < i.
Exam Tips
Frequently Asked Questions
What does 2016 AP CSA FRQ 2 SystemLog ask?
It asks students to write the LogMessage constructor and getMachineName() accessor, then two SystemLog methods: removeEarlierThan() (removes old messages using backward traversal) and getLastHackerMessages() (returns all hacker messages in reverse order).
Why must removeEarlierThan use backward traversal?
When you remove an element from an ArrayList during a forward loop, all subsequent elements shift left by one index. The loop counter then skips over the element that shifted into the removed position. Iterating backward avoids this because removing index i only affects indices greater than i, which have already been processed.
See All 2016 AP CSA FRQs
View the complete 2016 exam hub with solutions, difficulty analysis, and scoring for all four questions.
View 2016 FRQ Hub →Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]