Skip to main content
Logo image

Section 2.25 FRQ 1 MessageBuilder with Loops and Strings

The AP exam’s first free response question (FRQ) is on Methods and Control Structures using expressions, Strings, loops, and if statements. Students will write two methods or one constructor and one method of a given class based on provided specifications and examples. In Part A (4 points),the method or constructor will require students to write iterative or conditional statements, or both, as well as statements that call methods in the specified class. In Part B (3 points), the method or constructor will require calling String methods as of 2026. The AP exam provides the method header with some parameter variables and other methods that you will need to call in the solution. This question does not involve more complex topics such as arrays.
FRQ Question 1 on Control Structures will probably involve:
  • a for or while loop that probably uses the method’s parameter variables,
  • an if statement, probably inside the loop,
  • calls to other class methods given to you,
  • a numerical or string value that is calculated by the loop and returned at the end of the method.
  • Part A will require a loop and part B will require String methods (with or without a loop).
On question 1, you will get points for:
Try to have some code for each of these steps. Do not use arrays or other more complex code. You may need to use Math and String methods.

Subsection 2.25.1 FRQ 1 MessageBuilder

The following free response question is a great FRQ 1 example from page 168 of the 2025-2026 AP CSA Course and Exam Description
 1 
https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf
.
This question involves the MessageBuilder class, which is used to generate a message based on a starting word. The MessageBuilder class contains a helper method, getNextWord. You will write a constructor and a method in the MessageBuilder class.
public class MessageBuilder
{
  private String message; // To be initialized in part (a)
  private int numWords;   // To be initialized in part (a)

  /**
   *  Builds a message starting with the word specified by the 
   *  parameter and counts the number of words in the message,
   *  as described in part (a)
   *  Precondition: startingWord is a single word with no spaces.
   */
  public MessageBuilder(String startingWord)
  { /* to be implemented in part (a) */ }

  /**
   * Returns a word to follow the word specified by the 
   * parameter or null if there are no remaining words.
   * Precondition: s is a single word with no spaces. 
   * Postcondition: Returns an individual word with no spaces.
   */
  public String getNextWord(String s)
  { /* implementation not shown */ }

  /**
   * Returns an abbreviation for the instance variable message,
   * as described in part (b)
   * Preconditions: Each word in message is separated by a single space.
   *                message contains two or more words.
   * Postcondition: message is unchanged.
   */
  public String getAbbreviation()
  { /* to be implemented in part (b) */ }

  /* There may be instance variables, constructors, and methods that are not shown. */
}

Subsection 2.25.2 Part A: MessageBuilder Constructor

In Part A, you will write the MessageBuilder constructor, which uses a helper method to create a message starting with the word specified by the parameter startingWord and assigns the message to the instance variable message. The constructor also counts the number of words in the message and assigns the count to the instance variable numWords.
Each word in the message will be separated by a single space. A helper method, getNextWord, has been provided to obtain words to be added to the message. Each call to getNextWord returns the next word in the message based on the word most recently added. When there are no more words to be added, getNextWord returns null.
Example 1: Consider the following calls to getNextWord, which are made within the MessageBuilder class. The return value of each call is used in the next call.
Table 2.25.1.
Method Call Return Value
getNextWord("the") "book"
getNextWord("book") "on"
getNextWord("on") "the"
getNextWord("the") "table"
getNextWord("table") null
Based on these return values, a call to the MessageBuilder constructor with argument "the" should set the instance variable message to "the book on the table" and should set the instance variable numWords to 5.
Example 2: Now consider a different set of calls to getNextWord. Again, the return value of each call is used as input to the next.
Table 2.25.2.
Method Call Return Value
getNextWord("good") "morning"
getNextWord("morning") "sunshine"
getNextWord("sunshine") null
Based on this, a call to the MessageBuilder constructor with argument "good" should set message to "good morning sunshine" and numWords to 3.

Subsection 2.25.3 How to solve the MessageBuilder constructor

Here are some exercises to help you to tackle this FRQ. First, identify and highlight the methods and variables that you must use in the problem.
We will learn more about writing constructors in the next unit, but for now, you can think of a constructor as a method that is called when an object is created to initialize the instance variables of the class, in this case message and numWords.
You will write a loop that adds to the message variable using the getNextWord method and increments numWords as you add more words to the message. The constructor starts with startingWord and repeatedly adds the result of getNextWord until it returns null. Each word is separated by a space.
You can declare a temporary variable such as nextWord as your loop variable to store the result of getNextWord, and update message and numWords in each loop iteration.
Now let’s plan the loop. The following questions will help guide your thinking.

