Algorithms are Instructions #26-34 & Algorithms and Problem-Solving #1-16

¡Supera tus tareas y exámenes ahora con Quizwiz!

32. (Difficult) Understanding & Interpreting Pseudocode: Pseudocode is used to describe an algorithm so that other people can better understand it. The following pseudocode was written by a systems analyst to describe the function that he wanted the programmer to develop. The actual programming language would be determined later by the development team. n <-- INPUT () i <-- 0 REPEAT UNTIL ( n = -1 ) { i <-- i + 1 list[i] <-- n n <-- INPUT () } m <-- 0 j <-- 1 REPEAT i TIMES { IF ( list[j] > m ) { m <-- list[j] } j <-- j + 1 } DISPLAY ( m ) The systems analyst wrote the pseudocode in the style used on the AP Computer Science Principles exam. However, instead of using more descriptive variable names, the analyst used single letter variable names. This made it more difficult to read. Which of the following is a possible description that would correctly and more simply explain this pseudocode? A. Final exam numeric grades ranging from 0-100 are entered into a list. Once all the grades are entered, the highest grade is displayed. B. Golf scores from the first round of the county championship are entered into a list. Once all the scores are entered, the lowest score is displayed. C. Report card letter grades (A-F) for all freshmen in an AP Calculus class are entered into a list. Each entered letter grade is displayed. D. Class rankings of all students majoring in Computer Science are entered into a list. Once all the class rankings are entered, the average ranking is displayed.

CHOICE A IS CORRECT: A summary of the pseudocode is as follows: -Something is input and stored in variable n. -Variable i is given the value 1. -The first loop keeps repeating until n equals -1, which is the value entered by the user when they have no more values to input. -Each time through the loop, the value entered is added to a list named "list", then index i increases by 1, and another value is input and assigned to n. -After the loop completes, "list" contains all of the values input by the user. -Variable m is given the value of 0, and variable j is given the value of 1. -The second loop repeats i times which is the number of entries in "list". -Within this loop, the code checks to see if the element in "list" is greater than m and if so, m is replaced by this element. Also, index j is increased by 1. -After looping, the value of m is the maximum value in "list" and it is displayed. Suppose that n represents a numeric grade, list is a list of grades, and m represents the maximum grade. Choice 'A' would be a possible description since it fits the logic of this algorithm. Explanation of Distractors Choice 'B' is an incorrect answer. Variable n could certainly represent a golf score that is input and stored in list. However, since m represents a maximum value, Choice 'B' would not be a possible description since it says that the lowest score in the list is displayed. Choice 'C' is an incorrect answer. Variable n could certainly represent a report card letter grade that is input and stored in list. However, the display statement is after the loop and will only display one value, which is the maximum value in the list. Therefore, it is not displaying all the letter grades that were input and stored in list. Choice 'D' is an incorrect answer. Variable n could certainly represent a class ranking that is input and stored in list. However, since m represents a maximum value, Choice 'D' would not be a possible description since it says that the average ranking in the list is displayed.

9. (Easy) Solving Problems: Eric reads an article about a problem in computer science. He reads that there is an algorithm to solve the problem, but the algorithm only works for some of the possible inputs. It's been proven that there is no algorithm that works for the other inputs. What kind of problem is Eric reading about? A. Undecidable B. Unreasonable C. Unsolvable D. Undetermined

CHOICE A IS CORRECT: An undecidable problem is one for which a single algorithm cannot be created to always lead to a yes or no answer. The problem that Eric is studying fits this description perfectly; an algorithm works for some of the possible inputs, but for others there is not an algorithm that can be used to reach a decision. Explanation of Distractors: Choice 'B' is incorrect; an algorithm with an unreasonable run time is an algorithm that cannot solve a problem in a reasonable amount of time. That does not describe this situation because Eric reads that the problem can't be solved for some of the inputs. Choice 'C' is incorrect; an unsolvable problem cannot be solved at all, but Eric read that there is an algorithm that solves the problem for some of the inputs. Choice 'D' is also incorrect; undetermined does not make sense.

12. (Easy) Binary Vs. Linear: Consider the following lists. I. list1 <---[2, 4, 5, 7, 11, 33] II. list2 <---[-2, -1, 0, 1, 2, 3, 4, 5, ..., 900, 901, 902] III. list3 <---[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 0] For which of the following lists would a binary search be more efficient than a linear search? A. II only B. III only C. II and III only D. I, II, and III

CHOICE A IS CORRECT: Binary searches are used to search through sorted lists. The efficiency of the binary search increases as the size of the list increases. Therefore, the only list that is both large and sorted is list2. Explanation of Distractors: Choice 'B' is incorrect. While list3 is large, it is not sorted and we cannot use binary search. Choice 'C' is incorrect. While list3 is large, it is not sorted and we cannot use binary search. Choice 'D' is incorrect. While list1 is sorted, it is a small list and a linear search would be more efficient. While list3 is large, it is not sorted and we cannot use binary search.

