3.1 Part 1 Hacks (copied from site)

Go through the following examples and with your table discuss the best variable name and the data type for each situation and stored variable:

  1. You want to store the best price for apples: Use a variable called priceApples which will be an integer type (so that the price could be added to/subtracted from)
  2. You are storing true or false in a variable that asks if the classroom is cold: use a boolean variable called isCold
  3. How do you store the ID numbers for the students in the classroom: use a list variable called IDList and store the elements as strings. Here’s an example:
IDList = ["1324978", "1235478", "1324890", "1634890"]

3.1 Part 2 Hacks (copied from site)

Please note that some of this is redundant with the previous section.

1) You want to store the number of apples in a shop. What is the best variable name and data type?

Answer: (1) numApples, integer

This name is fairly descriptive and also allows for addition/subtraction operations in case apples are purchased or imported into the store (therefore changing the number of apples)

2) You are storing true or false in a variable that asks if the classroom is cold. What is the best variable name and data type?

Answer: (3) isCold, boolean

A true or false value describes the boolean type variable. In addition, the variable name is descriptive of what is being measured (namely if a classroom is cold)

3) How do you store the ID numbers for the students in the classroom? Choose the best variable name and data type:

Answer: (1) IDnumber, string

the name IDnumber is concise and is descriptive of what value is being stored into the variable. Since math is not performed on ID numbers, a string variable is appropriate. Note that this variable is appropriate to store the ID numbers of single students only. If multiple ID numbers need to be stored, a list should be created with string elements of each ID number.

4) Is itisRainingtodayinsandiego a better option than isRaining?

Answer: (1) No

The variable itisRainingtodayinsandiego is too long and affects readability of the program. isRaining is far more concise and is almost just as descriptive as the former variable name. If there must be boolean variables for the weather of multiple cities (for example, for a weather program), a more concise form of this variable could be isRainingSD, though at some point a dictionary or array could be used to match cities to their corresponding weather conditions.

5) Which of the following types of data is best for a true or false question?

Answer: (1) Boolean

A boolean variable contains either a true value or a false value.

6) What is the difference between an integer and string of numbers?

Answer: (4) An integer can be changed with addition and subtraction and a string is a set number or string of letters

The answer is relatively self-explanatory, though it’s worth it to note that strings can also be made of combinations of numbers and letters (or even special characters)

Practice Scenario 1

Suppose that a variable should be created which notes the type of device a student brings to a class. This variable should be a string variable called deviceType. Here’s an example:

deviceType = Desktop

If device type for multiple students need to be noted, a list should be used instead (using string elements)

Practice Scenario 2

Suppose that a variable representing a student’s class grade is required. Keep in mind that a student’s grade is made up of averaged scores from many assignments and possibly grade categories. In this case, a float/integer variable called Grade should be used. If the grades of multiple students need to be recorded, a dictionary/array may be required with the student names as string elements and the grades as float/integer elements.

Practice Scenario 3

Review scenario 2. Take scenario 2 with the additional information of trying to evaluate if each student is passing the class. For an individual student, this would probably be a boolean variable called isPassing. In order to determine the value of the variable, an if statement will likely be used. In the case of using this variable for multiple students in the class, there will likely be boolean elements associated with each student and his/her grade in a dictionary/array, and a loop will likely be used.

3.1 Part 3 Hacks

1) Consider the following code segment:

currentScore ⟵ 10
highScore ⟵ currentscore
currentScore ⟵ 7
DISPLAY(currentScore)

What will the currentScore and highScore be after running this code segment?

Answer: (3) currentScore will have a value of 7 after running the code segment, as the most recent line of code set the value of currentScore equal to 7

Answer: (2) highScore will have a value of 10 because after the value of 10 was assigned to the variable currentScore, the value of currentScore was assigned to highScore.

2) Consider the following code segment:

num1 ⟵ 4
num2 ⟵ 6
num1 ⟵ num 2
DISPLAY(num1)
DISPLAY(num2)

What is displayed after running this code segment?

Answer: (4) 6 6 is displayed after running the code segment. After the value 4 was assigned to num1 and value 6 was assigned to num2, the value of num2 was assigned to num1. This makes both variable have a value of 6. Therefore, 6 6 is displayed.

3) Consider the following code segment:

num1 ⟵ 25
num2 ⟵ 15
num3 ⟵ 30
num2 ⟵ num3
num3 ⟵ num1
num1 ⟵ num2
DISPLAY(num1)
DISPLAY(num2)
DISPLAY(num3)

