Outside the sheet
The AP Computer Science Principles Exam Reference Sheet describes the exam's pseudocode in two pages. It is a sheet, not a language definition, and it leaves many questions open. This tool has to answer every one of them to run a program. This page lists each answer, so that no one presents a choice of ours as an exam fact.
When a step of a running program depends on one of these choices, the trace marks that step with a small "Outside the sheet" note and one sentence. Those sentences are listed at the end of this page.
The sheet itself is on College Board's site: AP CSP Exam Reference Sheet (PDF). Where the sheet does decide something, this tool follows it: lists start at 1, assignment copies, DISPLAY puts one space after each value, REPEAT UNTIL checks before every pass, RANDOM(a, b) includes both ends, and MOD has the same precedence as * and /.
Numbers
- Division and decimals
- Numbers are exact fractions.
17 / 5is exactly 3.4, and0.1 + 0.2 = 0.3is true. A value that does not end as a decimal, like 1/3, is shown rounded to six places with a "rounded for display" note; comparisons use the exact value. - MOD with a negative number or a decimal
- The sheet defines
MODfor whole numbers, 0 or more on the left and above 0 on the right. Outside that, the result takes the sign of the right side:-1 MOD 5is 4. A right side of 0 is an error. - Very large numbers
- Numbers have no fixed size. A number past 1,000 digits stops the program with an error, so a runaway loop cannot freeze the page. The Settings panel has a fixed-size switch (8, 16 or 32 bits) for the overflow lesson in topic 2.1.
Expressions
- Order of NOT, AND and OR
- Comparisons first, then
NOT, thenAND, thenOR. SoNOT x < 5meansNOT (x < 5). A chain likea < b < cis a syntax error with a message. - Does AND or OR stop early?
- Yes, left to right. When the left side already decides the answer, the right side is not run, and the trace says so. This keeps
IF (i ≤ LENGTH(list) AND list[i] = x)safe. - Comparing different kinds of value
5 = "5"is false and5 ≠ "5"is true, never an error. The same for a list against a number or text.- Comparing text and lists
=and≠compare text exactly and lists item by item.<and>on two pieces of text compare character by character, as a dictionary does, with capital letters first.- Text operations
- The sheet has none. Exam questions define their own procedures for joining and cutting text, so this tool offers
concat,substringandstrLengthas given procedures, with the usual meaning and positions counted from 1."a" + 1is an error, andtext[i]is an error that points tosubstring. A program may define its own procedure with one of these names, and its own wins.
Loops and lists
- REPEAT n TIMES with an odd n
nis read once, before the first pass, and must be a whole number 0 or more. Changing the variable inside the loop does not change how many passes run.- FOR EACH when the loop changes the list
- The loop walks the list as it was when the loop began, and the trace notes that the list changed. The item is a copy: changing it does not change the list.
- Lists inside lists
- Copying is deep. Assigning or appending a list copies every level.
- INSERT at LENGTH + 1
- An error, by the sheet's own index rule: an index runs from 1 to
LENGTH. Some teachers expect it to append;APPENDis the way to add at the end.
Procedures
- A list passed to a procedure
- Copied, by the sheet's own wording: each argument is "assigned to" its parameter, and assignment copies. So a procedure that changes its list parameter changes nothing the caller can see. This is the one choice on this page that could disagree with an exam answer, so it is a switch in Settings: "shared" makes the procedure change the caller's list, as most programming languages do.
- Main-program variables inside a procedure
- A name assigned anywhere in the main program (outside every PROCEDURE) is a main-program variable everywhere: a procedure reads and changes that one variable. Every parameter, and any other name a procedure assigns, belongs to that one call. A parameter with the same name as a main-program variable hides it inside the call. The trace marks a step where a procedure changes a main-program variable.
- Recursion
- Allowed. Past 200 calls inside calls the program stops with a message.
- A procedure defined below the line that calls it
- Allowed. Every PROCEDURE is known before the first line runs.
- A procedure the program never defines
- The run pauses and asks what it returns, or keeps its answers in a table, so a question that says "isPrime returns true when..." can be traced without writing isPrime.
- RETURN with nothing after it, or outside a procedure
- A syntax error with a message.
Input and output
- What INPUT returns
- Text that reads as a number becomes a number: digits, with an optional decimal part and an optional minus sign in front. Anything else stays text, including
.5,5.,+5and1,000. - How DISPLAY writes things
- Numbers without trailing zeros, text without quotes,
trueandfalse, lists as[1, 2, 3]. One space after each value, as the sheet says.
Writing the code
- ELSE IF
- Accepted as shorthand for
ELSE { IF ... }and drawn that way in block form. - Comments
//to the end of the line. Shown in gray, left out of block form.- Typing the symbols
<-becomes←,!=becomes≠,>=and<=become≥and≤. Both spellings run. Keywords are uppercase,trueandfalselowercase, and names are case-sensitive. Text pasted from a PDF is cleaned of curly quotes and other look-alike characters first.- The robot on the goal square
- Nothing happens on its own: the goal is a marker. The given procedure
GOAL_REACHED()is true when the robot stands on it.
The notes the trace shows
Each of these appears on a step only when that step depends on the choice.