13. (Easy) An Algorithm for All Integers: Stan is working on a procedure that takes an integer as an input and returns true or false depending on certain criteria. He tests his procedure with a variety of inputs and notices that his algorithm only gives the correct answer when the input is negative or zero. He cannot come up with an algorithm that works for all integers. What kind of problem does Stan have? A. Unreasonable B. Undecidable C. Unfixable D. Undefined

CHOICE B IS CORRECT: Choice 'B' is correct. An undecidable problem is one for which there is no algorithm that results in a yes-or-no answer for every possible input. Since Stan cannot come up with an algorithm that always produces a yes or a no, his problem is undecidable. Explanation of Distractors: Choice 'A' is incorrect; unreasonable refers to an algorithm's run time and Stan's problem does not have to do with an algorithm's runtime. Choice 'C' and Choice 'D' are both incorrect as well.

6. (Easy) Compressed the Best: In data compression, there are many different methods for compressing data. We have many different protocols because there is no one method that provides the absolute best compression for all data files in a reasonable amount of time. Which of the following is the BEST term for the above example? A. An algorithm B. A heuristic C. A selection D. An iteration

CHOICE B IS CORRECT: The definition of a heuristic is a technique that allows the user to find an approximate answer in a reasonable amount of time. We use heuristic algorithms when problems cannot be solved in a reasonable time and they do not have an exact answer. Explanation of Distractors: Choice 'A' is incorrect because, while it is an example of an algorithm, the BEST answer is a heuristic because it is an algorithm that solves a problem that does not have an exact solution and cannot be solved in a reasonable amount of time. Choice 'C' is incorrect because selection is when a Boolean condition is used to determine if a part of an algorithm is used. Choice 'D' is incorrect because iteration is the repetition of a part of an algorithm.

10. (Easy) Searching a List: What method would be the best way to search for item in the list below? list <-- 6, 2, 1, 0, 3, 19, 201, 33, 17 A. Linear search because a linear search can only be used to search a sorted list. B. Linear search because a linear search can be used to search any list. C. Binary search because a binary search can be used to search a sorted list. D. Binary search because a binary search can be used to search any list.

CHOICE B IS CORRECT: The provided list is not in order and we cannot use a binary search to find a specific item. A linear search checks each term, one after another. A binary search checks the middle item and determines if it needs to look to the left or right of that number. A binary search only works in list that are sorted. Explanation of Distractors: Choice 'A' is incorrect because a linear search can sort any list. Choice 'C' is incorrect. While a binary search does search a sorted list, the list provided is not sorted. Therefore we cannot use a binary search. Choice 'D' is incorrect. A binary search searches a sorted list.

34. (Difficult) Implement Algorithm Using New Programming Language: Two Computer Science students, Drew and Jane, decided to create their own basic programming language which they called Drane (Drew + Jane). The following is a small subset of the statements available in the Drane programming language: --------------------------------------------------------------------------------------------------- Statement--- Explanation b = expression--- The result of the expression is assigned to variable b a + b, a - b, a * b, a / b--- + = addition, - = subtraction, * = multiplication, / = division WHEN [ condition ] { instructions1 } OTHERWISE { instructions2 }--- This is a conditional statement. When the condition is true, the statements represented by instructions1 are executed. Otherwise the statements represented by instructions2 in the OTHERWISE block are executed. Note: The WHEN statement can be used without the OTHERWISE clause. LOOP [ n ] { instructions }--- Loops n number of times and executes the instructions each time --------------------------------------------------------------------------------------------------- You have been asked to code the following algorithm using the Drane programming language: "You have been given two positive integers 'num1' and 'num2', where 0 < num1 < num2 and 'num2' is an even number. Keep subtracting num1 from num2 until the result is less than half of the original value of num2, then display the result only once at the end. For example, if num1 = 3 and num2 = 14, then the final result would be 14 - 3 - 3 - 3 = 5, and 5 would be displayed." Of the following, which would successfully code this algorithm in the Drane programming language? A. result = num2 LOOP [ num2 / 2 ] { WHEN [ result < ( num2 / 2 ) ] { result = num2 + 1 } OTHERWISE { result = result - num1 } } SHOW [ result ] B. result = num2 LOOP [ num2 ] { WHEN [ result >= ( num2 / 2 ) ] { result = result - num1 } } SHOW [ result ] C. result = num2 LOOP [ num2 / 2 ] { result = result - num1 } SHOW [ result ] D. result = num2 LOOP [ num2 ] { WHEN [ result >= ( num2 / 2 ) ] { result = result - num1 } OTHERWISE { SHOW [ result ] } }