What is displayed after running this code segment?

Answer: (2). After the corresponding values are assigned to the three number variables in the first three lines of code, the value of num3 is assigned to num2, making num2 have value 30. num1 is assigned to num3, making it have value 25. Then, num2 is assigned to num1, making num1 have value 30. Here are the new values:

num1⟵30
num2⟵30
num3⟵25

With this information, the display commands will run and display num1, then num2, then num3. Therefore, 30 30 25 will be displayed.

4) Consider the following code segment:

p ⟵ 10
q ⟵ 20
r ⟵ 30
s ⟵ 40
p ⟵ q
q ⟵ r
s ⟵ q
r ⟵ p

What is the value of r as a result of running this code segment

Answer: (2) It is important to note that r is assigned the value of p at Line 8. p was assigned the original value of q at Line 5. Therefore, the value of r is 20.

5) Consider the following code segment:

first ⟵ true
second ⟵ false
second ⟵ first
first ⟵ second

Insert Question

Answer: (3). The first two lines assigns true to first and false to second. The third line assigns the value of first to second, making both variables have the value true. The fourth line assigns second to first, which does nothing. Therefore, both variables have value true.

6) Consider the following code segment:

a ⟵ 10
b ⟵ 20
c ⟵ 30
d ⟵ 40
x ⟵ 20
b ⟵ x + b
a ⟵ x + 1
d ⟵ c / d + 2
DISPLAY(a)
DISPLAY(b)
DISPLAY(c)
DISPLAY(d)

What is displayed as a result of executing the code segment?

Answer: (). The first five lines assign values to the variables a, b, c, d, and x. Line 6 assigns the sum of x and b (which is 40) to b. Line 7 assigns the sum of x and 1 (which is 21) to a. Line 8 assigns the quotient of c and d plus 2 (which is technically (30/40) + 2 or 2.75) to d.

At this point I realized this question might be miswritten because the answers were printing values of 40 or 50 for d

Anyways, the actual display as far as I’m concerned should be 21 40 30 2.75 (unless I’m getting something wrong with the decimal display)

Now according to the answer key, line 8 should actually be written as c + d / 2. This would make the value of d equal to c plus half of d (30 + 20 = 50). This would make the value of d 50 and answer (1)

3.2 Hacks

Please see the Jupyter Notebook upload to see my interactions.

Binary hacks
  1. Binary 111 = (1 + 2 + 4) = decimal 7
  2. Binary 1011 = (1 + 2 + 0 + 8) = Decimal 11
  3. Binary 1101011 = (1 + 2 + 0 + 8 + 0 + 32 + 64) = Decimal 107
  4. Decimal 12 = (8 + 4 + 0 + 0) = Binary 1100
  5. Decimal 44 = (32 + 0 + 8 + 4 + 0 + 0) = 0010 1100
  6. Decimal 254 = (128 + 64 + 32 + 16 + 16 + 8 + 4 + 2 + 0) = 1111 1110
24-bit binary hacks

Decimal to Binary: At this point I’d need a program

  1. 57345 decimal =
  2. 16777215 decimal =
  3. 11184810 decimal =

Binary to Decimal

  1. Binary 101011101010 = (0 + 2 + 0 + 8 + 0 + 32 + 64 + 128 + 0 + 512 + 0 + 2048) = Decimal 2794
  2. Binary 10011100000 = (0 + 0 + 0 + 0 + 0 + 32 + 64 + 128 + 0 + 0 + 1024) = Decimal 1248
  3. Binary 1101001000101000 = (0 + 0 + 0 + 8 + 0 + 32 + 0 + 0 + 0 + 512 + 0 + 0 + 4096 + 0 + 16384 + 32768) = 53800

Final Hacks

DISCLAIMER

I have some deviations from the provided answers for a few of these questions. Please do not blindly subtract points for these differences and read my reasoning. Thank you!

1) Consider the following code segment:

scores1 ← [89, 78, 92, 63, 95, 88]
scores2 ← [92, 79, 97, 63]
scores1 ← scores2

What are the contents of scores1 after the code segment is executed?

Answer: (4). Lines 1 and 2 set the values of the lists scores1 and scores2. Line 3 overrides the value of scores1 with that of scores2. Therefore, the list of scores1 is that of scores2, namely [92, 79, 97, 63]

