Four problems on lists. One is a translation, and two of them are the two places Lua tables and exam lists genuinely disagree — the ones from session 19. Both are on the Quarter 1 exam.
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{ ... }
An index below 1 or above LENGTH ends the program with an error.The good news first. Exam lists start at [1] and so do Lua tables. That agreement is worth more than it sounds, because most languages do not do this and the students who took a different course have to unlearn it.
Rewrite this Lua program in exam notation.
local names = {"Avery", "Blake", "Casey"}
print(names[1])print(#names)Two of these three lines change more than the punctuation. The square brackets do not.
Write the whole list out after every line. What do the last two lines display?
aList ← [10, 20, 30]APPEND(aList, 40)INSERT(aList, 2, 15)REMOVE(aList, 1)DISPLAY(LENGTH(aList))DISPLAY(aList[1])INSERT and REMOVE both move everything after them. If you are not writing the list out each time, you are guessing.
What does the exam-notation version display?
first ← [1, 2, 3]second ← firstsecond[1] ← 99
DISPLAY(first[1])DISPLAY(second[1])And what does the Lua version print?
local first = {1, 2, 3}local second = firstsecond[1] = 99
print(first[1])print(second[1])These do not have the same answer. Explain the difference in one sentence, using the words copy and alias.
Both of these ask a two-element list for its third element. Write down everything each one puts on the screen, in order, and stop where it stops.
scores ← [4, 8]DISPLAY("start")DISPLAY(scores[3])DISPLAY("end")local scores = {4, 8}print("start")print(scores[3])print("end")The word end is the whole question. One of these reaches it and one does not.
Your four answers, including both halves of problems 3 and 4, and the three one-sentence explanations. 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.