CHOICE B IS CORRECT: This code from the Drane programming language will produce and display the correct result. To begin, result is given the value of num2. Then it loops num2 times, which more than covers the number of times it will actually need to loop. Each time it loops, the code checks to see if result >= ( num2 / 2 ). If this condition is true, then num1 is subtracted from result. Therefore, once result becomes less than num2 / 2, it will remain unchanged. After the loop repeats num2 times, it will end and the code will display result. As an example, let's say that num1 is 3 and num2 is 14:... Looping ends after it repeats 14 times. The code would then display 5, which is correct. Explanation of Distractors Choice 'A' is an incorrect answer. To begin, result is given the value of num2. Then it loops num2 / 2 times. Each time it loops, the code checks to see if result < ( num2 / 2 ). If this condition is true, then result is given the value of num2 + 1. Otherwise, it subtracts num1 from result. After the loop repeats num2 times, it will end and the code will display result. Choice 'C' is an incorrect answer. To begin, result is given the value of num2. Then it loops num2 / 2 times. Each time it loops, it subtracts num1 from result. After the loop repeats num2 / 2 times, it will end and the code will display result. Choice 'D' is an incorrect answer. To begin, result is given the value of num2. Then it loops num2 times. Each time it loops, the code checks to see if result >= ( num2 / 2 ). If this condition is true, then it subtracts num1 from result. Otherwise, it displays result. However, it is only supposed to display the answer once, not multiple times like this code.

16. (Moderate) Counting the Number of Loops in an Algorithm: In order to determine whether code can run in a reasonable amount of time, it is important to get a feel for the runtimes of different code segments. Let's assume that runtime is measured as the number of passes through a loop. In the following choices, this is represented by the variable "count". Assume that every element in the list named licenses is a unique license number, and that searchNum is a valid license number that exists in licenses. Which of the following segments of code would generally have the shortest runtime according to this definition of runtime? A. count <-- 0 FOR EACH licenseNum IN licenses { count <-- count + 1 } B. count <-- 0 i <-- 0 REPEAT UNTIL ( licenses[i] = searchNum ) { count <-- count + 1 i <-- i + 1 } C. count <-- 0 FOR EACH licenseNum IN licenses { IF ( licenseNum = searchNum ) { DISPLAY (licenseNum) } count <-- count + 1 } D. Variable n in the following code represents the number of elements in licenses. count <-- 0 REPEAT n TIMES { count <-- count + 1 }

CHOICE B IS CORRECT: This code uses a REPEAT UNTIL loop that checks the condition licenses[i] = searchNum at the bottom of the loop. It is stated that searchNum is a valid license that exists in licenses, therefore this condition will eventually be true since index i is incremented inside the loop. In the best case runtime scenario, searchNum is equal to the first element and count would be 1. In the worst case, searchNum is equal to the last element and count would be the number of elements in licenses. On average, this code would loop n / 2 times if n is the number of elements in licenses. Explanation of Distractors: Choice 'A' is an incorrect answer. This code loops through all elements in licenses. Therefore, this code would always loop n times if n is the number of elements in licenses. Choice 'C' is an incorrect answer. This code loops through all elements in licenses. Therefore, this code would always loop n times if n is the number of elements in licenses. Choice 'D' is an incorrect answer. This code loops through all elements in licenses, since n is the number of elements in licenses.

26. (Moderate) High-Level and Low-Level Programming Languages: Which of the following is NOT a true statement about high-level and low-level programming languages? A. An advantage of high-level programming languages is that they are generally more portable than low-level programming languages. B. Code written in a high-level programming language is considered to be more understandable by humans than code written in a low-level programming language. C. Java is a lower level programming language than assembly language. D. A low-level programming language is a language whose commands closely mimic the commands of the computer's processor.

CHOICE C IS CORRECT. A low-level programming language is a language that has fewer available instructions and is very close to the commands used by the computer's processor. A high-level language is a language that has many more commands and abstractions. Java is an example of a high-level language.Assembly language is very close to machine code, so it is a low-level language. Therefore, Java is not a lower level programming language than assembly language. NOT A: incorrect answer, since it is true. High-level programming languages are designed so that they are portable across different types of computers and operating systems. For example, Java is a high-level language that can be run on a multitude of computer platforms. In contrast, low-level programming languages are written to be very close to the language of the computer, which means that their code cannot be ported to other types of computer processors. NOT B: incorrect answer, since it is true. High-level programming languages are designed so that their instruction sets are much more extensive and human-readable than the small instruction sets of low-level programming languages. There are more layers of abstraction between the high-level instructions and the machine-readable code. Therefore, the high-level language code is more readable than low-level programming language code. NOT D: incorrect answer, since it is true. Low-level programming languages are designed to be very close to the instructions actually used by the computer's processor. For that reason, programs written in low-level code tend to run faster than those written in high-level programming languages.

2. (Easy) Can't Crack This!: By using a password that contains a mixture of 8 letters and numbers, a hacker could crack your password in a little over 16 minutes. However, by adding two more letters or numbers, it increases the time to about 15 days! By adding an "@" symbol to your password, the time is increased to almost 1628 years! Which of the following is TRUE regarding passwords and the length of time it takes to crack them? A. As you increase the number of characters in your password the ability to crack the password is solvable. B. As you increase the number of characters in your password the ability to crack the password is unsolvable. C. As you increase the number of characters in your password the ability to crack the password becomes more unreasonable to solve. D. As you increase the number of characters in your password the ability to crack the password becomes more reasonable to solve.