2) Consider the following code segment:

listA <- ["Sam", "Ann"]
listB <- ["Jamal", "Tamara"]
listB <- listA
listA <- listB

What are the contents of listA after the code segment is executed?

Answer: (1). In a similar manner to Question 1, Lines 1 and 2 define listA and listB. In Line 3, ListA overrides ListB, making them both have the contents [“Sam”, “Ann”]. Therefore, Line 4 (where ListB overrides ListA) does nothing since the lists have the same contents. Therefore, the contents of ListA are [“Sam”, “Ann”]

3) What is the length of this list? ["Red", "Orange", "Yellow", "Green", "Blue", "Purple"]

Answer: (3) The length of this list is 6 because there are 6 elements (which are apparently basic colors). Each element in the list can be distinguished because they are separated by commas. The elements are put in quotation marks, indicating that they are string elements.

4) What is the index number of “Purple” in this list? ["Red", "Orange", "Yellow", "Green", "Blue", "Purple"]

Answer: If CollegeBoard pseudocode is being followed (see page 3 of the pseudocode guide), the first element in a list has an index of 1. This is implied by the fact that there will be errors if an index less than 1 is used on a list operation. Therefore, the 6th element (which is “purple”) has an index of 6 and the answer is (3). Keep in mind that this may vary based on which programming language is used.

Now of course a well known example of this is the Python language, where the first element of a list has an index of 0. As such, the 6th element would have an index of 5 in Python and yield answer (4).

Now to prevent all this confusion, assign the list as a variable. Different languages have different syntaxes with defining lists, and these distinctions would make it more clear which rules to follow with indexing.

5) Which of the following types of data can be stored in a list?

This one was tricky. I made some improvised code in python here.

isOnline = bool(1==2)

aList = [1.1, 1.2, 1.3, 1.4, "hrar", "wow"]
print(aList)
aList.extend(isOnline)
print(aList)

I could not add a boolean variable to the list using extend commands, but having float data (1.1, 1.2) and strings printed just fine. Then I tried this:

isOnline = bool(1==2)
aList = [1.1, 1.2, 1.3, 1.4, "hrar", "wow", bool(1==2)]
print(aList)

Then the list printed just fine. Here’s output: [1.1, 1.2, 1.3, 1.4, 'hrar', 'wow', False]. This is probably an indication I didn’t use extend commands properly.

I’ll therefore answer with (4)

6) Which of the following variables is a float?

Answer: (2). -106.2 is not an integer, but it is still a number (especially since it has decimals). This falls under the classification of float variables. A few statements about the other variables:

Apples could be a string variable if apples is the value of the variable.

34 is an integer variable since it’s an integer.

True is the value of a Boolean variable, of which the other possible value is false. Boolean variables are the simplest variables and are binary.

7) If a list has a length of 24 items, what is the index number of the 17th item?

Answer: If Python is being used, the first element in a list has an index of 0. Therefore, the 17th element has an index of 16 and the answer is (3).

If CollegeBoard pseudocode is being followed, the first element in a list has an index of 1. Therefore, the 17th element has an index of 17 and the answer is (2)

8) A variable is permanent and cannot be changed later on.

Answer: (2). The values of existing variables can be changed in multiple ways. I’ll first mention that integer/float variables can have mathematical operations (ex: addition, subtraction, multiplication, and division) performed on them. These operations often change their values, and are crucial for the ability of programs to dynamically change numbers displayed (ex: displayed grades can be changed). Second, it is possible for an existing variable to be overwritten by another value. This often happens when one variable is copied onto another existing variable.

9) Which of the following is true about the list? ["Apples", 42.0, "Bananas", 0.5, "Avocado", -902.2, "Lychee", 6.9, "Orange", 7.2]

I’ll consider each of the four options one-by-one then come up with a final answer

i) There are strings in this list (assorted fruit) and some float elements ii) There are 5 string elements and 5 float elements. The ratio is 1:1. iii) There are 10 elements in the list, so the length is 10. iv) This one depends, and I’m not going to go over indexing differences between Python and pseudocode again since I already explained that twice. Here’s what I’ll say:

If Python is being used (which is probably what the makers of the question envisioned), “Avocado”, as the 5th element, has an index of 4. Therefore, the final answer is (6)

If CollegeBoard pseudocode is being used, “Avocado” has an index of 5. Therefore, the final answer is (1).