Skip to main content

Question 1: Methods and Control Structures

Digit-sum puzzle and mirror strings

  • Units 1 and 2
  • 7 points
  • About 25 minutes

You write two methods, or a constructor and a method, of a class you're given. Part A needs loops and/or if statements plus calls to the class's own methods; Part B needs String methods. New since the course was revised for 2025–26 (first tested in May 2026): it's worth 7 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 1 of 4, 7 points (Part A 4 points, Part B 3 points). Section II has 4 free-response questions in 90 minutes (45% of the score), all assessing Practice 2 (Develop Code). The exam is fully digital in Bluebook: you type your Java, and the Java Quick Reference is provided. No calculator. Suggested time is our split of the 90 minutes by points; College Board gives no per-question timing.

The question and its sources

A number puzzle asks players to find numbers whose digits add up to a target value. For example, the digits of 2,041 add up to 2 + 0 + 4 + 1 = 7. You will write two methods of the DigitPuzzle class.

The DigitPuzzle class

Java
public class DigitPuzzle
{
    /** The sum that the digits of a winning number must add up to */
    private int target;
 
    /**
     * Returns true if n may be used in the puzzle; false otherwise.
     * Precondition: n >= 0
     */
    public boolean isAllowed(int n)
    { /* implementation not shown */ }
 
    /** Described in part (a) */
    public int nextMatch(int start)
    { /* to be implemented in part (a) */ }
 
    /** Described in part (b) */
    public static boolean isMirror(String str)
    { /* to be implemented in part (b) */ }
 
    // There may be instance variables, constructors, and methods that are not shown.
}

Example for part (a): a puzzle with target 5, where isAllowed returns false for every multiple of 10 and true otherwise

CallValue returnedWhy
nextMatch(13)141 + 4 = 5, and 14 is allowed.
nextMatch(14)2315 through 22 don't have a digit sum of 5; 2 + 3 = 5.
nextMatch(41)10450 has a digit sum of 5 but isn't allowed; no number from 51 to 103 has a digit sum of 5.
nextMatch(0)55 is the first number after 0 whose digits add up to 5.

Source: Hypothetical example

Suggested time: 25 minutes

Your answers are saved in this browser as you type.

Something wrong with this question?

What's wrong?

Please don't include personal details.

Part (a)

4 points

Write the method nextMatch, which returns the smallest integer greater than start whose digits add up to target and for which isAllowed returns true. Assume that start >= 0, that target > 0, and that such an integer exists. You must use isAllowed appropriately to receive full credit. Complete method nextMatch.public int nextMatch(int start)

Tab indents. Press Esc, then Tab, to move on.

0 lines · 0 / 2,500 characters

Part (b)

3 points

A String is a *mirror* if it reads the same from left to right as from right to left. For example, isMirror("12321") and isMirror("4554") return true, isMirror("4564") returns false and isMirror("7") returns true. Write the static method isMirror, which returns true if str is a mirror and false otherwise. Assume str.length() >= 1. Complete method isMirror.public static boolean isMirror(String str)

Tab indents. Press Esc, then Tab, to move on.

0 lines · 0 / 2,500 characters

Checking scoring…

Scoring it yourself shows you the rubric, examples and a model answer. Try writing your answer first.