CHOICE C IS CORRECT: As the number of characters in a password increases, the amount of time needed to crack the password increases exponentially. While the password is solvable, the time it takes to solve becomes unreasonable. Explanation of Distractors: Choice 'A' is incorrect because the problem remains solvable regardless of the number of characters. However, the time it takes to crack the password increases exponentially. Choice 'B' is incorrect because the problem remains solvable regardless of the number of characters. However, the time it takes to crack the password increases exponentially. Choice 'D' is incorrect. As the number of characters in a password increases, the amount of time needed to crack the password increases exponentially. The time it takes to crack the password becomes unreasonable.

14. (Easy) Heuristics in Everyday Life: Which of the following is NOT an example of a heuristic? A. Meredith estimates that each floor tile is 10 inches by 10 inches. She counts floor tiles to determine the size of a room. B. Avaya begins reading a positive, well written review for a product online. Based on the quality of the writing, she assumes that the product must be good without reading the rest of the review. C. Martha is instructed to inventory all of the items in three aisles of a grocery store. She counts everything item by item and writes down the totals. D. Sarah, a college junior, goes to the campus center to give a tour to a high school student. She sees a crowd of people and assumes that the person wearing the high school letter jacket is the prospective student.

CHOICE C IS CORRECT: In this scenario, Martha must count every item in three aisles of a grocery store. There is no shortcut and she gets an exact answer, not an approximate one. A heuristic can be described as a shortcut that leads to an approximate answer, so this choice is not a heuristic. Explanation of Distractors: Choices 'A', 'B', and 'D' all describe heuristics. In all of these scenarios, a person is using a shortcut to reach an approximate answer. In Choice 'A', Meredith uses an estimation to get an idea of the size of the room without making actual measurements. This saves her time and gets her an approximate answer. In Choice 'B', Avaya guesses that the product review must be true because of the style of writing. She uses her past experiences with online reviews to get an approximate answer in less time than if she read the whole review. Similarly, in Choice 'D', Sarah uses her past knowledge to determine that the student wearing the high school letter jacket is most likely the prospective student. This saves her the time of having to ask each person in the crowd if they are a prospective student.

1. (Easy) Reasonably Reasonable: Algorithms need to run in a "reasonable" amount of time in order to be considered practically useful. Which of the following BEST describes how programmers determine if an algorithm takes a reasonable amount of time? A. Programmers time how long it takes a program to execute using multiple inputs. An average time is calculated after several trials. B. Programmers count the number of lines of code in an algorithm. C. Programmers count the number of steps an algorithm takes and compare that number to a polynomial function of the size of the input. D. Programmers run a standard base algorithm at the same time as their algorithm and note which one finishes executing first.

CHOICE C IS CORRECT: Run time is determined by first counting the number of steps an algorithm takes and then comparing that number to a polynomial function of the size of the input, as described in Choice 'C'. Actually examining those polynomial functions is outside the scope of this class, but it's important to know that run time is calculated by counting steps, not using a stopwatch to actually time the algorithm (as described in Choice 'A'). Choice 'B' is incorrect; the number of lines of code does not necessarily affect runtime. Choice 'D' is also incorrect; there are no "standard base algorithms" to which to compare.

8. (Easy) Be Reasonable, Heuristic!: Which of the following BEST describes what the purpose of a heuristic in programming? A. To provide an answer to a problem that can be solved in a reasonable time and an approximate solution is acceptable. B. To provide an answer to a problem that can be solved in a reasonable time and an exact solution is needed. C. To provide an answer to a problem that cannot be solved in a reasonable time and an approximate solution is acceptable. D. To provide an answer to a problem that cannot be solved in a reasonable time and an exact solution is needed.

CHOICE C IS CORRECT: The definition of a heuristic is a technique that allows the user to find an approximate answer in a reasonable amount of time. We use heuristic algorithms when problems cannot be solved in a reasonable time and they do not have an exact answer. Explanation of Distractors: Choice 'A' is incorrect because a heuristic is used when a problem cannot be solved in a reasonable time. Choice 'B' is incorrect because a heuristic does not provide an exact answer, only an approximate answer. Choice 'D' is incorrect because a heuristic does not provide an exact answer, only an approximate answer.

15. (Easy) Search Algorithms One: Consider the following algorithms. Assume that you have a deck of cards with numbers on them, and you are looking for a card with a particular number: Algorithm 1: 1. look at the middle card in your deck. 2. if correct, done! You have found what you are searching for! 3. if you are looking for a larger number, discard the lower half of the deck 4. else, discard the upper half of the deck 5. return to step 1 Algorithm 2: 1. look at the first card in your deck. 2. if this is what you are looking for, great! You have found what you are searching for! 3. discard the first card in your deck 4. return to step 1 Which of the following statements is TRUE? A. Both algorithms will find the value you are looking for in any list. B. Neither algorithm will find the value that you are looking for in any list. C. Algorithm 1 requires the list be already sorted. Algorithm 2 will always work. D. Algorithm 2 requires the list be already sorted. Algorithm 1 will always work.

