AP Computer Science Principles Exam Reference Sheet Study Guide
AP Computer Science Principles Exam Reference Sheet Overview The AP Computer Science Principles Exam Reference Sheet, dated July 2015, is a guiding document designed to assist students in understanding the format and logic of the questions they will encounter on the exam. Since the AP Computer Science Principles course does not mandate the use of a specific programming language, this reference sheet serves as a universal standard, providing instructions and explanations for two primary programming formats: text-based and block-based. These instructions utilize four fundamental data types: numbers, Booleans, strings, and lists. The categories of instructions covered by the sheet include Assignment, Display, and Input; Arithmetic Operators and Numeric Procedures; Relational and Boolean Operators; Selection; Iteration; List Operations; Procedures; and Robot instructions. # Assignment, Display, and Input The reference sheet details three primary operations for handling data flow within a program. The first is assignment, denoted in text as a←expression. This operation evaluates the given expression and assigns the resulting value to the variable a. The second operation is display, written as DISPLAY (expression). This command outputs the value of the expression, and notably, it is followed by a space. The third operation is input, written as INPUT (), which accepts a value provided by the user and returns that value for use within the program. # Arithmetic Operators and Numeric Procedures Mathematical operations on the exam include standard arithmetic and specific numeric procedures. The basic operators for addition, subtraction, multiplication, and division are represented in both text and block formats as a+b, a−b, a×b, and a/b. For example, the division operation 3/2 evaluates to the numeric value 1.5. Another vital operator is the modulo operator, represented as a MOD b. This operation evaluates to the remainder when the integer a is divided by the integer b. The reference sheet assumes that both a and b are positive integers; for instance, the expression 17 MOD 5 evaluates to 2. Additionally, the sheet defines the procedure RANDOM (a, b), which evaluates to a random integer starting from a and going up to and including b. For example, the execution of RANDOM (1, 3) results in either 1, 2, or 3. # Relational and Boolean Operators Relational operators are utilized to test relationships between variables, expressions, or values. These include equality represented as a=b (which evaluates to true if the values are equal and false otherwise), inequality represented as a=b, greater than represented as a>b, less than represented as a<b, greater than or equal to represented as a≥b, and less than or equal to represented as a≤b. Boolean logic is handled through three operators: NOT condition evaluates to true if the condition is false and false if the condition is true. The condition1 AND condition2 operator evaluates to true only if both conditions are true; otherwise, it is false. Finally, condition1 OR condition2 evaluates to true if either condition1 is true, condition2 is true, or if both are true; it only evaluates to false if both individual conditions are false. # Selection and Iteration Selection instructions allow a program to branch based on Boolean conditions. The basic IF (condition) structure executes a block of statements only if the condition evaluates to true; if false, no action is taken. The IF (condition) ELSE structure provides two paths: the first block of statements is executed if the condition is true, while the second block of statements is executed if the condition is false. Iteration instructions control the repetitive execution of code. The REPEAT n TIMES structure evaluates the block of statements exactly n times. The REPEAT UNTIL (condition) structure repeats a block of statements continuously until the specified Boolean condition evaluates to true. # List Operations Lists are structured data sets where indices represent positioning. A critical rule for all list operations is that if a list index is less than 1 or greater than the actual length of the list, an error message is produced and the program terminates immediately. The reference sheet specifies that the first element of a list is located at index 1. Specific operations include list[i], which refers to the element at index i, and list[i]←list[j], which assigns the value of the element at index j to the position at index i. A list can be initialized as list←[value1, value2, value3], assigning the values to symbols at indices 1, 2, and 3 respectively. The FOR EACH item IN list loop assigns the variable item the value of each element in the list sequentially from the first to the last, executing the block of statements once for each assignment. Modification commands include INSERT (list, i, value), which shifts all values at indices greater than or equal to i to the right, increases the list length by 1, and places the new value at index i. The APPEND (list, value) command increases the list length by 1 and adds the value to the very end of the list. REMOVE (list, i) deletes the item at index i, shifts all subsequent values to the left, and decreases the list length by 1. The LENGTH (list) function returns the total number of elements currently in the list. # Procedures Procedures are defined blocks of code that can take zero or more parameters. The text syntax is PROCEDURE name (parameter1, parameter2, ...) followed by a block of instructions. Procedures may also return a value using the RETURN (expression) statement. This statement can appear at any point within the procedure; when executed, it causes an immediate return from the procedure back to the calling program, passing back the evaluated value of the expression. # Robot Instructions The Robot category defines how a simulated robot interacts with a grid. If the robot attempts to move to a square that is not open or is beyond the grid’s edge, the robot remains in its current location and the program terminates. The command \text{MOVE_FORWARD ()} moves the robot one square in its current direction. \text{ROTATE_LEFT ()} turns the robot 90∘ counterclockwise, while \text{ROTATE_RIGHT ()} turns it 90∘ clockwise. The \text{CAN_MOVE (direction)} function evaluates to true if there is an open square in the specified direction relative to the robot's current facing; otherwise, it returns false. Valid directions for this function are left, right, forward, or backward.