Informal Code Analysis (Bonus)

Unit 4 · Bonus Lesson · Code Analysis

Informal Code Analysis

🕑 40–50 min · 10 Practice Questions · Unit 4 Capstone · AP Exam Strategy

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

  1. Identify the return type and parameters — what goes in and what comes out.
  2. Find the loop structure — what range does it cover, and does it modify the collection?
  3. Trace with a small example — pick 3–4 element input and trace manually.
  4. Check the edge cases — empty collection, single element, all-same values.
  5. 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 false inside 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

MCQ 1
What does this method return when called with {3, 1, 4, 1, 5, 9}?
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;
}
Identify what the method counts, then trace.
A 6
B 3
C 2
D 1
C. This method counts how many times an element is greater than the next (a "descent"). Array: {3,1,4,1,5,9}. 3>1 ✓ (count=1), 1<4 ✗, 4>1 ✓ (count=2), 1<5 ✗, 5<9 ✗. Count = 2.
MCQ 2
What is the precondition for this method to work correctly?
public static int findMax(ArrayList list) {
    int max = list.get(0);
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i) > max) max = list.get(i);
    }
    return max;
}
A The list must be sorted in ascending order.
B The list must contain only positive integers.
C The list must contain at least one element.
D There are no preconditions — this works on any ArrayList.
C. The method calls 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.
MCQ 3
What does this method do?
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;
}
Identify the pattern before reading the options.
A Returns true if the array contains no duplicates.
B Returns true if the array is sorted in descending order.
C Returns true if all elements are positive.
D Returns true if the array is sorted in non-decreasing order.
D. The method returns false the moment any element is greater than the next — meaning a descent was found. If no descent is found across the whole array, it returns true. That means every element is <= the next — non-decreasing (ascending) order. Note it allows equal adjacent elements (non-decreasing, not strictly increasing).
MCQ 4
Which bug does this method contain?
public static double average(ArrayList list) {
    int total = 0;
    for (int x : list) {
        total += x;
    }
    return total / list.size();
}
A Integer division truncates the decimal — should cast to double before dividing.
B The for-each loop cannot be used here.
C list.size() should be list.length.
D total should be declared as a double.
A. Both 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.
MCQ 5
What is returned by 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;
}
Identify which elements are accessed before computing.
A 6
B 9
C 12
D 21
C. The method accesses the last element of each row: grid[0][1]=2, grid[1][1]=4, grid[2][1]=6. Sum = 2+4+6 = 12. This sums the last column of the 2D array.
Tier 3 · AP Mastery

Mastery: Informal Code Analysis

MCQ 6
A Student class has getName() and getScore(). What bug does this method contain?
public static String topStudent(ArrayList roster) {
    int maxScore = 0;
    String topName = "";
    for (Student s : roster) {
        if (s.getScore() > maxScore) {
            maxScore = s.getScore();
            topName = s.getName();
        }
    }
    return topName;
}
Think about what happens if all scores are negative.
A topName should be initialized to roster.get(0).getName(), not "".
B maxScore should be initialized to roster.get(0).getScore(), not 0.
C The for-each loop should be a standard for loop.
D There is no bug — this works correctly for all inputs.
B. If all scores are negative, no score will ever be greater than 0, so 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.
MCQ 7
What does this method return for a 3×3 array where all values are 1?
public static int mystery(int[][] grid) {
    int total = 0;
    for (int i = 0; i < grid.length; i++) {
        total += grid[i][i];
    }
    return total;
}
Identify the access pattern, then compute.
A 9
B 6
C 1
D 3
D. This sums the main diagonal: grid[0][0] + grid[1][1] + grid[2][2] = 1+1+1 = 3. A (9) would be all elements. B (6) might be a row or column. The diagonal has exactly 3 elements in a 3×3 grid.
MCQ 8
What is returned by mystery(6)?
public static String mystery(int n) {
    if (n == 0) return "";
    return mystery(n / 2) + (n % 2);
}
Trace the recursive calls — what pattern do you recognize?
A "110"
B "011"
C "6"
D "3"
A. This converts a decimal number to binary. Trace: mystery(6) = mystery(3) + (6%2=0). mystery(3) = mystery(1) + (3%2=1). mystery(1) = mystery(0) + (1%2=1). mystery(0) = "". Unwind: "" + "1" = "1", "1" + "1" = "11", "11" + "0" = "110". Binary of 6 is 110. ✓
MCQ 9
Under what condition does this method NOT work correctly?
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;
}
A When the array is empty
B When the target is at the last index
C When the array is not sorted
D When the array contains negative numbers
C. This is binary search — it requires the array to be sorted. On an unsorted array, it may eliminate the half containing the target and return false incorrectly. A is handled (low > high immediately, returns false). B works — binary search finds elements at any valid index. D is fine — binary search handles negatives correctly.
MCQ 10
A Book class has getGenre() (String) and getPages() (int). Which correctly describes what this method returns?
public static int mystery(ArrayList books, String genre) {
    int total = 0;
    for (Book b : books) {
        if (b.getGenre().equals(genre)) {
            total += b.getPages();
        }
    }
    return total;
}
Read all layers before selecting.
A The number of books in the specified genre
B The total page count of all books in the specified genre
C The average pages per book in the specified genre
D The title of the longest book in the specified genre
B. The method accumulates 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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]