CHOICE C IS CORRECT: The first algorithm is an example of binary search. In a binary search, you start in the middle of a sorted list. If you are not in the right place, you ignore the half of the list that is in the wrong direction, and jump to the middle of what is left. It is more efficient that the linear search of Algorithm 2, but it requires that the list already be sorted. Algorithm 2 will always work. Algorithm 2 is an example of linear search, where you start anywhere in the list, though usually at the start, and proceed one place at a time until you find the value for which you are searching. Explanation of Distractors Choice 'A' is incorrect because the first algorithm will not find the value you are looking for in ANY list. It only works for sorted lists. Choice 'B' is incorrect. The second algorithm will always work. Choice 'D' is incorrect as the second algorithm DOES NOT require the list already be sorted.

29. (Difficult) Dealing A Full Hand of Cards to Players: A programmer is asked to write a program to deal cards one at a time to each player until they each have five cards. As a start, the programmer develops an algorithm to deal a random card from a deck of cards and assign it to a given player (represented by a player number). This algorithm is coded as a procedure named dealCard, which accepts the player number (1-4) as a parameter. The implementation details of dealCard are not shown below. The programmer now wants to call this procedure within another procedure named dealCardToEachPlayer, whose purpose is to deal a random card to four different players. The last procedure named dealHand should call dealCardToEachPlayer and deal each player five cards. How could these algorithms be combined to deal a full hand of cards to each of the players (as described above) by calling the dealHand procedure? A. PROCEDURE dealCardToEachPlayer() { REPEAT 4 TIMES { dealHand(); } } PROCEDURE dealHand() { playerNum <-- 1 REPEAT 5 TIMES { dealCard( playerNum ); playerNum <-- playerNum + 1 } } B. PROCEDURE dealCardToEachPlayer( num ) { REPEAT 5 TIMES { dealCard( num ); } } PROCEDURE dealHand() { playerNum <-- 1 REPEAT 4 TIMES { dealCardToEachPlayer( playerNum ); playerNum <-- playerNum + 1 } } C. PROCEDURE dealCardToEachPlayer() { playerNum <-- 1 REPEAT 5 TIMES { dealCard( playerNum ); playerNum <-- playerNum + 1 } } PROCEDURE dealHand() { REPEAT 4 TIMES { dealCardToEachPlayer() } } D. PROCEDURE dealCardToEachPlayer() { playerNum <-- 1 REPEAT 4 TIMES { dealCard( playerNum ); playerNum <-- playerNum + 1 } } PROCEDURE dealHand() { REPEAT 5 TIMES { dealCardToEachPlayer() } }

CHOICE D IS CORRECT: The dealCardToEachPlayer procedure is defined to loop four times and call the dealCard procedure each time, passing this procedure the player number starting at 1. Each time at the bottom of the loop, the player number is incremented by 1, so that the next player is dealt a card. The dealHand procedure loops five times and calls the dealCardToEachPlayer procedure each time. This will correctly deal a single card to each player five times, so that each player ends up with five cards each. Explanation of Distractors: Choice 'A' is an incorrect answer. The dealHand procedure is looping five times and calling dealCard each time, so instead of dealing a hand (5 cards) to every player, it is dealing a single card to every player. Another issue is that since it is looping five times, it is dealing a single card to five players instead of four players as it should be doing. The dealCardToEachPlayer procedure is defined to loop 4 times and call the dealHand procedure each time. Choice 'B' is an incorrect answer. The end result of calling dealHand is that five cards will be dealt to each of the four players. However, the algorithm is not dealing cards in the order specified in the instructions. The dealHand procedure loops four times and each time it calls dealCardToEachPlayer. However dealCardToEachPlayer loops five times and deals all five cards to the same player. Therefore, calling dealHand will deal five cards to one player, then deal five cards to the next player, etc. Choice 'C' is an incorrect answer. The dealHand procedure loops five times and each time it calls dealCardToEachPlayer. This means that it is dealing cards to five players instead of four players as it should be doing. Also, dealCardToEachPlayer loops only four times, so each of the players is only dealt four cards.

11. (Easy) Yes, No, or Maybe?: The Entscheidungsproblem was a problem posed by David Hibert. The Entscheidungsproblem looks for an algorithm that will determine if any mathematical statement is provable from known axioms. The problem looks for a simple "yes" or "no" answer, however, just eight years after the problem was posed it was proved that there is no one algorithm that can be constructed that always lead to a correct "yes" or "no" answer. What do we call these types of problems? A. Unpredictable B. Unsolvable C. Unreasonable D. Undecidable

CHOICE D IS CORRECT: The definition of an undecidable problem is that we can write an algorithm that will run in some instances, but will not always lead to a correct "yes" or "no" answer. Explanation of Distractors: Choice 'A' is incorrect. If a problem yields and unpredictable response it is most likely a logic error in the code. It does not mean that the code does not have an algorithm that always leads to a correct "yes" or "no" answer. Choice 'B' is incorrect because unsolvable problems cannot be truly solved in any instance. They may give approximate answers or may not be solvable in any instance. Undecidable problems have at least one solution but that solution does not work for all problems. Choice 'C' is incorrect because an unreasonable problem is a problem that cannot be decided in a reasonable amount of time. Usually an approximate solution is used in these problems.

