3.1 Variables and Assignment 1 Hacks

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

  1. numApples, integer
  2. apples, text
  3. numApples, string
  4. isApples, boolean

☆ My answer: #1 because "numApples" is a good variable name to store the # of apples because it's easy to read and understand. Also, the data type has to be integer because the variable stores an integer.

Click for the answer! 1. Because the name is descriptive of what is stored. Also, the number of apples in the store can change so the integer lets you add a nd subtract to the number of apples.

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?

  1. weather, integer
  2. weather, boolean
  3. isCold, boolean
  4. cold, string

☆ My answer: #3 is the best answer because isCold clearly represents what the variable is storing and the name is appropriate. Also, the variable must be a boolean because it stores true or false depending on if the weather is cold or not.

Click for the answer! 3. Because this is a true or false question. It is either cold or not, so the variable name describes the circumstance, then the boolean data type specifies true or false.

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

  1. IDnumber, string
  2. whatID, integer
  3. IDnumberofthestudentsintheclassroom, boolean
  4. IDnumberofthestudentsintheclassroom, integer

☆ My answer: #1 would be the best option because the name isn't too long like in #3 and #4 and a string makes more sense than an integer in this case. This is because we want to store a string of a numbers and not a large number.

Click for the answer! 1. IDnumber because it is descriptive but not too long and innefective. it is also a string because it doesn't change and its a string of numbers.

Is itisRainingtodayinsandiego a better option than isRaining?

  1. Yes
  2. No

☆ My answer: isRaining is a better name because it isn't too descriptive.

Click for the answer! 2. No, because it is too long and inefficient to use when trying to store variables.

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

  1. Boolean
  2. String
  3. Float
  4. All of the above

☆ My answer: #1 because a boolean allows you to store true or false depending on if the user selected true or false and depending on which answer is correct.

Click for the answer! 1. A boolean data type allows a variable to store true and false.

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

  1. An integer is just a set data type while a string of numbers can be changed with addition and subtraction
  2. An integer can be letters and numbers while a string is just numbers
  3. An integer is just numbers while a string is just words
  4. An integer can be changed with addition and subtraction and a string is a set number or string of letters.

☆ My answer: #4 I thinks this is the best option because an integer is data stores as a number that can manipulated through something like addition or subtraction while a string of numbers stores numbers as a string and can't be manipulated in the same way.

Click for the answer! 4. This is the only answer that has two true statements

3 Practice Questions:

Q: If I wanted to store whether I have eaten an apple today in a variable, what is a good variable name I can use and what data type should I use?

A: eatenApple, boolean

Q: What would be the best variable name and data type to store someone's age for a video game?

A: playerAge, integer

Q: What would be the best variable name and data type to store the user's favorite word?

A: favoriteWord, string

3.1 Variables and Assignments 2 Hacks

Hacks for the lesson

Answer the practice problems and blog about which one's you got wrong. After, create at least 6 problems that are alike to what you learned in this portion of the lesson.


Consider the following code segment:

  • currentScore ⟵ 10
  • highScore ⟵ currentscore
  • currentScore ⟵ 7

DISPLAY (currentScore)

What will the currentScore be after running this code segment?

  1. 17
  2. 10
  3. 7
  4. none of the above

☆ My answer: #3 because 7 is the last thing currentScore is set equal to.

Click for the answer! 3. because that is the latest value that was stored within the variable.

What will the highScore be?

  1. 17
  2. 10
  3. 7
  4. none of the above

☆ My answer: #2 because highScore is equal to currentScore when it was still equal to 10.

Click for the answer! 2. because the value stored in a variable will alwasy be the most recent value assigned.

Consider the following code segment:

  • num1 ⟵ 4
  • num2 ⟵ 6
  • num1 ⟵ num 2

DISPLAY(num1)

DISPLAY(num2)

What is displayed after running this code segment?

  1. 4 6
  2. 6 4
  3. 4 4
  4. 6 6

☆ My answer: #4 because at first num1 = 4 and num2 = 6. Next, num1 is set to the value of num 2 which is 6. This means that 6 will be displayed twice because num1 and num2 are both 6.

Click for the answer! 4. because the value for num 2 becomes the value of num 1 therefore making the first outcome 6 and the second outcome would be six as well because that is the value that num2 is assigned.

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?

  1. 25 15 30
  2. 30 30 25
  3. 30 15 30
  4. 15 30 25

☆ My answer: #2 because first, num2 is set to the value of num3 which is 30. Then, num3 is set to the value of num 1 which is 25. Lastly, since num2 is now 30 and num1 is set to the value of num2, num1 is set to 30. This means that displaying num1, num2, and num3 in this order will print 30 30 25.

Click for the answer! 2. because for num 1 the value is replaced by num 3's value which is 30. For num 2 the value is 30 because its replaced by num 1's value which was originally 25 but then was replaced by num 3's value 30. For num 3, the value is 25 because it's value is replaced by num 2 which num 2's value was replaced by num 1's.

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

  1. 10
  2. 20
  3. 30
  4. 40

☆ My answer: The answer is #2 which is 20 because first, p is set to the value of q which is 20. Next, q is set to the value of r which is 30. Next, s is set to the value of q which has changed to 30. Lastly, r is set to the value of p which is 20.

Click for the answer! 2. because q is the variable assigned to p and q's value is 20.

Consider the following code segment:

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

Insert Question

  1. The value of first is true, and the value of second is false.
  2. The value of first is false, and the value of second is true.
  3. The value of first is true, and the value of second is true.
  4. The value of first is false, and the value of second is false.

