AP CSA 2016 FRQ 2 Systemlog

FRQ Archive2016 FRQs › FRQ 2: SystemLog
2016 AP CSA • ArrayList Manipulation

AP CSA 2016 FRQ 2: SystemLog

Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF

9 Points Medium-Hard ArrayList Manipulation Classic Format (Pre-2019)
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

Download Full 2016 FRQ 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) */ }
}
Timer 22:00

Part A — LogMessage class (4 Points)

Write the LogMessage constructor (parse the log string to extract machine name and description) and getMachineName() accessor.

Drag corner to expand ▽

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;
    }
}
The log format is 'machineName:description'. indexOf(':') finds the separator; substring(0, colonIndex) extracts the machine name; substring(colonIndex + 1) gets the rest.

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.

Drag corner to expand ▽

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);
        }
    }
}
Iterate BACKWARD (from size()-1 down to 0). Removing an element while iterating forward shifts all subsequent indices, causing elements to be skipped. Backward traversal avoids this entirely.

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).

Drag corner to expand ▽

Scoring Rubric

+1 Collects messages where getMachineName() equals 'hacker'
+1 Returns them in reverse order (most recent first)

Solution

public ArrayList getLastHackerMessages()
{
    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;
}
Iterate backward through messageList and add matching messages in that order — this naturally produces reverse-chronological output without needing to reverse the result list afterward.

Common Mistakes to Avoid

Forward iteration while removing in removeEarlierThan

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

The backward-traversal-for-removal pattern is the key skill in this FRQ and appears frequently on all years.
For getMachineName(), the comparison must be 'hacker'.equals(...) or .equals('hacker') — exact case-sensitive match.

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]