28. (Difficult) Introducing a New Programming Language: Two Computer Science students, Drew and Jane, decided to create their own basic programming language which they called Drane (Drew + Jane). The following is a small subset of the statements available in the Drane programming language: --------------------------------------------------------------------------------------------------- Statement--- Explanation b = expression--- The result of the expression is assigned to variable b. a + b, a - b, a * b, a / b--- + = addition, - = subtraction, * = multiplication, / = division WHEN [ condition ] { instructions1 } OTHERWISE { instructions2 }--- This is a conditional statement. When the condition is true, the statements represented by instructions1 are executed. Otherwise the statements represented by instructions2 in the OTHERWISE block are executed. Note: The WHEN statement can be used without the OTHERWISE clause. LOOP [ n ] { instructions }--- Loops n number of times and executes the instructions each time --------------------------------------------------------------------------------------------------- You have been asked to code the following algorithm using the Drane programming language: "You have been given two integers 'start' and 'stop' (both inclusive) and display the result. For example, if given the numbers 2 & 5, then the algorithm should display 14 (which is 2 + 3 + 4 + 5). Which of the following would successfully code this algorithm in the Drane programming language? A. sum = 0 num = start LOOP [ stop ] { num = num + 1 sum = sum + num } SHOW [ sum ] B. sum = 0 num = start LOOP [ stop - start ] { sum = sum + num num = num + 1 } SHOW [ sum ] C. sum = start num = start + 1 LOOP [ stop - start ] { WHEN [ num < stop ] { sum = sum + num num = num + 1 } } SHOW [ sum ] D. sum = 0 num = start LOOP [ stop - start + 1 ] { sum = sum + num num = num + 1 } SHOW [ sum ]

CHOICE D IS CORRECT: This Drane programming language code will produce and display the correct result. To begin, sum is given the value 0 and num is given the value of start. Then it loops stop - start + 1 times. As an example, let's say that start is 2 and stop is 5. That means that it would need to loop 4 times to add the numbers 2 + 3 + 4 + 5. So, stop - start + 1 = 5 - 2 + 1 = 4 is correct for calculating the number of loops. Inside the loop, the value of num (which begins as start) is added to sum, and then num is increased by 1. As an example, if start = 2 and stop = 5: pass sum num [before looping] 0 2 1 0 + 2 = 2 2 + 1 = 3 2 2 + 3 = 5 3 + 1 = 4 3 5 + 4 = 9 4 + 1 = 5 4 9 + 5 = 14 5 + 1 = 6 Looping ends since it has now looped 4 times. The code would then display 14, which is correct. Explanation of Distractors: Choice 'A' is an incorrect answer. To begin, sum is given the value 0 and num is given the value of start. Then it loops stop times, which is incorrect. As an example, let's say that start is 2 and stop is 5. That means that it would need to loop 4 times to add the numbers 2 + 3 + 4 + 5. However, the LOOP [ stop ] statement would cause it to loop 5 times... Looping ends since it has now looped 5 times. The code would then display 25. Choice 'B' is an incorrect answer. To begin, sum is given the value 0 and num is given the value of start. Then it loops stop - start times, which is incorrect. As an example, let's say that start is 2 and stop is 5. That means that it would need to loop 4 times to add the numbers 2 + 3 + 4 + 5. However, the LOOP [ stop - start ] statement would cause it to loop only 5 - 2 = 3 times. Looping ends since it has now looped 3 times. The code would then display 9. Choice 'C' is an incorrect answer. To begin, sum is given the value of start and num is given the value of start + 1. Then it loops stop - start times. As an example, let's say that start is 2 and stop is 5. The problem is that the code only adds to sum and increments num if num < stop. So on the last pass through the loop, it would not add num to sum since num is equal to stop at this point. Looping ends since it has now looped 3 times. The code would then display 9.

3. (Easy) Efficiency at its Finest: When determining the efficiency of an algorithm you must consider which of the following things? Select TWO answers. A. How long the program takes to run. B. The amount of storage the program requires. C. How many lines of code the program has. D. The platforms on which the program can run.

CHOICES A & B ARE CORRECT: Efficiency of an algorithm includes both execution time and memory usage. Explanation of Distractors: Choice 'C' is incorrect. Sometimes, more efficient algorithms are more complex. Just because a program has more lines of code does not mean it is less efficient. Choice 'D' is incorrect. An algorithms efficiency is not determined by which platforms is can be run from.

33. (Difficult) Factorial in Flowcharts: A factorial is a mathematical function that returns the product of the given counting number and all the counting numbers leading up to it. Counting numbers are integers greater than zero. A factorial is often indicated with an exclamation point (!). In math class you may have defined 0! =1., however, we are not worried about that situation in this problem. For example: 3! = 3 * 2 * 1 = 6 5! = 5 * 4 * 3 * 2 * 1 = 120 Which of the following properly defines a factorial function for a counting number n? Select TWO answers. A. (image) B. (image) C. (image) D. (image)