Activity 2.25.1.

Which loop structure is best suited to keep building the message until getNextWord returns null?
  • for loop with fixed number of iterations
  • The number of words is not known in advance, so a for loop with a fixed range won’t work.
  • while loop that continues until getNextWord returns null
  • Correct. A while loop is appropriate because we don’t know in advance how many words will be added.

Activity 2.25.2.

What is the purpose of the variable nextWord in the loop?
  • It holds the entire message being built.
  • No, the message variable holds the full message. nextWord holds each new word temporarily.
  • It stores the word returned by getNextWord in each iteration.
  • Correct. nextWord lets you keep track of the latest word returned before adding it to the message.
  • It counts how many words have been added.
  • No, that’s the purpose of the numWords variable.
As you write the loop, remember the 3 steps of the loop:
  1. Initialize the loop variable nextWord using the getNextWord method with the starting word as an argument.
  2. Test the loop variable to make sure it is not null.
  3. Change the loop variable to the next word using the getNextWord method with the last word as an argument.

Subsection 2.25.4 Solve MessageBuilder Part A

Complete the MessageBuilder constructor below.

Activity 2.25.3.

Complete the MessageBuilder constructor below that builds a message starting with the startingWord specified by the parameter. It should use a while loop to add on to the message using the given getNextWord method and increment numWords. Make sure there are spaces between the words in the message.

Subsection 2.25.5 Part B: getAbbreviation

Starting in 2026, the AP exam will require students to use String methods in part B of FRQ 1.
In MessageBuilder Part B, you are asked to write the getAbbreviation method, which returns an abbreviation consisting of the first letter of each word in message. Assume that message consists of two or more words, each separated by a single space. For example, if the value of message is "as soon as possible", then getAbbreviation() should return "asap".

Activity 2.25.4.

Which of the following String method can be used to return the first letter of a word?
  • length()
  • You do not need to know the length of a string to return its first letter.
  • indexOf(String s)
  • You do not need to know the index of a substring.
  • substring(int start, int end)
  • substring could be used to get the first letter of a string.
  • equals(String another)
  • equals is used for comparisons, which are not needed for this method.

Activity 2.25.5.

Which of the following String method can be used to find a space character in the message?
  • length()
  • You do not need to know the length of the string.
  • indexOf(String s)
  • You can use indexOf(" ") to find the index of a space in a string.
  • substring(int start, int end)
  • substring only works if you know the index of the space.
  • equals(String another)
  • equals is used for comparisons, which are not needed here.

Activity 2.25.6.

Which of the following loop patterns can be used to find the next space in a String?
  • for(int i=0; i < message.length; i++)
  • This iterates through the message letter by letter, but it doesn’t find the next space. You could make it work with an if statement inside to find the space, but there is another solution.
  • while(message.substring(0,1) == " ")
  • This while loop would only check the first character. You could make this work if you used an index variable i but there is another solution.
  • while( tempMessage.indexOf(" ") >= 0 )
  • You can use indexOf(" ") to find the index of a space in a string easily. You can copy message into tempMessage so that you can change it to skip to the next word.

Subsection 2.25.6 AP Scoring Rubric

Here is the AP rubric for this problem. Did your code meet the requirements for all 4 points? Notice that even a partial solution would get some of the points. It is not all or nothing. In class, your teacher may have you grade each others’ code.
Figure 2.25.3. AP Rubric

Subsection 2.25.7 Solve MessageBuilder Part B

Complete the getAbbreviation() method below which returns a String that consists of the first letter of each word in the message. Assume that message consists of two or more words, each separated by a single space.

Activity 2.25.7.

Complete the getAbbreviation() method below that returns a String that consists of the first letter of each word in the message. Assume that message consists of two or more words, each separated by a single space.

Subsection 2.25.8 AP Scoring Rubric

Here is the AP rubric for this problem. Did your code meet the requirements for all 7 points? Notice that even a partial solution would get some of the points. It is not all or nothing. There are also many other solutions possible. In class, your teacher may have you grade each others’ code.
Figure 2.25.4. AP Rubric
You have attempted of activities on this page.