Fiveable
💻AP Computer Science A
​

💻AP Computer Science A

FRQ 1 – Methods and Control Structures
​
Unit 1: Using Objects and Methods
​
AP CS A FRQ Types & Units

Each FRQ type tests specific skills taught in particular units. Here's why certain units appear for each question type:

FRQFocusUnitsWhy
FRQ 1Methods and Control Structures1-2Tests method writing, loops, conditionals - foundational programming from Units 1-2
FRQ 2Classes3Tests class design, constructors, instance variables - all Unit 3 OOP content
FRQ 3Array/ArrayList4Tests 1D array and ArrayList traversal, manipulation - Unit 4 content
FRQ 42D Array4Tests 2D array traversal and manipulation - Unit 4 content

This mapping reflects College Board's exam structure - each FRQ type tests specific skills that are taught in particular units.

Practice FRQ 1 of 191/19

1. This question involves the PlaylistBuilder class, which is used to generate a music playlist based on a starting song and a time limit. The PlaylistBuilder class contains helper methods to determine the next song and the duration of a song. You will write a constructor and a method in the PlaylistBuilder class.

</>Java
public class PlaylistBuilder
{
    /** The string containing the list of songs in the playlist */
    private String playlist; // To be initialized in part (a)

    /** The total duration of the playlist in minutes */
    private int totalDuration; // To be initialized in part (a)

    /**
     * Constructs a playlist starting with the song specified by the
     * parameter and adds subsequent songs until the playlist is full,
     * as described in part (a)
     * Precondition: firstSong is a non-empty string.
     *               maxMinutes > 0
     */
    public PlaylistBuilder(String firstSong, int maxMinutes)
    { /* to be implemented in part (a) */ }

    /**
     * Returns the song that should follow the parameter song
     * or null if there are no more songs to add.
     * Precondition: currentSong is a non-empty string.
     */
    public String getNextSong(String currentSong)
    { /* implementation not shown */ }

    /**
     * Returns the duration of the parameter song in minutes.
     * Precondition: song is a non-empty string.
     */
    public int getDuration(String song)
    { /* implementation not shown */ }

    /**
     * Returns a summary string containing the first few characters
     * of each song in the playlist, as described in part (b)
     * Precondition: playlist contains at least one song.
     */
    public String createSummary()
    { /* to be implemented in part (b) */ }

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

Write the PlaylistBuilder constructor, which initializes the playlist and totalDuration instance variables. The constructor starts the playlist with the parameter firstSong and adds its duration to totalDuration. It then repeatedly calls the helper method getNextSong to obtain the next song to add.

Each new song is added to the playlist string, preceded by the delimiter " -> ", and its duration is added to totalDuration. This process continues as long as a next song exists (is not null) AND adding the song would not cause totalDuration to exceed maxMinutes.

The playlist string consists of song titles separated by " -> ". A helper method, getNextSong, returns the next song based on the current song. A second helper method, getDuration, returns the duration of a song in minutes.

Example 1

Consider the following helper method calls. Assume maxMinutes is 20.
Method CallReturn Value
getDuration("Song A")5
getNextSong("Song A")"Song B"
getDuration("Song B")4
getNextSong("Song B")"Song C"
getDuration("Song C")6
getNextSong("Song C")"Song D"
getDuration("Song D")10
A call to new PlaylistBuilder("Song A", 20) starts with "Song A" (5 mins). It adds "Song B" (4 mins, total 9). It adds "Song C" (6 mins, total 15). It checks "Song D" (10 mins), but 15 + 10 = 25 which is > 20, so "Song D" is NOT added. The final playlist is "Song A -> Song B -> Song C" and totalDuration is 15.
Complete the PlaylistBuilder constructor.
/**
 * Constructs a playlist starting with the song specified by the
 * parameter and adds subsequent songs until the playlist is full,
 * as described in part (a)
 * Precondition: firstSong is a non-empty string.
 *               maxMinutes > 0
 */
public PlaylistBuilder(String firstSong, int maxMinutes)
b.

Write the createSummary method, which returns a string consisting of the first 3 characters of each song title in the playlist. If a song title has fewer than 3 characters, the entire title is included in the summary. The summary should not include the " -> " delimiters.

Example 1

Assume the instance variable playlist contains the following string:
Instance VariableValue
playlist"Hello -> World -> Of -> Java"
The method should return "HelWorOfJav". - "Hello": first 3 chars are "Hel" - "World": first 3 chars are "Wor" - "Of": length is 2, so include "Of" - "Java": first 3 chars are "Jav"
Complete the createSummary method.
/**
 * Returns a summary string containing the first few characters
 * of each song in the playlist, as described in part (b)
 * Precondition: playlist contains at least one song.
 */
public String createSummary()






Pep

essential ap study content awaits..

Features
Testimonials
Testimonials
start studying →
FRQ Directions
Free Response Question Practice

This practice environment simulates the AP AP Computer Science A Free Response Questions section. Here are some guidelines:

  • Read each question carefullybefore responding. Pay attention to command verbs like "identify," "explain," "analyze," or "evaluate."
  • Use the timer to practice time management. You can pause, restart, or hide the timer as needed.
  • Mark for Review if you want to come back to a question later.
  • Your responses are saved automatically as you type. You can also use the drawing tool for questions that require diagrams or graphs.
  • Use the toolbar for formatting options like bold, italic, subscript, and superscript.
  • Navigate between questions using the Previous and Next buttons at the bottom of the screen.

Tip: Answer all parts of each question. Partial credit is often available, so even if you are unsure, provide what you know.