Informal Code Analysis (Bonus)
Informal Code Analysis
What You'll Learn
- 4.17.A: Describe the behavior and conditions that produce specified results from algorithms involving collections.
- Determine what a given method does without running it.
- Identify the preconditions required for a method to work correctly.
- Predict the output or return value of methods involving arrays, ArrayLists, 2D arrays, and recursion.
- Recognize correct and incorrect implementations of standard algorithms.
📌 Unit 4 Capstone Lesson
bonus lesson brings together every skill from Unit 4. Informal code analysis — reading code and reasoning about what it does — is tested on both the MCQ and FRQ sections of every AP exam. The questions here mirror the difficulty and style of real AP exam questions covering arrays, ArrayLists, 2D arrays, sorting, searching, and recursion.
What Is Informal Code Analysis?
Informal code analysis means reading a method and answering questions like:
- "What does this method return?"
- "What does this method print?"
- "Under what conditions does this method produce the correct result?"
- "What is wrong with this implementation?"
You're not running the code — you're tracing it mentally, applying your understanding of the language and algorithms.
Strategy: Read the Method in Layers
- Identify the return type and parameters — what goes in and what comes out.
- Find the loop structure — what range does it cover, and does it modify the collection?
- Trace with a small example — pick 3–4 element input and trace manually.
- Check the edge cases — empty collection, single element, all-same values.
- Match to known patterns — is this min/max? filter? linear search? insertion sort?
✅ Example: Identify What This Method Does
public static int mystery(int[] arr) {
int result = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < result) {
result = arr[i];
}
}
return result;
}
Layer by layer: returns int, takes array. Loop from 1 to end. Compares each element to result and updates when smaller. Initializes to arr[0]. This is the minimum algorithm.
✅ Example: Identify the Precondition
public static int binarySearch(int[] arr, int target) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) return mid;
else if (target < arr[mid]) high = mid - 1;
else low = mid + 1;
}
return -1;
}
Precondition: the array must be sorted in ascending order. Without this, the algorithm may eliminate the wrong half and return -1 even if the target exists.
Common AP Exam Analysis Patterns
The AP exam repeatedly tests these analysis questions across Unit 4 topics:
-
Off-by-one: Does the loop start at 0 or 1? Does it use
<or<=? - Wrong getter: Does the method call the right getter method on each object?
-
Early return: Is
return falseinside or outside the loop? - Initialization: Is min/max initialized to 0 or to the first element?
- Modification during traversal: Does the method remove elements inside a for-each loop?
- Integer division: Is the average cast to double before or after dividing?
-
Wrong comparison: Is
==used for Strings instead of.equals()?
Summary
- Informal code analysis means reading code and reasoning about its behavior without running it.
- Trace with a small example to verify your understanding quickly.
- Preconditions are assumptions a method makes about its inputs — binary search requires sorted input; min/max assumes the array is non-empty.
- Every bug pattern from Units 1–4 can appear in an analysis question.
Practice Questions
public static int mystery(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) count++;
}
return count;
}
public static int findMax(ArrayListlist) { int max = list.get(0); for (int i = 1; i < list.size(); i++) { if (list.get(i) > max) max = list.get(i); } return max; }
list.get(0) on the first line — if the list is empty, this throws an IndexOutOfBoundsException. The method requires at least one element. A is wrong — the method works on unsorted lists. B is wrong — it handles negatives correctly by initializing to the first element.public static boolean mystery(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) return false;
}
return true;
}
public static double average(ArrayListlist) { int total = 0; for (int x : list) { total += x; } return total / list.size(); }
list.size() should be list.length.total should be declared as a double.total (int) and list.size() (int) are integers — integer division truncates. Fix: return (double) total / list.size(). The method signature says it returns double but the division produces an int that gets silently widened — the decimal is already lost before the return. D would also work (declaring total as double avoids integer division) but A is the most precise description of the bug.mystery(new int[][]{{1,2},{3,4},{5,6}})?
public static int mystery(int[][] grid) {
int sum = 0;
for (int row = 0; row < grid.length; row++) {
sum += grid[row][grid[row].length - 1];
}
return sum;
}
Mastery: Informal Code Analysis
Student class has getName() and getScore(). What bug does this method contain?
public static String topStudent(ArrayListroster) { int maxScore = 0; String topName = ""; for (Student s : roster) { if (s.getScore() > maxScore) { maxScore = s.getScore(); topName = s.getName(); } } return topName; }
topName should be initialized to roster.get(0).getName(), not "".maxScore should be initialized to roster.get(0).getScore(), not 0.maxScore stays 0 and topName stays "". The method returns an empty string instead of the actual top student's name. Initializing maxScore to the first student's score (and topName to the first student's name) fixes this.public static int mystery(int[][] grid) {
int total = 0;
for (int i = 0; i < grid.length; i++) {
total += grid[i][i];
}
return total;
}
mystery(6)?
public static String mystery(int n) {
if (n == 0) return "";
return mystery(n / 2) + (n % 2);
}
public static boolean contains(int[] arr, int target) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) return true;
else if (target < arr[mid]) high = mid - 1;
else low = mid + 1;
}
return false;
}
Book class has getGenre() (String) and getPages() (int). Which correctly describes what this method returns?
public static int mystery(ArrayListbooks, String genre) { int total = 0; for (Book b : books) { if (b.getGenre().equals(genre)) { total += b.getPages(); } } return total; }
b.getPages() — a sum of page counts. A would use count++ instead of total += b.getPages(). C would divide by a count at the end. D would track a Book object and return a String. This is a genre-filtered page-count sum.Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]