Unit 2 FRQ Practice
Write AP style methods that use selection and iteration, check them against built in tests, and learn what the FRQ rubric rewards.
Unit 2 FRQ Practice: Methods and Control Structures
Why this page: AP CSA free response question 1 is always “Methods and Control Structures.” It asks you to write a method that uses exactly what Unit 2 covers: Boolean expressions, if statements, and loops.
How FRQ 1 Works
- You get a class description and one or two methods to write, labeled (a) and (b).
- Each part is worth points on a rubric. Points are earned for specific things: the correct loop bounds, the right condition, updating a variable, returning the result.
- Part (b) often lets you call the method from part (a). Do it, even if your part (a) is wrong. The rubric grades (b) as if (a) works.
- You write on paper, so there is no compiler. Small syntax slips usually cost nothing; wrong logic costs points.
Habits That Earn Points
- Copy the method header exactly as given. Do not rename parameters.
- Declare and initialize your accumulator (
int count = 0;) before the loop. - Check the loop bounds:
i < s.length()for characters,i <= s.length() - kfor windows of lengthk. - Compare Strings with
.equals, never==. - Put the
returnafter the loop, not inside it, unless you mean to stop early. - Reread the question after writing. Did you handle the edge case it mentions?
Activity 1: Spot the Lost Point
Each response below would lose rubric points. Find why.
The method should return the number of characters in s that equal target. Which rubric point does this response lose?
The method should return true if any digit of n is 7. Which rubric point does this response lose?
The method should return the sum of the multiples of 3 from 1 through max. Which rubric point does this response lose?
Activity 2: Assemble a Model Answer
Put the lines of this full credit response in order. The method returns the index of the first character that also appears earlier in s, or -1 if there is none. Note the early return inside the loop.
FRQ A: Digit Analysis
A DigitTools class analyzes the digits of positive integers.
(a) Write countDigitsAbove, which returns how many digits of num are greater than threshold. For example, countDigitsAbove(4718, 5) returns 2 because 7 and 8 are above 5.
(b) Write isIncreasing, which returns true when every digit of num is strictly greater than the digit to its left. For example, isIncreasing(1359) returns true and isIncreasing(1339) returns false. A one digit number is increasing.
Write both methods, then run the tests.
Code Runner Challenge
FRQ A: digit analysis. Write both methods and run the tests.
View IPYNB Source
// CODE_RUNNER: FRQ A: digit analysis. Write both methods and run the tests.
public class DigitTools {
/** Part (a): number of digits of num that are greater than threshold. */
public static int countDigitsAbove(int num, int threshold) {
int count = 0;
// TODO: peel off digits with % 10 and / 10, count the ones above threshold
return count;
}
/** Part (b): true when each digit is greater than the digit to its left. */
public static boolean isIncreasing(int num) {
// TODO: compare each digit (num % 10) with the digit to its left ((num / 10) % 10)
// while num has at least two digits
return true;
}
public static void main(String[] args) {
System.out.println("countDigitsAbove(4718, 5) = " + countDigitsAbove(4718, 5) + " expected 2");
System.out.println("countDigitsAbove(999, 8) = " + countDigitsAbove(999, 8) + " expected 3");
System.out.println("countDigitsAbove(123, 5) = " + countDigitsAbove(123, 5) + " expected 0");
System.out.println("isIncreasing(1359) = " + isIncreasing(1359) + " expected true");
System.out.println("isIncreasing(1339) = " + isIncreasing(1339) + " expected false");
System.out.println("isIncreasing(7) = " + isIncreasing(7) + " expected true");
System.out.println("isIncreasing(3210) = " + isIncreasing(3210) + " expected false");
}
}
DigitTools.main(null);
FRQ B: Word Scanner
A WordScanner class inspects a String text made of lowercase letters and single spaces.
(a) Write countVowels, which returns the number of characters in text that are a, e, i, o, or u.
(b) Write longestWordLength, which returns the length of the longest word in text. Words are separated by single spaces and there are no leading or trailing spaces. For example, longestWordLength("the quick brown fox") returns 5. You may call countVowels if it helps, but you do not need to.
Write both methods, then run the tests.
Code Runner Challenge
FRQ B: word scanner. Write both methods and run the tests.
View IPYNB Source
// CODE_RUNNER: FRQ B: word scanner. Write both methods and run the tests.
public class WordScanner {
/** Part (a): number of vowels in text. */
public static int countVowels(String text) {
int vowels = 0;
// TODO: one character window, compound || condition, count
return vowels;
}
/** Part (b): length of the longest word in text. */
public static int longestWordLength(String text) {
int longest = 0;
int current = 0;
// TODO: walk the characters; a space ends a word, anything else makes the
// current word longer; keep the largest length seen, and check the last word
return longest;
}
public static void main(String[] args) {
System.out.println("countVowels(\"the quick brown fox\") = " + countVowels("the quick brown fox") + " expected 5");
System.out.println("countVowels(\"rhythm\") = " + countVowels("rhythm") + " expected 0");
System.out.println("longestWordLength(\"the quick brown fox\") = " + longestWordLength("the quick brown fox") + " expected 5");
System.out.println("longestWordLength(\"a bb ccc\") = " + longestWordLength("a bb ccc") + " expected 3");
System.out.println("longestWordLength(\"word\") = " + longestWordLength("word") + " expected 4");
}
}
WordScanner.main(null);
Activity 3: Grade Yourself
After your tests pass, answer these two rubric questions honestly.
In FRQ A part (b), what should isIncreasing(3210) return? Type true or false.
In FRQ B part (b), how many times does a correct loop over "the quick brown fox" execute its body if it visits every character once? Type a number.
Summary
- FRQ 1 rewards exactly the Unit 2 skills: bounds, conditions, accumulators, and returns.
- Write the header exactly, initialize before the loop, compare Strings with
.equals, return after the loop. - Test with the examples the question gives you, then add an edge case of your own.
- More practice: the Unit 2 quiz has two more challenges in this style.