Five problems on traversal — going through a list one element at a time. This is the single most common shape in the multiple-choice section, and the exam gives you two ways to do it that look interchangeable and are not.
Do this on paper. There is nothing to run and nothing to type. On exam day you get this notation on a printed sheet and no computer, so practicing it any other way practices the wrong thing.
Everything you need for this page. This is the same notation the College Board uses on the exam.
a ← expression assign a copy of the result to aDISPLAY(expression) show the value, FOLLOWED BY A SPACE
IF(condition) no ELSE IF exists - nest an IF inside an ELSE{ ... }ELSE{ ... }
REPEAT n TIMES no counter variable is provided{ ... }REPEAT UNTIL(condition) checked BEFORE each pass; can run zero times{ ... }
aList ← [v1, v2, v3] FIRST ELEMENT IS aList[1]aList ← bList assigns a COPY of bListLENGTH(aList) number of elementsAPPEND(aList, value) add to the end, length + 1INSERT(aList, i, value) shift right from i, place value at iREMOVE(aList, i) delete index i, shift left, length - 1FOR EACH item IN aList item takes each VALUE, first to last{ ... } THERE IS NO INDEX. If you need one, you make it.
An index below 1 or above LENGTH ends the program with an error.FOR EACH gives you the value and nothing else. No position, no index, no way to look at the element before this one. When a question needs any of that, it uses REPEAT with a counter instead — and now you know why.
What does this display, and on how many lines?
scores ← [7, 3, 9]
FOR EACH s IN scores{ DISPLAY(s)}Same list walked the other way. Track total and i in two columns. What does it display?
values ← [4, 8, 15]total ← 0i ← 1
REPEAT LENGTH(values) TIMES{ total ← total + values[i] i ← i + 1}DISPLAY(total)Then answer this: i is set to 1 on its own line above the loop, and increased on the last line inside it. Say what would happen if the increase were missing, and say why the exam makes you write those two lines when Lua's numeric for writes them for you.
What does this display?
items ← [5, 10, 15, 20]count ← 0
FOR EACH v IN items{ IF(v > 8) { count ← count + 1 }}DISPLAY(count)A decision inside a traversal, counting how many elements pass a test, is the most-reused shape on the exam. It is worth being able to read it in one pass.
Rewrite this Lua loop in exam notation.
local temps = {60, 75, 90}
for i, t in ipairs(temps) do print(t)endThe Lua version declares two names and uses one. Say which one vanishes in the translation, and what you would have to do differently if the body had used it.
There is no exam-notation version of this one, and that is the point. Write down exactly what it prints.
local letters = {"a", nil, "c"}
for i, v in ipairs(letters) do print(v)endprint(#letters)Both answers surprise people. Say what ipairs does when it meets the gap, and what #letters reports.
Your five answers, plus the short explanations from problems 2, 4 and 5. For the traces, write the output exactly as it appears, including spacing.
Worth 3 points, graded on completion. Show your working for the traces — a wrong answer with visible reasoning is worth more to both of us than a right one with none.