AP Computer Science A · Exam guide

AP Computer Science A Question 1 Explained: Methods and Control Structures

Question 1 asks you to write one or two methods that use loops, conditions and calls to other methods. It is worth 9 points and is often the most approachable question on the section. The example below was compiled and run to make sure it works.

A sample question

A class contains the static method countVowels(String str), which returns the number of vowels (a, e, i, o, u) in str. Write moreVowels(String a, String b), which returns true if a has more vowels than b, and false otherwise. Then write countVowels.

A full-credit solution

public static int countVowels(String str)
{
    int count = 0;
    for (int i = 0; i < str.length(); i++)
    {
        String ch = str.substring(i, i + 1);
        if ("aeiou".indexOf(ch) >= 0)
        {
            count++;
        }
    }
    return count;
}

public static boolean moreVowels(String a, String b)
{
    return countVowels(a) > countVowels(b);
}

Tested: countVowels("education") returns 5, and moreVowels("queue", "rhythm") returns true.

How the points are earned

PieceWhere it appears
Declares and initializes the accumulatorint count = 0;
Loops over every positioni runs from 0 to str.length() - 1, so no character is missed and none is out of range
Tests the condition"aeiou".indexOf(ch) >= 0
Updates on each qualifying passcount++;
Returns at the right timereturn count; after the loop
Calls the given methodmoreVowels uses countVowels instead of repeating its loop
RowPointsWhat earns it
Correctness of the algorithm6Six one-point pieces: declares the needed variables; loops over the correct range; tests the stated condition; updates the right variable on each pass; handles edge cases; returns the right value at the right time.
Use of the Java API and syntax2One point for using the given methods and the ArrayList and array syntax correctly; one for Java that would compile, with matching headers and declared variables.
Program design and encapsulation1Uses the class's own methods and keeps data private, as the question's design requires.
Total9

Habits for Question 1

  • Use the method headers exactly as given.
  • When a helper method is provided or you wrote it, call it. Rewriting it risks mistakes and loses the design point.
  • Use str.substring(i, i + 1) to get one character as a String, and compare Strings with equals.
  • Return after the loop unless the question needs you to stop early.

Common mistakes

  • Looping to str.length() instead of stopping before it, which throws an error.
  • Comparing Strings with == instead of equals.
  • Returning inside the loop, which stops after the first character.
  • Forgetting to initialize the counter.

Frequently asked questions

What is Question 1 about?

Writing methods with loops, conditions and calls to other methods. It is one of four free-response questions, each worth 9 points.

Do I need to write the whole class?

No. The question gives you the class header and tells you which methods to write.

Can I use an enhanced for loop with a String?

No. Loop over the index and use substring or charAt to get each character.

What if I cannot finish the second method?

Write what you can. Each method is scored on its own pieces.

Where can I find the official scoring rules?

Exam formats change a little from year to year, so check the current course and exam description on the College Board site before test day.

Keep practicing

The AP Computer Science A course has eight free-response prompts, two of each exam type, each with a coding space, the scoring notes, and the canonical solution explained point by point. These free lessons also build the skills the free-response section tests:

See the AP Computer Science A course Free lessons and lesson list

Other free-response guides:

AP is a registered trademark of the College Board, which is not affiliated with and does not endorse this site. This guide is our own explanation of the question types and scoring. For the official scoring guidelines, see the College Board.