3.1-3.10 AP-Style MC Practice & Quizzes
Using the table given below, determine the ASCII to binary code conversion for the word science. a)01110011 01100011 01101001 01100011 01100101 01101110 01100101 b)01110011 01100011 01101001 01100101 01101110 01100011 01100101 c)01110011 01100011 01101001 01101110 01100011 01100101 01100101 d)01110011 01100011 01101001 01100101 01101110 01100101 01100011
b)01110011 01100011 01101001 01100101 01101110 01100011 01100101 -This is correct. The characters making up science are s, c, i, e and n. By finding each of these characters in the table and writing the 4 digits for the row it is in followed by the 4 digits for the column it is in we can represent each of these as a binary ASCII encoding: s = 0111 0011, c = 0110 0011, i = 0110 1001, e = 0110 0101, n = 0110 1110,. Putting these characters one after another to make the word science we get the encoding in this answer choice.
What is the binary equivalent to the decimal number of 78? a)1001100 b)1100110 c)1001110 d)
c)1001110 -This is correct. The value of 78 can be found using the following algorithm: Find the highest power of 2 which is smaller than or equal to 78: this is 26 = 64. Subtract this from 78: 78 − 64 = 14. Repeat this procedure until 0 is reached: the largest power of 2 smaller than or equal to 14 is 23 = 8 and 14 − 8 = 6. The largest power of 2 which is smaller than or equal to 6 is 22 = 4 and 6 − 4 = 2. The largest power of 2 which is smaller than or equal to 2 is 21 = 2 and 2 − 2 = 0. Having completed this algorithm we can see that 78 = 21 + 22 + 23 + 26. As the nth column from the right in a binary number represents 2n, this tells us that the 2nd, 3rd, 4th and 7th digits from the right will be 1's and the other digits will be 0's giving us the binary number 1001110.
Which of the following can be represented by a single binary digit? a)The temperature setting of a thermostat. b)A darkness setting on a toaster. c)The speed limit on a major highway. d)The direction of travel for an elevator.
d)The direction of travel for an elevator. -This is correct. The direction of an elevator can take only 2 values (up/down), therefore a single binary digit would be sufficient to represent this information (e.g. using a 1 to represent up and a 0 to represent down).
Which of the following statements about 32-bit binary values is NOT true? a)All real numbers within a finite interval can be expressed by a 32-bit integer representation. b)All whole numbers within a finite interval can be expressed by a 32-bit floating-point representation. c)All real numbers within a finite interval can be approximately expressed by a 32-bit floating-point representation. d)All whole numbers within a finite interval can be expressed by a 32-bit integer representation.
a) All real numbers within a finite interval can be expressed by a 32-bit integer representation. -This is correct. This is a false statement. The term "real numbers" refers to all numbers, including irrational numbers like π and 2 as well as infinitely repeating decimals. Every finite interval contains infinitely many of these real numbers, therefore it is not possible to represent them all using a finite amount of digits.
The code below and accompanying table will output a number which represents which of the following: a)The average of grades above 79. b)The code has errors, none of these will be displayed. c)The total number of grades above 79. d)The average of the 13 grades.
a)The average of grades above 79. -This is correct. The repeat loop checks the value of the item at position index# on Grades. If it is smaller than 80 then it is deleted. The value of index# is not changed if this is the case, as the next item on Grades will be shifted into the position of index# by this deletion. If the value is larger than 80, then it is added to the variable total, and the value of index# is incremented by 1. As the repeat loop is run the same number of times as the number of items on the list, then each item on the list will be processed in this way. At the end, the list will only contain the grades which are not smaller than 80, and each of these will have been added to total. As all the grades are integers, the grades which are not smaller than 80 are equivalent to grades which are larger than 79. The value of total is then divided by the current length of Grades to give the average of these numbers.
The following block of code in a video game is intended to keep track of the name of the high scorer. In the code, (number) is the score of the current player at the end of the game, (highscore) is the previous high score, (scoreboard) is the list that contains the names of previous players in order of their score (with the entry at index 1 containing the name of the current high scorer). IF (number > highscore) REMOVE (scoreboard, 1) name ← INPUT INSERT (scoreboard, 1, name) ELSE DISPLAY ("Sorry, Try Again") Which of the following best describes what this fragment of code does when the current player achieves a score which is higher than the current high score? a)The current player's name is inserted in the first position in the list (scoreboard), the name of the previous high scorer is deleted and all other names remain in the same position in the list. b)The current player's name is inserted in the first position in the list (scoreboard), the name of the previous high scorer is deleted and all other names are shifted one place lower in the list. c)The current player's name is inserted in the first position in the list (scoreboard) and all other names remain in the same position in the list. d)The current player's name is inserted in the first position in the list (scoreboard) and all other names are shifted one place lower in the list.
a)The current player's name is inserted in the first position in the list (scoreboard), the name of the previous high scorer is deleted and all other names remain in the same position in the list.
If our list called List is populated as [0, 1, 2, 3, 4], what happens if our code tries to access List [5] per the rules of the AP Computer Science Principles Reference Guide? a)The output will be 4 b)The program gives us the number 5 because it comes after 4 c)The output will be 1 d)An error message will be produced
a)The output will be 4 -This is correct. The Pseudocode used in the AP CSP course indexes items on the list according to their position, beginning from 1. Hence in this list, the item at index 1 is 0, the item at index 2 is 1 etc. Therefore when the code is run with the statement List [5], the item in the 5th position of the list will be accessed. This is the number 4.
Which of the following questions cannot be easily answered using a binary set of answers? a)Which is the best song new song from last year? b)Would you like to go to the store? c)Do you prefer Mac or PC computers? d)When you write with a pen or pencil, which hand do you use?
a)Which is the best song new song from last year? -This is correct. There are many possible answers to this question, as in one year many thousands of songs are released. A binary set of answers only allows for 2 possibilities, represented by a 1 or a 0.
A teacher uses the following program to adjust student grades on an assignment by adding 5 points to each student's original grade. However, if adding 5 points to a student's original grade causes the grade to exceed 100 points, the student will receive the maximum possible score of 100 points. The students' original grades are stored in the list gradeList, which is indexed from 1 to n. i ← 1 REPEAT n TIMES { i ← i + 1 } The teacher has the following procedures available. min (a, b): Returns the lesser of the two values a and b max (a, b): Returns the greater of the two values a and b Which of the following code segments can replace so that the program works as intended? Select two answers. a)gradeList [i] ← min (gradeList[i] + 5, 100) b)gradeList [i] ← gradeList[i] + 5 IF (gradeList [i] > 100) { gradeList [i] ← 100 } c)gradeList [i] ← max (gradeList[i] + 5, 100) d)gradeList [i] ← gradeList[i] + 5 IF (gradeList [i] > 100) { gradeList [i] ← gradeList[ [i] - 5 }
a)gradeList [i] ← min (gradeList[i] + 5, 100) -This is correct. The statement min (gradeList[i] + 5, 100) returns gradeList[i] + 5 if this is smaller than 100, and 100 otherwise since it returns the minimum of the two expressions. Therefore with this code the program will set each grade 5 points higher if that does not give a result greater than 100, and set it to 100 otherwise. b)gradeList [i] ← gradeList[i] + 5 IF (gradeList [i] > 100) { gradeList [i] ← 100 } -This is correct. For each grade, this code will initially increase its value by 5, before using an IF statement with a condition checking if the modified grade is greater than 100. If it is, then the code in the IF block will run, setting the value of the grade to 100. Therefore with this code the program will set each grade 5 points higher if that does not give a result greater than 100, and set it to 100 otherwise.
Which of the following decimal values, when converted to binary (ignore leading zeros) have exactly 3 zeros in them? Select two answers. a)40 b)50 c)20 d)30
b)50 -This is correct. The binary representation of 50 is 110010, so this has exactly 3 zeros in it. & c)20 -This is correct. The binary representation of 20 is 10100, so this has exactly 3 zeros in it.
The code segment below uses the procedure IsPartOf (list, item), which returns true if item appears in list and returns false otherwise. The list newList is initially empty. FOR EACH item IN oldList { IF (NOT IsPartOf (newList, item)) { APPEND (newList, item) } } Which of the following best describes the contents of newList after the code segment is executed? a)All elements in oldList, including any repeats of elements. b)All unique elements in oldList NOT including any repeats of elements. c)All elements in oldList which are NOT repeated. d)All elements in oldList which are repeated.
b)All unique elements in oldList NOT including any repeats of elements. -This is correct. The code uses an IF condition to check if the condition NOT IsPartOf (newList, item) is satisfied. This will only be satisfied if IsPartOf (newList, item) is false; in other words if item does not appear on newList. If this is the case, item is added to newList. As this code uses the structure FOR EACH item IN oldList, item is consecutively set to each item on oldList, and added to newList if it is not already on this list. At the end of the code therefore, newList will only contain unique items, and will contain every item which appeared at least once on oldList.
Daija is creating a budget, and evaluating his purchases over the last month. Consider the following code segment which attempts to find the sum of all purchases made in the last month, stored in (purchaseList). n ← 1 sum ← 0 REPEAT UNTIL (<missing code>) { sum ← sum + purchaseList[n] n ← n + 1 } What code can replace missing code to assign the total of all values in (purchaseList) to sum? I. n = LENGTH (purchaseList) II. n ≥ LENGTH (purchaseList) III. n > LENGTH (purchaseList) a)I, II, and III b)III only c)II and III d)II only
b)III only -This is correct. The loop needs to exit after the last item in purchaseList has been processed. The variable n starts with a value of 1, and the last item of the list will have a position number equal to LENGTH (purchaseList). The item at position n is added to the list each run of the loop before the value of n is increased by 1. Therefore the last time the loop runs we want n to be equal to LENGTH (purchaseList), but at the end of this run of the loop, increase to greater than this. Therefore the repeat until condition should be that n is greater than LENGTH (purchaseList). This is expressed as n > LENGTH (purchaseList). If one of the other two conditions is used, the loop will exit when n is equal to equal to LENGTH (purchaseList). Therefore the last item on the list at position n will not be added to the list.
What would be the result of running the program below? list ← [ 1, 2, 3 ] APPEND ( list, 5 ) I NSERT ( list, 4, 4 ) REMOVE ( list, 5 ) DISPLAY ( list ) a)The program would display the list [1, 2, 3, 5, 4]. b)The program would display the list [1, 2, 3, 4]. c)The program would display an error message. d)The program would display the list [1, 2, 3, 4, 4].
b)The program would display the list [1, 2, 3, 4].
The procedure below is intended to return true if a particular city name (cityName) appears more than once in a list of cities (cityList), and false if it does not appear, or only appears once in the list. PROCEDURE CityRepeat (cityList, cityName) { counter ← 0 FOR EACH city IN cityList { IF (city = cityName) { counter ← counter + 1 } } IF (counter = 1) { RETURN false } ELSE { RETURN true } } Which of the following statements about the testing of this procedure is true? a)Calling the procedure CityRepeat (["Chicago", "New York", "Los Angeles", "New York"], "New York") shows that the procedure works as intended in all cases. b)Calling the procedure CityRepeat (["Chicago", "Chicago", "New York", "Los Angeles", "Chicago", "New York"], "Chicago") shows that the procedure does not work as intended in this case. c)Calling the procedure CityRepeat (["New York", "Los Angeles" "New York", "Chicago"], "Boston") shows that the procedure does not work as intended in this case. d)Calling the procedure CityRepeat with a wide variety of different parameters should show that the procedure works as intended in all cases.
c)Calling the procedure CityRepeat (["New York", "Los Angeles" "New York", "Chicago"], "Boston") shows that the procedure does not work as intended in this case. -This is correct. As "Boston" does not appear in the list ["New York", "Los Angeles" "New York", "Chicago"], the procedure should return false. However as the variable counter effectively counts the number of items in the list, and the condition for false to be returned is counter = 1 , this will not be the case: instead true will be returned, showing the procedure does not work as intended.
A program is used to print name tags for guests at an event. Unfortunately for each guest it prints the last name before the first name when the opposite is desired. The procedure for printing the name tags uses the variables FN for the first name and LN for last name. Which of the following code segments, inserted in the correct place in the procedure will cause the program to print the name tags in the desired way? a)Temp ← LN Temp ← FN FN ← LN LN ← FN b)FN ← LN LN ← FN LN ← Temp c)Temp ← LN LN ← FN FN ← Temp d)LN ← FN LN ← Temp FN ← Temp
c)Temp ← LN LN ← FN FN ← Temp -This is correct. First the value of the variable Temp is set to the current value of LN. Then the value of LN is changed to be the current value of FN. At this point, LN and FN are both set to first name (the original value of FN), while Temp holds the last name value (the original value of FN). To complete the swap, FN is set to the value of Temp. Now the variable LN holds the first name, and FN holds the last name, so these two will be printed in the correct order.
Assuming that the list months contains exactly 12 elements, what would happen when the script below is run? (insert [thing] at [19] of (months) ) a)The element (thing) will be added to the end of the list (months). b)The element (thing) will be added to the beginning of the list (months). c)The list (months) will remain unchanged. d)Blank entries will be added to the list (months) so that the new element (thing) can be inserted at index 19.
c)The list (months) will remain unchanged.
ASCII is a common format for the representation of characters in writing code. How many characters can be represented in the standard ASCII encoding? a)2^8 b)2^16 c)2^15 d)2^7
d)27 -This is correct. The standard ASCII encoding developed in the 1960s uses 7 binary digits to represent each character, giving a total of 27 = 128 different possible characters.
The following steps can be used to encode a string of text using Base64 encoding: 1. Convert the ASCII characters to their corresponding decimal value 2. Convert the decimal values to their 8-bit equivalents 3. Join all of the bits together to form one continuous string 4. Split the combined string into groups of 6 bits 5. From left to right, convert each group of 6 bits to their corresponding decimal values 6. Convert those decimal values back to the corresponding Base64 values If I started with the three ASCII characters "CSP", how many Base64 values will I be left with at the end? a)6 b)2 c)24 d)4
d)4 -This is correct. The three ASCII characters will each be represented by 8 bits when converted into binary. This means there will be 3 ✕ 8 = 24 bits representing the message. When this is split into groups of 6 bits, there will be in total 24 ÷ 6 = 4 groups. Each of these groups converts into one Base64 value, so the encoded message will consist of 4 values.
What value does the following code segment display? list ← (13, 9, 3, 11, 5, 12) { REPEAT UNTIL (LENGTH [list] = 1 ) a ← list [1] REMOVE [List, 1] a ← a + list [1] REMOVE [List, 1] APPEND [List, a] } a ← list [1] REMOVE (List, 1) DISPLAY [a] a)No value is ever displayed because the algorithm repeats the (REPEAT UNTIL) block indefinitely. b)36 c)17 d)53
d)53
The county clerk's office is writing a program to search their database for citizen records based on information about that citizen such as first name, last name, age, etc. Given below is a segment of pseudocode that should find the record of every citizen over the age of 50 in the county. The code makes use of the functions GetAgeOf and GetNameOf, which are used to retrieve the age and name of a citizen from a record which is inputted. For each person in citzlist { age=GetAgeOf(person) name=GetNameOf(person) IF(age < 50) { DISPLAY(name) } There is a mistake in this pseudocode. Which of the following is the correct pseudocode to find the records of all citizens over the age of 50. a)FOR EACH person IN citzlist { age = GetAgeOf(person) name = GetNameOf(person) IF(age < 51) { DISPLAY(name) } } b)FOR EACH person IN citzlist { age = GetAgeOf(person) name = GetNameOf(person) IF(age = 50) { DISPLAY(name) } } c)FOR EACH person IN citzlist { age = GetAgeOf(person) name = GetNameOf(person) IF(age > 51) { DISPLAY(name) } } d)FOR EACH person IN citzlist { age = GetAgeOf(person) name = GetNameOf(person) IF(age > 50) { DISPLAY(name) } }
d)FOR EACH person IN citzlist { age = GetAgeOf(person) name = GetNameOf(person) IF(age > 50) { DISPLAY(name) } } -This is correct. This code uses the condition (age > 50) in the IF block, meaning the code in this block is only run if the value of the variable age is greater than 50. As this variable is set to the age of the next person on the list each time the loop iterates, this block will only run for a person over 50. The code in the IF block displays the variable name, which is set to the name of the person on the list currently being dealt with. Therefore this code correctly displays the name of each person on the list over 50.
Based on the code below, what would be the best phrase to replace missing output? If ((LENGTH (list)) MOD 2 = 0) DISPLAY (missing output) ELSE DISPLAY (missing output) a)You have more than two items in your list! b)You have an odd amount of items in your list! c)You have zero items in your list! d)You have an even amount of items in your list!
d)You have an even amount of items in your list! -This is correct. The expression LENGTH (list) MOD 2 finds the remainder when the number of items on the list is divided by 2. This expression will have a value of 0 for lists for which LENGTH (list) is 0, i.e. lists on which there are an even amount of items.
A programmer has two lists of random words, list1 and list2. He has written a program that is intended to create a list, finalList, of all the words found in list1 and list2 in alphabetical order with no repeated words. His program makes use of the following procedures: Alphabetize(list)- Returns a list that contains the elements of list in alphabetical order. Join (list 1, list2)- Returns a list with the elements of list1 followed by the elements of list2 RemoveDuplicates (list)- Returns a list with the same elements in the same order as list, but with any duplicate elements removed. The programmer's program is as follows: list1 ← RemoveDuplicates (list1) list2 ← RemoveDuplicates (list2) finalList ← Join (list1, list 2) finalList ← Alphabetize (finalList) a)finalList, will contain all the words found in list1 and list2 with no repeated words, but these may not be in alphabetical order. b)As intended, finalList, will contain all the words found in list1 and list2 in alphabetical order with no repeated words. c)finalList, will contain all the words found in list1 and list2 but they may not be in alphabetical order and also contain repeated words. d)finalList, will contain all the words found in list1 and list2 in alphabetical order, but it may contain repeated words.
d)finalList, will contain all the words found in list1 and list2 in alphabetical order, but it may contain repeated words. -This is correct. The final command of the program is Alphabetize (finalList), therefore the items on the final list will be in alphabetical order at the end of the program, whatever these items are. While the RemoveDuplicates () command has been used, it has been used with parameters of list1 and list2 before these are joined. This allows there to still be duplicates as when the lists are joined, the same item could appear once on both list1 and list2. After joining these, this item would appear twice on list.
While running this block of code, the user enters in the following values when they are prompted: pizza yes bread no. What is stored to the list called grocery after the block of code is executed? grocery ← milk, eggs, cheese answer ← "yes" { REPEAT UNTIL (answer =/ "yes") DISPLAY ("Add an item") item ← INPUT INSERT (grocery, 2, item) DISPLAY ("Add an item?") answer ← INPUT } a)milk, bread, cheese b)milk, eggs, cheese, pizza, bread c), pizza, bread, eggs, cheese d)milk, bread, pizza, eggs, cheese
d)milk, bread, pizza, eggs, cheese -This is correct. The first input, "pizza", is assigned to the variable item, which is then inserted into the second position in the list, shifting the items after this position one place lower in the list. At this point the list is "milk, pizza, eggs, cheese". The answer of "yes" means the loop repeats, and then the next input "bread" is inserted into the second position in the list, again shifting the items after this position one place lower in the list. At this point the list is "milk, bread, pizza, eggs, cheese". The next input is "no", which causes the loop to exit and the program to terminate.
Sometimes we care about the order of a list, and need to reorder the items according to a condition (alphabetical, numerical, etc). An algorithm finds the minimum value in the list and swaps it with the value in the first position, then repeats these steps for the remainder of the list, swapping with the second position, then the third position and so forth. What type of sorting is this? a)insertion b)bubble c)shuffle d)selection
d)selection -This is correct. This algorithm is commonly called a selection sort as each time the minimum value is "selected" from the remaining list and moved to the top.