Unit 2: Abstraction

Lab 1: Games

2.1.1
Examples like this one are useful to test your understanding of assignment to variables, but you wouldn't use a sequence of assignments like this in an actual program.
What value will this code display?
a ← 3
b ← a
a ← 4
DISPLAY(b)
3
4
a
b
2.1.1
What value will this script report?
set m to 9, set k to 5, set m to (m + 1), set k to (k - m), report k
-5
3
-4
5
2.1.2
We really shouldn't be encouraging students to mutate a block's inputs. There are really two issues here; one is with the x=x+1 style of programming altogether, and the second is that formal parameters specifically shouldn't generally be mutated; it's better to make a script variable that's initialized to the input value. (If we change this, we need to update the Guided Notes.) --MF, 6/26/21 with BH
mystery function (input):
if (input < 5) (repeat until (input = 5)
(
    say (input) for (1) secs,
    set (input) to (input + 1)
)
report (finished) Which inputs to mystery function will report "finished"?
Any integer
Only integers greater than or equal to 5
Only integers less than 5
Only 5
2.1.2
mystery function (input):
if (input < 5) (repeat until (input = 5)
(
    say (input) for (1) secs,
    set (input) to (input + 1)
)
report (finished) What will the sprite say if you run the same function with the input 1?
speech bubble saying '4'
speech bubble saying '5'
four speech bubbles saying '1', '2', '3', and then '4'
five speech bubbles saying '1', '2', '3', '4', and then '5'
The feedback is not finished because I realized that BH might object to changing the value of an input variable... --MF, 3/1/19
2.1.2
mystery function (input):
if (input < 5) (repeat until (input = 5)
(
    say (input) for (1) secs,
    set (input) to (input + 1)
)
report (finished) What will happen if you run the same function with the input 9?
The sprite will say 9, and the block will report "finished."
The sprite will say 9, 8, 7, 6, and the block will report "finished."
The sprite will say 9, 10, 11, 12, 13, … and keep counting, and the block will never report "finished."
The sprite will say nothing, and the block will report "finished."
The feedback is not finished because I realized that BH might object to changing the value of an input variable... --MF, 3/1/19
2.1.5

AAP-2.N part b
Which of the following blocks will report a list of length 3?

Choose all that apply.

list (pineapple strawberry kiwi)
item 3 of {pineapple, strawberry, kiwi}
item 2 of {lemon, {pineapple, strawberry, kiwi}}
list () (pineapple) ()

Lab 2: Making Lists

2.2.2

Imagine you make a variable capitals and use set to give this list of lists a name:
set (capitals) to {{Augusta, Maine}, {Boise, Idaho}, {Columbia, South Carolina}, {Des Moines, Iowa}}
Which of the following statements are true?

Choose all that apply.

item (1) of (capitals) reports a list with two items: "Augusta" and "Maine".
item (1) of (item (1) of (capitals)) reports the word "Augusta".
item (last) of (item (2) of (capitals)) reports the word "Iowa".
(length) of (capitals) reports the number 8.
all but first of (item (last) of (capitals)) reports a list with one item: "Iowa".
item (3) of (capitals) reports the string "Boise".
item (last) of (all but first of (capitals)) reports the string "Iowa".
(length) of (item (1) of (capitals)) reports the number 2.
2.2.3

Which of the scripts below will produce the following sprite behavior?
sprite saying Abraham then Lincoln
(When you run the script, though, it actually says Abraham Lincoln only once.)

Choose all that apply.

for each (item) of {Abraham, Lincoln} [say (item) for (1) secs]
say {Abraham, Lincoln} for (2) secs
say (item (1) of {Abraham Lincoln, George Washington, Barack Obama}) for (2) secs
say (item (1) of {Abraham, George, Barack} for (1) secs, then say (item (1) of {Lincoln, Washington, Obama} for (1) secs
2.2.3
AAP-2.O part b
inputList ← [3, -1, 2, 10, -5]

FOR EACH item IN inputList
{
  IF(item > 0  AND  item * item > 4)
  {
    DISPLAY(item)
  }
}

What will be displayed as a result of running the code segment given?
9, 1, 4, 100, 25
9, 100
3, 2, 10
3, 10
2.2.3
PROCEDURE Mystery(numberList, targetNumber)
{
  counter ← 0
  FOR EACH number IN numberList
  {
    IF(number > targetNumber)
    {
      counter ← counter + 1
    }
  }
  RETURN(counter)
}
myMathGrades ← [100, 80, 90, 80, 60, 100, 50, 100]
Mystery(myMathGrades, 60)

What will be returned if the code above is run?
[100, 80, 90, 80, 100, 100]
7
6
60

Lab 3: Making Decisions

2.3.2
There's no built in ≤ block in Snap!. Suppose we wanted to build one. Which two of the following Boolean expressions are equivalent to the expression num less than or equal to 23 predicate?
(num < 23) and (num = 23)
(num < 23) or (num = 23)
not (num > 23)
not (num > 22)
2.3.2
Which of the following expressions will report true?
  1. (T and F) and (not (T and F))
  2. (not (T or F)) or (T or F)
I only
II only
I and II
Neither I nor II

Lab 4: Making Computers Do Math

2.4.1
AAP-2.L.2
Algorithms that look the same may have different results. The following incomplete code fragment was designed to test if
number
is odd:
IF (MISSING CONDITION)
{
  DISPLAY "It is odd."
}

Which of the following can be used in place of the MISSING CONDITION?
number MOD 1 = 0
number MOD 1 = 1
number MOD 2 = 0
number MOD 2 = 1
2.4.1
What's the value of 11 mod (2 + 3)?
1
0.2
2
2.2
2.4.3

When will "Error. Invalid entry." be displayed?

PROCEDURE greaterThanOrEqualTo(a, b)
    IF(a > b)
    {
        RETURN(true)
    }
    ELSE
    {
        RETURN(a = b)
    }
    DISPLAY("Error. Invalid entry.")
}
When
a > b
is true.
When
a > b
is false.
Always
Never