Skip to main content
All subjects

Intro to Computer Science (Java) FRQ practice

6 problems · Worked solutions · 9-point rubrics · Java

Code Scholars practice problems. These are not official exam prompts.

Problem 1 · First Programs and Values · 9 points

Convert elapsed seconds

A nonnegative int total stores elapsed seconds. Implement describe(int total) returning hours:minutes:seconds as unpadded numbers.

  1. A. Implement the conversion with division and remainder.
  2. B. Trace total = 3665.
  3. C. Explain why integer division is appropriate and test zero and an exact hour.
Show worked solution and rubric
public static String describe(int total) {
    int h = total / 3600;
    int m = (total % 3600) / 60;
    int s = total % 60;
    return h + ":" + m + ":" + s;
}
  1. A. Divide by 3600 for full hours; divide the remainder by 60 for minutes. The final remainder is seconds.
  2. B. 3665 gives 1:1:5 because 3665 = 3600 + 60 + 5.
  3. C. Integer division removes partial units. Zero gives 0:0:0; 3600 gives 1:0:0.

Scoring rubric

  • 3 points: Divide by 3600 for full hours; divide the remainder by 60 for minutes
  • 3 points: 3665 gives 1:1:5 because 3665 = 3600 + 60 + 5.
  • 3 points: Integer division removes partial units
Review this unit

Problem 2 · Making Decisions · 9 points

Classify a score

Implement grade(int score): return Invalid outside 0..100, A for 90..100, B for 80..89, and Other otherwise.

  1. A. Write the classifier with mutually exclusive branches.
  2. B. Give results for -1, 79, 80, 89, 90, 100, and 101.
  3. C. Explain why checking score >= 80 before score >= 90 would be wrong in an else-if chain.
Show worked solution and rubric
public static String grade(int score) {
    if (score < 0 || score > 100) return "Invalid";
    if (score >= 90) return "A";
    if (score >= 80) return "B";
    return "Other";
}
  1. A. Reject invalid inputs first, then test descending cutoffs so a wider range does not hide the A branch.
  2. B. The results are Invalid, Other, B, B, A, A, Invalid. Each cutoff must be tested on both sides.
  3. C. A score of 95 would satisfy >=80 first and incorrectly return B. Ordered branches stop after their first match.

Scoring rubric

  • 3 points: Reject invalid inputs first, then test descending cutoffs so a wider range does not hide the A branch.
  • 3 points: The results are Invalid, Other, B, B, A, A, Invalid
  • 3 points: A score of 95 would satisfy >=80 first and incorrectly return B
Review this unit

Problem 3 · Repetition · 9 points

Sum multiples without overshooting

Implement sumMultiples(int n), the sum of positive multiples of 3 not exceeding n. Assume 0 <= n <= 10000.

  1. A. Implement the method with a loop.
  2. B. Trace n = 10 and n = 2.
  3. C. State a loop invariant and give time and auxiliary-space complexity.
Show worked solution and rubric
public static int sumMultiples(int n) {
    int sum = 0;
    for (int i = 3; i <= n; i += 3) sum += i;
    return sum;
}
  1. A. Start at 3 and advance by 3 while the value is <=n. The bound includes an exact multiple.
  2. B. For 10, add 3,6,9 to obtain 18; for 2, no iterations occur and the result is 0.
  3. C. Before each iteration sum contains all positive multiples of 3 smaller than the current i. Time is O(n), auxiliary space O(1).

Scoring rubric

  • 3 points: Start at 3 and advance by 3 while the value is <=n
  • 3 points: For 10, add 3,6,9 to obtain 18; for 2, no iterations occur and the result is 0.
  • 3 points: Before each iteration sum contains all positive multiples of 3 smaller than the current i
Review this unit

Problem 4 · Working With Text · 9 points

Collapse adjacent repeated characters

Implement collapse(String s) for a non-null string of basic Latin characters. Keep only the first character of each consecutive run; do not remove later nonadjacent occurrences.

  1. A. Implement collapse using StringBuilder.
  2. B. Trace "aaabbcca" and the empty string.
  3. C. Explain the boundary check and analyze time and auxiliary space.
Show worked solution and rubric
public static String collapse(String s) {
    StringBuilder out = new StringBuilder();
    for (int i = 0; i < s.length(); i++)
        if (i == 0 || s.charAt(i) != s.charAt(i - 1)) out.append(s.charAt(i));
    return out.toString();
}
  1. A. Append a character only when its index is zero or it differs from its predecessor. This compares original neighbors.
  2. B. The outputs are abca and the empty string. The last a is kept because it starts a new run.
  3. C. The i==0 short-circuit protects charAt(i-1). For n characters, time and result-builder storage are O(n); the input is unchanged.

Scoring rubric

  • 3 points: Append a character only when its index is zero or it differs from its predecessor
  • 3 points: The outputs are abca and the empty string
  • 3 points: The i==0 short-circuit protects charAt(i-1)
Review this unit

Problem 5 · Collections of Values · 9 points

Copy passing scores

Implement passing(int[] scores) returning a new ArrayList<Integer> containing scores >=60 in original order. Input is non-null.

  1. A. Implement without changing the array.
  2. B. Trace [59,60,100,60] and explain duplicates.
  3. C. Give results for empty input and analyze complexity.
Show worked solution and rubric
public static ArrayList<Integer> passing(int[] scores) {
    ArrayList<Integer> result = new ArrayList<>();
    for (int score : scores) if (score >= 60) result.add(score);
    return result;
}
  1. A. Traverse the array and append each value that satisfies the threshold. A new list prevents the result from changing the input.
  2. B. The result is [60,100,60]. Both 60 values remain because they are separate qualifying observations.
  3. C. Empty input returns an empty list. Time is O(n); the returned list requires O(k) space for k passing scores.

Scoring rubric

  • 3 points: Traverse the array and append each value that satisfies the threshold
  • 3 points: The result is [60,100,60]
  • 3 points: Empty input returns an empty list
Review this unit

Problem 6 · Methods and Classes · 9 points

Maintain a bounded counter

Design Counter with positive capacity. It starts at zero. add() increments and returns true only when below capacity; otherwise it returns false. Expose getCount(), keeping fields private.

  1. A. Implement constructor, fields, add, and getter.
  2. B. Trace three calls to add for capacity 2.
  3. C. Explain the invariant and how clients are prevented from bypassing it.
Show worked solution and rubric
public static class Counter {
    private final int capacity;
    private int count;
    public Counter(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException("capacity");
        this.capacity = capacity;
    }
    public boolean add() {
        if (count == capacity) return false;
        count++;
        return true;
    }
    public int getCount() { return count; }
}
  1. A. Validate capacity in the constructor, then protect both fields. The add method is the only operation that changes count.
  2. B. The returns are true,true,false and count ends at 2. A rejected call leaves count unchanged.
  3. C. The invariant is 0<=count<=capacity. Private fields and validated methods prevent direct assignment; nonpositive capacity is rejected.

Scoring rubric

  • 3 points: Validate capacity in the constructor, then protect both fields
  • 3 points: The returns are true,true,false and count ends at 2
  • 3 points: The invariant is 0<=count<=capacity
Review this unit

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic