CSP Test 3

Ace your homework & exams now with Quizwiz!

Inspired by a tip-calculating mobile application that she made in the classroom, a student decides to create a program that uses percentages. She notices on a social media website that there is much debate over a proposed local sales tax increase. She decided to create a calculator into which users will be able to input the amounts they spend on various items, and which will output the current tax, and the proposed future tax on those items in dollars. The app will tell the user how much more the goods will cost with the new tax than the old tax. One of the procedures in her program, which finds the difference between the old and new tax, is listed below. PROCEDURE taxman (category, cost) { IF(category = "clothes") { old ← cost * .05 new ← cost * .06 } IF(category = "food") { old ← 0 new ← cost * .06 } IF(category = "other") { old ← cost * .05 new ← cost * .0725 } RETURN (new - old) }

.06 This is correct. The second IF block in the code will be run, as the condition (category = "food") is satisfied. This will set old to a value of 0 and new to a value of 1.00 * .06 = .06. Then the program will return the value of new - old, giving a value of .06 - 0 = .06.

What is the binary equivalent to the decimal number of 78?

1001110 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.

ASCII is a common format for the representation of characters in writing code. How many characters can be represented in the standard ASCII encoding?

2 ^ 7 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 value2. Convert the decimal values to their 8-bit equivalents3. Join all of the bits together to form one continuous string4. Split the combined string into groups of 6 bits5. From left to right, convert each group of 6 bits to their corresponding decimal values6. 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?

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.

Which of the following decimal (base-10) values is equivalent to the binary (base-2) value 101001?

41 This is correct. To convert a binary number to a decimal number, remember that the columns of a binary number represent powers of 2, beginning with the rightmost being 20 = 1, the next right being 21 = 2... etc. The columns with 1's in this number represent 20 = 1, 23 = 8 and 25 = 32. Therefore this binary number is equivalent to 1 + 8 + 32 = 41.

Which of the following decimal values, when converted to binary (ignore leading zeros) have exactly 3 zeros in them? Select two answers.

50 and 20 This is correct. The binary representation of 50 is 110010, so this has exactly 3 zeros in it. This is correct. The binary representation of 20 is 10100, so this has exactly 3 zeros in it.

What is the binary number 1001110001 expressed as a decimal number?

625 This is correct. To convert a binary number to a decimal number, remember that the columns of a binary number represent powers of 2, beginning with the rightmost being 20 = 1, the next right being 21 = 2... etc. The columns with 1's in this number represent 20 = 1, 24 = 16, 25 = 32, 26 = 64 and 29 = 512. Therefore this binary number is equivalent to 1 + 16 + 32 + 64 + 512 = 625.

How many distinct values can be represented with 6 bits of data?

64 This is correct. The number of values which can be represented by n binary digits is given by 2n, as each digit has 2 possibilities, a 0 or a 1. Therefore with 6 digits, 26 = 64 different values can be represented. In numerical terms, as each column in a binary number represents successive powers of 2 from 20 in the rightmost column, the values which can be represented by 6 binary digits are equivalent to the decimal integers between 0 and 63 inclusive.

Baudot Code

A binary code invented by Emile Baudot in 1870 that uses crosses and dots in order to encode 2^5 or 32 characters.

Unicode

A binary encoding system that can represent much more of the world's text than ASCII can (represents 65,536 different characters)

Morse Code

A code where letters are represented by combinations of long and short signals of light or sound

Procedure

A group of blocks that are combined to perform a specific task.

String

A linear sequence of characters, words, or other data

List

A list is an ordered sequence of elements. For example, [value1, value2, value3, ...] describes a list where value1 is the first element, value 2 is the second element, value 3 is the third element, and so on.

Linear Search

A method for finding a target value within a list (whether presorted or not) by checking each value until a match is found or until all the elements have been searched.

Bit String

A sequence of bits that can be used to represent sets or to manipulate binary data.

Substring

A subset of a string of alphanumeric fields or variables

ASCII (American Standard Code for Information Interchange)

A table that outlines a common set of conventions established for converting between binary values and alphanumeric (represents 128 different characters)

Which of the following statements about 32-bit binary values is NOT true?

All real numbers within some finite intervals can be expressed by a 32-bit integer representation. 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 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?

All unique elements in oldList NOT including any repeats of elements.

Element

An element is an individual value in a list that is assigned a unique index. data structure: a particular way of organizing and storing data such as an array, table, etc.

Mapping

Associating each element of a given set with one or more elements of a second set.

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?

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.

Data

Characters, symbols or quantities on which operations are performed, stored and/or transmitted by a computer.

Binary Code

Code represented with the two symbols of 1 and 0

Decimal

Describes the base-10 number system. The most commonly used number system.

Approximation

Digital copies are only approximations of the natural object

Consider the following procedure. PROCEDURE find (target, list) { i ← 1 FOR EACH item IN list { IF (item = target) { DISPLAY (i) } i ← i + 1 } } Which of the following best describes the behavior of the find procedure?

Displays the positions of all occurrences of target in list This is correct. The FOR EACH item IN list structure sets the value of the variable item to each item in list in turn, while the variable i is initially set to 1, and is increased by 1 on each run of the loop. This means i will be equal to the position number of item in list at the start of each run of the repeated code. The code within the IF block is run when this value matches target, and as the code in this block displays the value of i, it is the position of item on list which is displayed each time this runs.

Data Abstraction

Help manage complexity in programs by giving a collection of data a name without referencing the specific details of the representation.

Digital

How information is stored, accessed, transformed and used by computers state space: the space of potential possibilities

Consider the goal of finding the sum of all odd integers in a list, myInts, of integers. What should replace <block of statements> to successfully find the sum of all odd integers? sum ← 0; FOR EACH num IN myInts { <block of statements> }

IF (NOT (num MOD 2 = 0)) { sum ← sum + num } This is correct. The statement num MOD 2 evaluates to the remainder when the variable num is divided by 2. Therefore num MOD 2 = 0 will be true when num is even, and false when it is odd. Since the condition of the IF block is NOT (num MOD 2 = 0), the code in the IF block will only run if the value of num is odd. As this code adds num (which is set to each item on the list in turn by the FOR EACH num IN myInts structure) to the current value of sum, using this fragment will mean that the program adds all of the odd numbers on myInts.

Dichotomous

In which something can only be one thing or another (yes or no)

Digital Noise

Irrelevant or meaningless data that has found its way into otherwise meaningful code.

Analog

Non-digital signals or information represented by a continuously variable physical quantity such as spatial position or voltage.

Real Numbers

Numbers approximated by floating-point representations that do not necessarily have infinite precision.

Floating-point numbers

Numbers where the decimal point can float because there is no fixed number of digits before and after the decimal point. AKA: real numbers

Fixed-point numbers

Numbers where the decimal point is always in the same place.

Output

Observable behaviors generated by the computer such as animation, sound, and text

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?

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.

Discrete

Separate or divided (digital)

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?

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.

Join

The "join" block in Scratch concatenates, or links two values together.

Alphanumeric

The characters that consists of uppercase and lowercase letters in addition to numerals 0-9.

Which of the following can be represented by a single binary digit?

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).

Bits

The foundation for digital computing (1s and 0s) - short for binary digits

Scientific Notation

The mathematical representation of a decimal number in floating-point form

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?

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.

Abstraction

The process of removing or suppressing details to create a manageable level of complexity

Exponential Growth

The rate of growth that rapidly increases in proportion to the growing total number or size

Index Value

The representation of the location of each item in a list.

Consider the following algorithm. Subtract 1 from the current value of Y and store the answer as the new value of YSet the value of X to the current value of Y+1Set the value of X to the current value of YRepeat steps 1 to 3 until X = 10 Which of the following best describes the behavior of this algorithm based on the starting values of the variables X and Y?

There are many possible starting values for X and Y which will cause the algorithm to eventually terminate, and many possible starting values for X and Y which will cause the algorithm to run indefinitely This is correct. Each time steps 1 to 3 are run Y is set to one less than its previous value, and X is (eventually) set to this same value. Therefore for any value of Y which is larger than 10 the value of both X and Y will eventually reach 10 and the program will terminate. However for any value of Y less than or equal to 10 the program will never terminate as the values of X and Y will decrease each time steps 1 to 3 are run, never reaching 10.

Students were asked to order a list of the seven days of a week, without a specification as to the rules of the ordering. Which of the following ordered lists may be considered correctly ordered? I. Mon-Tue-Wed-Thu-Fri-Sat-Sun II. Thu-Fri-Sat-Sun-Mon-Tue-Wed III. Fri-Mon-Sat-Sun-Thu-Tue-Wed

