Back

Question Set 4

Six questions · 10 minutes · cumulative through 1.20

divider

What This Is

Six exam-style questions, weighted toward lists. Two of them are about places where Lua and the reference sheet do different things, so read the title on each code block before you answer.

Set a timer for 10 minutes. On paper, no computer, and mark each answer sure or unsure.


The Questions


Question 1

Given this table, what are pets[1] and #pets?

Lua
local pets = {"cat", "dog", "fish", "bird"}
  • (A) "dog" and 4
  • (B) "cat" and 4
  • (C) "cat" and 3
  • (D) "dog" and 3

Question 2

In Lua, pets[9] on that same four-element table gives:

  • (A) An error, and the program stops
  • (B) nil, and the program carries on
  • (C) The last element
  • (D) An empty string

Question 3

What happens here?

Exam notation
aList ← [4, 8, 15]
DISPLAY(aList[7])
  • (A) It displays nothing and carries on
  • (B) It displays 15
  • (C) An error message is produced and the program terminates
  • (D) It displays 0

Question 4

What does this print?

Lua
local orig = {1, 2, 3}
local other = orig
other[2] = 50
print(orig[2])
  • (A) 2
  • (B) 50
  • (C) nil
  • (D) An error

Question 5

What does this print?

Lua
local pets = {"cat", "dog", "fish", "bird"}
for i, p in ipairs(pets) do
if i % 2 == 1 then
print(p)
end
end
  • (A) cat and fish
  • (B) dog and bird
  • (C) All four
  • (D) cat only

Question 6 — select all that apply

xs has 5 values and ys has 3. A loop walks xs and draws a circle at xs[i], ys[i]. Which are true?

  • (A) The program stops with an error about a nil value
  • (B) The error happens on the fourth pass of the loop
  • (C) Lua warns you that the tables are different lengths
  • (D) Swapping the two tables would draw three circles and not stop

Submit

Six answers with sure or unsure beside each, and your time. For question 6, list every letter you chose.

Questions 2 and 3 are the same situation in two notations. If you gave them the same answer, one of them is wrong.

Worth 4 points, graded on completion.

Start the Timer