☆ My answer: They are both true and the answer is #3 because first, second is set to the value of first which is true. Next, first is set to the new value of second with is true. This means they are both true.

Click for the answer! 3. because the first two statements assign values to the variables. The third statement assigns the value of first (which is true) to second. The fourth statement assigns the value of second (which is true) to first.

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?

  1. 21 40 30 50
  2. 10 20 30 40
  3. 21 40 30 40
  4. 21 30 40 50

☆ My answer: None of these answers are correct because first a = 10, b = 20, c = 30, d = 40, and x = 20. Next, b is set to the sum of x and b which is 40. Next, a is set to the value of x + 1 which is 21. Lastly, d is set to the value of c/d + 2 which is 2 3/4. Since this isn't an answer, none of these answers are correct.

Click for the answer! 1. because the first five statements assign values to the variables. The sixth statement assigns the value of x + b (which is 40) to b. The seventh statement assigns the value of x + 1 (which is 21) to a. The eighth statement assigns the value of c + d / 2 to d. According to the order of operations, division has higher precedence than addition. Since c is 30 and d / 2 is 20, d is assigned the value 50. The last four statements display the values of a, b, c, and d.

Binary Hacks

Convert these binary notation to decimal notation. (the way we normally count)

  1. The binary number 111.

☆ My answer: 4 + 3 + 1 = 7


  1. The binary number 1011.

☆ My answer: 8 + 2 + 1 = 11


  1. The binary number 1101011.

☆ My answer: 1 + 2 + 8 + 32 + 64 = 107


Convert the decimal notation to binary notation. (You can use the Binary Math from Mr. Yeung or the one you have)

  1. 12

☆ My answer: 1100


  1. 44

☆ My answer: 101100


  1. 254

☆ My answer: 11111110


Extra Binary Hacks if you changed your bits to 24

Convert decimal notation to binary notation.

  1. 57345

☆ My answer: 1110000000000001


  1. 16777215

☆ My answer: 24 1s = 111111111111111111111111


  1. 11184810

☆ My answer: 12 pairs of "10" which is 101010101010101010101010


Convert the binary notation to decimal notation

  1. 101011101010

☆ My answer: 2 + 8 + 32 + 64 + 128 + 512 + 2048 = 2794


  1. 10011100000

☆ My answer: 32 + 64 + 128 + 1024 = 1248


  1. 1101001000101000

☆ My answer: 8 + 32 + 512 + 4096 + 16384 + 32768 = 53800


Homework/Hacks

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?:

☆ My answer: The answer is #4 because scores1 is set to the list that scores2 was set to.

  1. [89, 78, 92, 63, 95, 88]
  2. [89, 78, 92, 63, 95, 88, 92, 79, 97, 63]
  3. [92, 79, 97, 63, 89, 78, 92, 63, 95, 88]
  4. [92, 79, 97, 63]
Click for the answer! 4. Because the data is not being appended from scores2 into scores1.

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?

  1. ["Sam", "Ann"]
  2. ["Jamal", "Tamara"]
  3. ["Sam", "Ann", "Jamal", "Tamara"]
  4. ["Jamal", "Tamara", "Sam", "Ann"]

☆ My answer: The answer is #1 because first listB is set to what listA was. Next listA is set to what the new listB contains which is the list containing Sam and Ann.

Click for the answer! 1. Because all of the data from listA "Sam" and "Ann" replace what is in listB. So if listB replaces what is in listA, "Sam" and "Ann" will still be the only data in that list.

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

  1. 5
  2. 7
  3. 6
  4. 4

☆ My answer: The answer is #3 and the length is 6 because there are 6 items in the list.

Click for the answer! 3. The length of the list is 6.

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

  1. 7
  2. 0
  3. 6
  4. 5

☆ My answer: Since the first item of the list starts at an index of 0, "Purple" is associated with the idex number 5. This means the answer is #4.

Click for the answer! 4. The index count starts at 0, making "Red" 0 and "Purple" index 5.

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

  1. Boolean
  2. String
  3. Float
  4. All of the above

☆ My answer: The answer is #4 because any data type can be stored in a list.

Click for the answer! 4. Any type of data can be stored within a list. A list can contain a mix of types of data.

Which of the following variables is a float?

  1. Apples
  2. -106.2
  3. 34
  4. True

☆ My answer: #2 is a float because it contains decimals.

Click for the answer! 2. A float is a decimal number.

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

  1. 21
  2. 17
  3. 16
  4. 69

☆ My answer: #3 because the index number is 1 less than the item number (because index starts at 0).

Click for the answer! 3. An index count starts at 0 so the index number of a variable is one less than it's spot in the length.

A variable is permanent and cannot be changed later on.

  1. True
  2. False

☆ My answer: The answer is #2 and false because variables can always be reassigned to another value.

Click for the answer! 2. False. Variables can be changed later on in the code.

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

☆ My answer: #6 because only statements 1 and 4 are true. The list contains floats and strings, and the index of "Avocado" is 4.

  1. The list has floats and string variable types.
  2. The ratio of float variables to string variables is 2:1.
  3. The length is 9.
  4. The index of "Avocado" is 4.
  5. All of the above
  6. 1 and 4
  7. 1, 3, and 4
Click for the answer! 6. The list has floats or decimals and strings, and the length is 10, the ratio of string to floats is 1:1, and the index number of "Avocado" is 4.