This is correct. As no rules by which the ordering should be conducted were specified, any of the orderings could be true. Orderings I and II could both be following a modality based on time, but starting at different points as the week is cyclical. Ordering III could be based on an alphabetic modality.

Continuous

Unbroken, without interruption (analog)

Variable-width encoding

Using codes of different lengths to encode a character set for representation (example: Morse Code)

Fixed-width encoding

Using codes with a fixed width to encode a character set for representation (example: Baudot Code)

Which of the following questions cannot be easily answered using a binary set of answers?

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.

The procedure prime returns a value of "PRIME" if number (a positive integer) is prime (divisible by only 1 and itself) and returns "NOT PRIME" otherwise. PROCEDURE prime (number) { divisor ← number count ← 0 REPEAT number TIMES { IF (number MOD divisor = 0 ) { count ← count + 1 } divisor ← divisor - 1 } IF (<MISSING CODE>) { RETURN ("PRIME") } ELSE { RETURN ("NOT PRIME") } }

count = 2 This is correct. The variable count is initialized with a value of 0. Each time the REPEAT loop runs, the value of count is increased by 1 if the current value of divisor is a factor of number. The variable divisor begins with a value of number, its value is decreased by 1 each time the loop runs, and the loop runs number times in total. Therefore this condition is checked for every integer between 1 and number inclusive. If the value of count at the end of 2, therefore number has exactly 2 divisors and must be prime. So the condition to display "PRIME" should be that count = 2.

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) What will be the contents of finalList after running this program?

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.

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.

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. 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.

The procedure Max(list) is used to find the maximum number in list and display that number. See the procedure below: PROCEDURE Max(list) { maximum ← 0 FOR EACH number IN list { IF (number > maximum) { maximum ← number } } DISPLAY (maximum) } Which of the following list assignments will demonstrate that the procedure does NOT work as intended? Select two answers.

list ← [-5, -3, -10, -8, -1] This is correct. When this list is passed to the procedure the value of the variable maximum will be initially set to 0. After this each number on the list is iterated through, and maximum is set to this number if it is greater than the previous value of maximum. Since this will not be true of any numbers on this list (they are all smaller than 0) the value of maximum will not change, and 0 will be displayed by the procedure, despite it not appearing on the list. list ← [ ] This is correct. When this list is passed to the procedure the value of the variable maximum will be initially set to 0. After this each number on the list is iterated through, and maximum is set to this number if it is greater than the previous value of maximum. Since this list does not contain any numbers at all, the value of maximum will not change, and 0 will be displayed by the procedure, despite it not appearing on the list.

In order to program a robot to move around within a two-dimensional space, a programmer considers four different designs for defining the robot's behavior. Which of the following sets of procedures would be the best design in terms of the readability, efficiency, and flexibility of the resulting program?

move(dir, dist): Moves the robot dist spaces in the dir direction, where dist is a number and dir is NORTH, SOUTH, EAST or WEST This is correct. This is a very flexible solution, as only one single procedure would be defined rather than 4 commands with very similar blocks of code. Changes to this procedure would only need to be applied once rather than multiple times, and the total amount of code would be less, making it more efficient and readable.

A gaming software company gave their new customers a survey, asking them to rate various aspects of a new product on a scale of 1 to 5. The integer values of their answers were stored in a list feedback. Which code segments would assign the mean average value of the items on the list to the variable average? Select two.

sum ← 0 item ← 1 REPEAT LENGTH(feedback) times { sum ← sum + feedback [item] item ← item + 1 } average ← sum / LENGTH(feedback) This is correct. The value of the variable sum is initially set to 0, and the variable item is set to 1. Next the REPEAT structure adds the value of the item on the list at the position given by item to the current value of sum, then increases the value of item by 1. It does this LENGTH (feedback) times (equal to the number of items on the list feedback), meaning each item from feedback is added to sum before the loop exits. sum ← 0 FOR EACH item IN feedback { sum ← sum + item } average ← sum / LENGTH(feedback) This is correct. The value of the variable sum is initially set to 0, then the FOR EACH item IN feedback structure sets the value of the variable item to each item on the list feedback sequentially from the first to the last, running the code in the loop each time. Each time the current value of item is added to sum, meaning each value on the list is added to sum until it is equal to the sum of all items on the list and the loop exits. The average is then calculated by dividing this sum by LENGTH (feedback) which is the number of items on the list.


Related study sets

Project Management: Ch 5 quiz Qs

View Set

ATI Placement Exam Practice Questions

View Set