Unit 3: Algorithms and Programming
AP Computer Science Principles: 50 practice questions with detailed explanations.
Unit Study Guide
Executive Summary
Programs store values, make decisions, repeat, and call reusable procedures. Algorithms differ in how much work they need as input grows.
Variables and expressions
Variables store values that change: x = x + 1 updates x. Expressions combine operators; the mod operator (%) gives remainders (17 mod 5 = 2). Strings concatenate with +; substrings extract parts.
Booleans and conditionals
Comparisons (==, >, <) give true/false; combine with AND (both), OR (either), NOT (flip). if/else runs one branch; nested ifs test layered conditions.
Iteration
Loops repeat while a condition holds. Count carefully: a counter starting at 0 with count < 5 runs five times. Use a loop with an index to modify lists; enhanced for only reads.
Lists and algorithms
Lists hold ordered values, indexed from 0. Linear search checks each element; binary search halves a SORTED list (about log2(n) checks). Efficiency measures how work grows with input size — binary search beats linear search on large inputs.
Procedures, randomness, and the limits of computing
Procedures group reusable code with parameters; libraries bundle prewritten procedures. Random values power simulations. Undecidable problems — like the halting problem — have NO algorithm that solves all cases.
Exam traps
Mod returns remainders, not quotients. Loops with ≤ run one too many times. Binary search needs sorted data. Efficiency is about growth, not speed of one run.