Fiveable

⌨️AP Computer Science Principles Review

QR code for AP Computer Science Principles practice questions

Practice 4: Code Analysis

Practice 4: Code Analysis

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
⌨️AP Computer Science Principles
Unit & Topic Study Guides

AP Computer Science Principles Exam

Pep mascot

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.

Pep mascot
more resources to help you study

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 + 1 reads 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 RETURN belongs.
  • 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 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 (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.
  • 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 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 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.
Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot