Overview
AP Computer Science Principles Practice 4: Code Analysis is the skill of evaluating and testing algorithms and programs. You read code that someone else wrote (or that you wrote), explain what it does, predict the output, and find and fix mistakes. This is the practice that shows up every time a question hands you a code segment and asks "what happens here" or "which value breaks this."
Practice 4 has three subskills:
- 4.A: Explain how a code segment or program functions.
- 4.B: Determine the result of code segments.
- 4.C: Identify and correct errors in algorithms and programs, including error discovery through testing.
All three appear on the multiple-choice section and can appear in the Create written responses.

What Practice 4: Code Analysis Means
Code analysis is reading code carefully and reasoning about it instead of writing new code from scratch. You act like a debugger and a reviewer at the same time.
Three things you do:
- Explain function (4.A): Put into words what a code segment accomplishes and how it gets there.
- Determine results (4.B): Trace the code step by step and figure out the final value, output, or behavior.
- Fix errors (4.C): Spot logic, syntax, or runtime problems and choose a change that makes the code work as intended. Testing with specific inputs is how you find these problems.
This practice uses the Exam Reference Sheet language, which works for both block-based and text-based programming. You do not need a specific programming language to do code analysis.
What This Practice Requires
To do Practice 4 well, you need to slow down and track state. Most errors come from skimming.
- Read the intended purpose first. Many questions tell you what the code is supposed to do.
- Trace variables line by line. Keep a small table of values if needed.
- Watch loop conditions, especially when a loop should stop.
- Check where variables are initialized. A reset in the wrong place is a common bug.
- Test edge cases. Try inputs that are equal, out of order, or at the boundary.
Skills You Need for This Practice
You pull from across Unit 3 (Algorithms and Programming) and Unit 1 (Creative Development) to analyze code:
- Variables and assignment (3.1): know that
x ← x + 1reads the old value and stores a new one. - Boolean expressions (3.5): evaluate AND, OR, and NOT correctly.
- Conditionals and nested conditionals (3.6, 3.7): follow which branch runs.
- Iteration (3.8): track loop counters and stopping conditions.
- Lists (3.10): use correct indexing and avoid going past the end.
- Procedures (3.12, 3.13): understand parameters, return values, and where a
RETURNbelongs. - Identifying and correcting errors (1.4): tell apart logic, syntax, and runtime errors and pick a correction.
How It Shows Up on the AP Exam
The multiple-choice section is 70 questions worth 70% of the exam. Practice 4 questions are common in the algorithms and programming content (Big Idea 3 is weighted 30 to 35%).
Typical MCQ formats:
- Boolean logic (4.B): Given several expressions, decide which evaluate to true under a condition. Example: which expression is true if a person is old enough to drive but not old enough to vote. The answer combined
(age ≥ 16) AND (NOT(age ≥ 18))with the equivalent(age < 18) AND (NOT(age < 16)). - Counterexample (4.C): A code segment finds the maximum of three values but fails sometimes. You pick the input set that exposes the bug, such as
x=3, y=2, z=1. This is error discovery through testing. - Bug fix (4.C): A
countNumOccurencesprocedure does not count correctly becausecountis reset inside the loop. The fix moves the initialization to before the loop so it only runs once.
For the Create written responses, you may need to explain how part of your own program works (4.A) and describe how you tested it (4.C).
Examples Across the Course
Code analysis connects to many parts of the course, not just one unit.
- Unit 2, Binary (4.B): Add 1 to the binary ID
1001 0011to get1001 0100. Determining a result is code analysis even when the "code" is arithmetic on data. - Unit 3, Robot grid (4.B): Trace move and turn commands to see which segment walks the robot along an arrow path to the gray square.
- Unit 3, Drawing loop (4.B): Decide which
REPEATsegments produce a figure when order ofdrawCircle,r ← r+1, andy ← y+1changes inside the loop. Order of statements changes the result. - Unit 3, Missing loop steps (4.A and 4.C): An algorithm counts list values over 100 but is missing steps. The correct fix increases
positionby 1 and repeats untilpositionis greater thann. The wrong options stop oncount, which breaks the logic. - Unit 3, Procedure bug (4.C): The occurrence counter only works when
countis initialized once, before theFOR EACHloop.
How to Practice Practice 4: Code Analysis
These are practical study habits, not official rules.
- Build a trace table. For each line, write the current value of every variable. This catches loop and initialization bugs fast.
- State the goal in one sentence. Before tracing, write what the code should produce so you can compare.
- Test with boundary inputs. Use equal values, descending values, an empty list, and a single element to find where code fails.
- For Boolean questions, test true and false cases. Plug in a value that should pass and one that should fail, then confirm each expression matches.
- Read every answer choice. Bug-fix questions often include changes that look reasonable but break something else.
- Rewrite tricky lines in plain words. "Reset count to 0" inside a loop reveals why a counter keeps resetting.
Common Mistakes
- Skipping the initialization spot. A counter or accumulator reset inside the loop instead of before it is one of the most tested bugs.
- Misreading loop stop conditions. Stopping on the wrong variable, like
countinstead ofposition, changes everything. - Off-by-one errors. Forgetting that lists may be indexed from 1 to n, or running one step too many or too few.
- Boolean confusion. Mixing up AND with OR, or mishandling NOT. Remember
NOT(age ≥ 18)is the same asage < 18. - Picking one counterexample without testing it. For "which input shows the bug," actually run each choice through the code instead of guessing.
- Ignoring statement order. In loops, doing the action before or after updating a variable produces different output.
Quick Review
- Practice 4 is about reading, tracing, and fixing code, not writing it from scratch.
- 4.A: explain what code does and how.
- 4.B: trace step by step to find the result.
- 4.C: find and fix logic, syntax, and runtime errors, and use testing to discover them.
- Use trace tables, boundary inputs, and a clear statement of the intended goal.
- Watch initialization placement, loop conditions, indexing, Boolean logic, and statement order.
- These skills show up most in Big Idea 3 questions and in your Create written responses.