---
title: "AP CSP Practice 4: Code Analysis Study Guide"
description: "Learn AP Computer Science Principles Practice 4: Code Analysis. Explain how code works, trace results, and find and fix errors through testing."
canonical: "https://fiveable.me/ap-comp-sci-p/computational-thinking-practices/practice-4-code-analysis/study-guide/pRO6QyOT1eUebXzUUGKz"
type: "study-guide"
subject: "AP Computer Science Principles"
unit: "Computational Thinking Practices"
lastUpdated: "2026-06-17"
---

# AP CSP Practice 4: Code Analysis Study Guide

## Summary

Learn AP Computer Science Principles Practice 4: Code Analysis. Explain how code works, trace results, and find and fix errors through testing.

## Guide

## Overview

[AP Computer Science Principles](/ap-comp-sci-p "fv-autolink") Practice 4: Code Analysis is the skill of evaluating and testing [algorithms](/ap-comp-sci-p/key-terms/algorithm "fv-autolink") 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](/ap-comp-sci-p/key-terms/procedure "fv-autolink") (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](/ap-comp-sci-p/key-terms/syntax "fv-autolink"), 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](/ap-comp-sci-p/unit-5/beneficial-harmful-effects/study-guide/rErWKPcu55DLj7N7L8hZ "fv-autolink") first. Many questions tell you what the code is *supposed* to do.
- Trace [variables](/ap-comp-sci-p/key-terms/variable "fv-autolink") 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](/ap-comp-sci-p/unit-3 "fv-autolink") (Algorithms and Programming) and Unit 1 (Creative Development) to analyze code:

- **Variables and assignment** (3.1): know that `x ← x + 1` reads the old value and stores a new one.
- **[Boolean expressions](/ap-comp-sci-p/unit-3/boolean-expressions/study-guide/LkLTi80KQM04AnmNBxCq "fv-autolink")** (3.5): evaluate AND, OR, and NOT correctly.
- **Conditionals and [nested conditionals](/ap-comp-sci-p/unit-3/nested-conditionals/study-guide/iM5nw43cqaIowgYhEjyD "fv-autolink")** (3.6, 3.7): follow which branch runs.
- **[Iteration](/ap-comp-sci-p/unit-3/iteration/study-guide/7megnIMaNHzDdrVKT9mR "fv-autolink")** (3.8): track loop counters and stopping conditions.
- **[Lists](/ap-comp-sci-p/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h "fv-autolink")** (3.10): use correct indexing and avoid going past the end.
- **Procedures** (3.12, 3.13): understand [parameters](/ap-comp-sci-p/key-terms/parameters "fv-autolink"), [return values](/ap-comp-sci-p/unit-3/calling-procedures/study-guide/lwdr3yhVOtUJZhAmJ5cu "fv-autolink"), and where a `RETURN` belongs.
- **[Identifying and correcting errors](/ap-comp-sci-p/unit-1/identifying-correcting-errors/study-guide/pz4hGcBBElziI3R8cfnQ "fv-autolink")** (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](/ap-comp-sci-p/unit-3/variables-assignments/study-guide/vtJhAf5XFOkm1uHNDMvh "fv-autolink") logic (4.B):** Given several expressions, decide which evaluate to true under a [condition](/ap-comp-sci-p/key-terms/condition "fv-autolink"). 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 `countNumOccurences` procedure does not count correctly because `count` is 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](/ap-comp-sci-p/unit-2/review/study-guide/YlNeQAM5snCDFEtp0CGd "fv-autolink") (4.B):** Add 1 to the binary ID `1001 0011` to get `1001 0100`. Determining a result is code analysis even when the "code" is arithmetic on [data](/ap-comp-sci-p/unit-2/extracting-information-data/study-guide/EFuLgc6tL71cegDFjXRl "fv-autolink").
- **Unit 3, Robot grid (4.B):** Trace move and turn commands to see which segment walks the robot along an arrow [path](/ap-comp-sci-p/unit-4/internet/study-guide/HouTEH6ypgVs8tNInelL "fv-autolink") to the gray square.
- **Unit 3, Drawing loop (4.B):** Decide which `REPEAT` segments produce a figure when order of `drawCircle`, `r ← r+1`, and `y ← y+1` changes inside the loop. Order of statements changes the result.
- **Unit 3, Missing loop steps (4.A and 4.C):** An algorithm counts [list](/ap-comp-sci-p/unit-3/data-abstraction/study-guide/kMMTClSiHohfiaHMGFFE "fv-autolink") values over 100 but is missing steps. The correct fix increases `position` by 1 and repeats until `position` is greater than `n`. The wrong options stop on `count`, which breaks the logic.
- **Unit 3, Procedure bug (4.C):** The occurrence counter only works when `count` is initialized once, before the `FOR EACH` loop.

## 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 `count` instead of `position`, 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 as `age < 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.