CHOICES A & D ARE CORRECT: Choices 'A' and 'B' use a loop. The function needs to decrease the number by which it is multiplying by one in each iteration of the loop. Choice 'B' is incorrect because it does not subtract one in each iteration of the loop. Thus it returns n^n. Choice 'A' is correct because the value of n is decremented in each iteration of the loop. Choices 'C' and 'D' use recursion rather than a loop. Again, the issue is making sure the function subtracts one in the correct place. The function needs to subtract one before making the recursive call. Choice 'C' is incorrect because does not subtract one each time, resulting in an infinite recursive call. Choice 'D' is correct because it subtracts one before calling the factorial function again.

30. (Difficult) Two Dice: You want to simulate rolling two six-sided dice. You are interested in how often you see a sum of 7 on the two dice. Which of the following would simulate counting the number of times one would see a sum of 7 on two dice rolls when rolling the dice 25 times? Select TWO answers. A. q ← 0 REPEAT 25 TIMES { a ← RANDOM(1,6) + RANDOM(1,6) IF (a = 7) { q ← q +1 } } DISPLAY (q) B. q ← 0 REPEAT 25 TIMES { a ← RANDOM(1,6) b ← RANDOM(1,6) IF (a = 7) { q ← q + 1 } } DISPLAY (q) C. q ← 0 REPEAT 25 TIMES { a ← RANDOM(2,12) IF (a = 7) { q ← q +1 } } DISPLAY (q) D. q ← 0 REPEAT 25 TIMES { a ← 0 REPEAT 2 TIMES { a ← a + RANDOM(1,6) } IF (a = 7) { q ← q + 1 } } DISPLAY (q)

CHOICES A & D ARE CORRECT: In Choice 'A', there are two random numbers chosen and added together. That sum is stored in a, then the program checks to see if a is equal to 7. In Choice 'D', there is a REPEAT loop that simulates the rolling of two dice. Again, the sum of the two simulated dice rolls is stored in a and subsequently checked to see if it is equal to 7. In both Choices 'A' and 'D', the variable q is used to store the total number of 7s found. At the end of both, they display the value of q, that is, the the total number of 7s rolled on the simulated dice. Explanation of Distractors In Choice 'B', though two dice are rolled, the scores are never added together. In Choice 'C', you need to roll two dice and add the results. Finding a random number between 2 and 12 does not have the same distribution as rolling finding two numbers between 1 and 6 and adding them together.

31. (Difficult) Random Odd Number Generation: A programmer is asked to write code to generate and display a random odd number between 1 and 1,000. Which of the following code segments will always successfully display a random number that is odd? Select TWO answers. A. num <-- 2 REPEAT UNTIL ( (num MOD 2) = 1 ) { num <-- RANDOM (1, 1000) } DISPLAY ( num ) B. num <-- RANDOM (1, 1000) IF ( (num MOD 2) = 1 ) { DISPLAY ( num ) } C. REPEAT UNTIL ( ((RANDOM (1, 1000)) MOD 2) = 1 ) { num = 0 } DISPLAY ( RANDOM (1, 1000) ) D. oddNum <-- false REPEAT UNTIL ( oddNum ) { num <-- RANDOM (1, 1000) IF ( (num MOD 2) = 1 ) { oddNum <-- true } } DISPLAY ( num )

CHOICES A & D ARE CORRECT: Note that num MOD 2 evaluates to the remainder when num is divided by 2, so num MOD 2 returns 1 if num is an odd number. Also, RANDOM (1, 1000) evaluates to a random integer from 1 to 1,000 (both inclusive). Note that a REPEAT UNTIL loop checks the condition at the bottom of the loop to determine if the loop exits, so the loop always loops at least one time. Choice 'A' keeps looping until num is an odd number. Inside the loop num is assigned a random number from 1 to 1,000. If num is an odd number then the loop ends, but if num is an even number the loop continues and num is once again assigned a random number from 1 to 1,000. The loop only ends when num is an odd number between 1 and 1,000. At that point, num is displayed. Choice 'D' begins with a boolean variable oddNum given a value of false. The loop keeps looping until oddNum is true. Inside the loop num is assigned a random number from 1 to 1,000. If num is an odd number, then oddNum is given a value of true and the loop ends. If num is an even number, the loop continues and num is once again assigned a random number from 1 to 1,000. The loop only ends when num is an odd number between 1 and 1,000 and oddNum is given a value of true. At that point, num is displayed. Explanation of Distractors: Choice 'B' is an incorrect answer. The variable num is assigned a random number from 1 to 1,000. If num is an odd number, then num is displayed. However, if num was assigned an even number, then nothing is displayed. This segment of code would not always display a random number between 1 and 1,000 that is odd. Choice 'C' is an incorrect answer. At the bottom of the loop, the statement ((RANDOM (1, 1000)) MOD 2) = 1 is evaluated and if it is true then the loop ends. Therefore, the loop ends if a random number between 1 and 1,000 is generated that is odd. If the random number is even, the loop continues. The statement inside the loop is of no consequence in this case. When an odd number is generated and the loop ends, the statement DISPLAY ( RANDOM (1, 1000) ) generates a new random number and displays it. The end result is that the random number tested in the loop is not the same as the random number being displayed. Therefore, this code is not guaranteed to display an odd number.

