Section 1: Informal Practice

Topic 3.5

Here’s some vocab (copy-pasted from site)

Boolean: binary variable with only two possible values, “true” or “false”

1) What boolean value would result be?

a <-- 19
b <-- 28
result <-- a = b

Answer: 2. False

The values of a and b (19 and 28 respectively) are not equal. Therefore, the statement a = b should return boolean value false.

2) What boolean value would result be?

score <-- 99
average <-- 99
result <-- score ≤ average

Answer: 1. True

The values of the variables score and average are equivalent and both 99. This satisfies the requirement for the operator less than or equal to and will therefore render the boolean result true.

3) What boolean value would be displayed?

type <-- "dog"
training <-- "yes"
DISPLAY((NOT (type = "dog")) AND (training = "no"))

To check the boolean result displayed, each statement must be checked. Given that an AND operator is used, both statements must evaluate to true for the result to evaluate as true.

i) This NOT statement returns the opposite result that type = “dog” would usually output. Given that the string “dog” was indeed assigned to type, the usual output is true and the output after the NOT statement is false

Given the AND operator is used, there is no need to check the second statement. The final output will evaluate to false regardless of what the second statement’s result is.

Answer: 2. False

Note: The makers of this question believed that since training = “no” was a false statement, two false statements through an AND operator would yield true. Only two true statements will yield a final output of true.

Topic 3.6

Here’s some vocab (copy-pasted from site)

Algorithm: Finite set of instructions that accomplish a specific task

Now here’s some questions.

1) What value will be displayed?

rank <-- "titan"
hall <-- 14
IF (rank = "titan") { DISPLAY("You like Clash of Clans a lot.")
}

Answer: 1. You like Clash of Clans a lot.

The string “titan” was assigned to the variable rank. The if condition in line 3 checks if the variable rank is equal to the string “titan”. Since it is, the associated command (DISPLAY(“You like Clash of Clans a lot.”)) is executed. The DISPLAY command prints to output the string enclosed in the parenthesis.

2) What portion of the code will run?

class <-- 33
cookies <-- 25
IF (class < cookies) { DISPLAY("Give out cookies today.")
}
ELSE { DISPLAY("Do not give out cookies today.")
}

Answer: 2. The ELSE portion

The first two lines assign integers to the variables class and cookies. The IF statement has the condition that the value of class is less than that of cookies. Since the value of cookies is greater than that of class, the commands associated with the ELSE statement (rather than the IF statement) are executed.

Topic 3.7

Here’s some vocab (copy-pasted from site)

Nested Conditional: A conditional statement inside a conditional statement

Here’s some practice questions

1) Is this an example of a nested conditional?

space <-- 90
download <-- 20
IF (space > download) { DISPLAY("File can be downloaded.")
}

Answer: 2. No

There is only one conditional statement command (an IF statement) within this portion of code. The definition of a nested conditional, which has a conditional statement inside a conditional statement, requires at least two conditional statement commands. Therefore, it is impossible for this to be a nested conditional.

2) What does this code display?

price <-- 200
discount <-- 65
IF (price > 100) { IF (discount > 50) {
  DISPLAY("You can buy this item.")
} ELSE {
  DISPLAY("You cannot purchase this item.")
} }

Answer: 3. You can buy this item

The variables price and discount are assigned 200 and 65, respectively.

The first if statement checks if the value of price is greater than 100. Since 200 is greater than 100, it executes a nested conditional that checks if discount is greater than 50. Since it is, it executes the associated command to display “You can buy this item.”

Note that for prices of 100 or less, the ELSE statement will be executed since the first conditional statement will not be fulfilled. This is likely a logic error.

Section 2: Hacks

Please see this notebook