7. (Easy) Two Are True: Which of the following statements are TRUE? Select TWO answers. A. Every programming problem can be solved. B. Approximate solutions can be used when a problem takes too long to solve. C. A heuristic should only be used to solve a problem if an approximate solution is acceptable. D. Heuristics are always helpful.

CHOICES B & C ARE CORRECT: Choice 'B' is correct; optimization problems such as "find the smallest" or "find the best" often cannot be solved in a reasonable amount of time. In these cases, an approximation can be used to find the answer. Choice 'C' is also correct; a heuristic is essentially a shortcut that leads to an approximate solution to a problem. They can be very useful provided an approximate answer is acceptable. Explanation of Distractors: Choice 'A' is incorrect; there are some problems that cannot be solved. In fact, there are lists online that outline some of the unsolvable computer programming problems! Choice 'D' is also incorrect; when a problem can be solved using typical methods, a heuristic is not helpful or necessary.

4. (Easy) Pick a Function You observe that function, when given an input of 2 returns an output of 4. An input of 4 returns an output of 16. Input Output 2 4 4 16 Which of the following are possible mathematical representations of this function? Select TWO answers. A. a(x) = 2 x B. b(x) = 2^x C. c(x) = x^2 D. d(x) = 4 / x

CHOICES B & C ARE CORRECT: In both cases, an input of 2 yields an output of 4 and an input of 4 yields an output of 16: b(x) = 2^x c(x) = x^2 b(2) = 2^2 c(2) = 22 b(2) = 4 c(2) = 4 b(4) = 2^4 c(4) = 4^2 b(4) = 16 c(4) = 16 Explanation of Distractors Choice 'A' does not work with the input of 4; that is, twice four is not sixteen. Choice 'D' does not work on either input. a(x) = 2 x d(x) = 4 / x a(2) = 2 × 2 d(2) = 4 / 2 a(2) = 4 d(2) = 2 ≠ 4 a(4) = 2 × 4 d(4) = 4 / 4 a(4) = 8 ≠ 16 d(4) = 1 ≠ 16

5. (Easy) Is This Reasonable?: Which of the following are true statements about run time? Select TWO answers. A. There is always a way to solve a problem in a reasonable amount of time. B. Reasonable run time is determined by counting the number of steps an algorithm takes to solve a problem. C. Two algorithms can solve the same problem using very different run times. D. Reasonable run time can be defined by individuals writing or using a program, and thus might vary from person to person.

CHOICES B & C ARE CORRECT: Programmers determine whether or not a runtime is "reasonable" by counting the number of steps an algorithm takes to solve a problem. This makes Choice 'B' correct. Choice 'C' is also correct; it is possible for two programmers to solve a problem using different algorithms, and it's possible for those algorithms to vary in the number of steps. Choice 'A' is not correct; there are problems that cannot be solved in a reasonable amount of time and might require heuristics to help the process. Choice 'D' is also incorrect; reasonable runtime is determined using the method described in Choice 'B'. Programmers cannot define it themselves.

27. (Difficult) Factorial in Simple Language: A factorial is a mathematical function that returns the product of the given counting number and all the counting numbers leading up to it. Counting numbers are integers greater than zero. A factorial is often indicated with an exclamation point (!). In math class you may have defined 0! =1., however, we are not worried about that situation in this problem. For example: 3! = 3 * 2 * 1 = 6 5! = 5 * 4 * 3 * 2 * 1 = 120 Which of the following properly defines a factorial function for a counting number n? Select TWO answers. A. DEFINE factorial( n ){ IF n < 1 { RETURN (1) } RETURN (n * factorial( n ) ) } B. DEFINE factorial( n ){ product ← 1 REPEAT n TIMES { product = n * product } RETURN (product) } C. DEFINE factorial( n ){ IF n < 1 { RETURN (1) } RETURN (n * factorial( n - 1 ) ) } D. DEFINE factorial( n ){ product ← 1 x ← n REPEAT n TIMES { product = x * product x ← x - 1 } RETURN (product) }

CHOICES C & D ARE CORRECT: Choices 'A' and 'C' use recursion rather than a loop. The issue is making sure the function subtracts one in the correct place. The function needs to subtract one before making the recursive call. Choice 'C' is correct because it subracts one from n before each recursive call. Choices 'B' and 'D' use a loop. The function needs to decrease the number by which it is multiplying by one in each iteration of the loop. Choice 'D' is correct because the value of n is decremented in each iteration of the loop. Explanation of Distractors Choice 'A' is incorrect because does not subtract one each time, resulting in an infinite recursive call. Choice 'B' is incorrect because it does not subtract one in each iteration of the loop. It returns n^n.


Conjuntos de estudio relacionados

Eating Disorders, Somatoform Disorders, and Factitious

View Set

PassPoint - Foundations of Pychiatric Nursing

View Set