Comp Sci U6
A sorted list of numbers contains 500 elements. Which of the following is closest to the maximum number of list elements that will be examined when performing a binary search for a value in the list?
10
Which of the following expressions represents the value stored in the variable x as a result of executing the program?
2 * 3 * 3 * 3 * 3 why? The program initializes x to 2, then multiplies it by 3 a total of four times.
Which of the following best approximates the difference in execution time between running the two processes in parallel instead of running them one after the other on a single processor?
30 seconds why? Running the two processes one after the other on a single processor requires 30+45=7530+45=75 seconds. Running the two processes in parallel requires 45 seconds because if both processes start at the same time on different processors, process P will finish in 30 seconds and process Q will finish 15 seconds later at 45 seconds. Therefore, the amount of time saved by running the two processes in parallel is 75−45=3075−45=30 seconds.
Which of the following best approximates the amount of time it takes the program to execute?
5 hours why? Each call to the Analysis procedure requires one hour of program execution time. The procedure is called-once before the loop, and then four times inside the loop (once for each of the four entries in genreList). Therefore, the program will take approximately 5 hours to execute.
Which of the following best describes the behavior of the two programs?
Both program I and program II display the correct sum. Why? In the program I, the value of I starts at 1 and finishes at n, so that result stores the sum of 1 + 2 + 3 + ... + n. In program II the value of i starts at n and finishes at 1, so that result stores the sum of n + ( n - 1 ) + ... + 3 + 2 + 1. Both programs display the correct sum.
Which of the following changes will NOT affect the results when the code segment is executed?
Changing line 3 to b <-- 10 why? Line 3 is executed only if the Boolean expression evaluates to. In the current version of the code, the statement would result in the value of 10 being stored in the variable , since equals 0. Changing line 3 would not affect the result.
For which of the following situations would it be best to use a heuristic in order to find a solution that runs in a reasonable amount of time?
Finding the fastest route that visits every location among nn locations, which requires n!n! possible routes be examined. Why? As this algorithm has a factorial efficiency, it does not run in a reasonable amount of time. A heuristic approach can be used to find an approximate solution than can run in a reasonable amount of time.
IF(num > 0) { DISPLAY("positive") } IF(num < 0) { DISPLAY("negative") } IF(num = 0) { DISPLAY("zero") } Which of the following code segments is equivalent to the code segment above?
IF(num < 0) { DISPLAY("negative") } ELSE { IF(num = 0) { DISPLAY("zero") } ELSE { DISPLAY("positive") } } why? The given code segment displays "positive" when num is positive, displays "negative" when num is negative, and displays "zero" when num is 0. This code segment produces the same result. When num is negative, "negative" is displayed. Otherwise, when num is 0, "zero" is displayed. Otherwise, "positive" is displayed.
Line 1: count ←← 0 Line 2: REPEAT n TIMES Line 3: { Line 4: REPEAT 2 TIMES Line 5: { Line 6: MOVE_FORWARD() Line 7: } Line 8: ROTATE_RIGHT() Line 9: } Consider the goal of modifying the code segment to count the number of squares the robot visits before execution terminates. Which of the following modifications can be made to the code segment to correctly count the number of squares the robot moves to?
Inserting the statement count ←← count + 1 between line 6 and line 7 why? Inserting this statement between lines 6 and 7 increases the value of count once each time the robot moves forward, which keeps an accurate count of the number of squares the robot visits.
When the robot reaches the gray square, it turns around and faces the bottom of the grid. Which of the following changes, if any, should be made to the code segment to move the robot back to its original position in the bottom-left square of the grid and facing toward the bottom of the grid?
No change is needed; the algorithm is correct as is. why? In order for the robot to move from the gray square back to its original position, it must move forward two squares, turn right, move forward four squares, turn left, and move forward two squares. The same set of moves can be used in both directions, so no change is needed to the algorithm.
Which of the following best compares the values displayed by programs A and B?
Program A and program B display identical values in the same order. why? Program A initializes i to 1. Inside the loop, it prints i and then increments i. The loop terminates when i is greater than 10, which occurs after 10 is printed. Program A prints 1 2 3 4 5 6 7 8 9 10. Program B initializes i to 0. Inside the loop, it increments i and then prints i. The loop terminates when i equals 10, which occurs after 10 is printed. Program B prints 1 2 3 4 5 6 7 8 9 10.
Which of the following best compares the values displayed by programs A and B?
Program A and program B display the same number of values, but the values differ. Why? The programs each display ten values, but each value displayed by program B is one greater than the corresponding value from program A. Program A displays 1 2 3 4 5 6 7 8 9 10 and program B displays . 2 3 4 5 6 7 8 9 10 11
Which of the following parallel computing solutions would minimize the amount of time it takes to execute all four processes?
Running processes P and Q on one processor and processes R and S on the other processor why? With two processors running in parallel, execution time is minimized when the processors take on as close to an equal workload as possible. Running processes P and Q on one processor will take a total of 40 seconds. Running processes R and S on the other processor will take a total of 35 seconds. As the processors run in parallel, all four operations are completed in 40 seconds.
Which of the following best explains how algorithms that run on a computer can be used to solve problems?
Some problems cannot be solved by an algorithm. why? Undecidable problems cannot be solved with an algorithm.
Which of the following algorithms will allow the robot to make a single circuit around the rectangular region of black squares, finishing in the exact location and direction that it started in each of the four grids?
Step 1: Keep moving forward, one square at a time, until the square to the right of the robot is no longer black. Step 2: Turn right and move one square forward. Step 3: Repeat steps 1 and 2 three more times. Why? In all four grids, the robot starts along the side of a black region. Step 1 moves the robot just past the end of the black region. Step 2 rotates the robot to the right and moves it forward, placing the robot along the next side of the black region. Step 3 will repeat steps 1 and 2 to move the robot around the other 3 sides to complete a circuit around the black region.
Based on the values in the table, which of the following best characterizes the algorithm for very large values of n ?
The algorithm runs in reasonable time. why? The pattern in the table appears to indicate that there are steps for a list containing n items. This number of steps is a polynomial and therefore the algorithm runs in a reasonable time.
A certain computer game is played between a human player and a computer-controlled player. Every time the computer-controlled player has a turn, the game runs slowly because the computer evaluates all potential moves and selects the best one. Which of the following best describes the possibility of improving the running speed of the game?
The game's running speed might be improved by using a process that finds approximate solutions every time the computer-controlled player has a turn. why? Selecting the best move is an optimization problem that cannot be solved in a reasonable time based on the information that the game runs slowly. If the algorithm for selecting the best move is running too slowly, the game may run more quickly if a heuristic is used to find approximate solutions.
A large number of genetic codes are stored as binary values in a list. Which one of the following conditions must be true in order for a researcher to obtain the correct result when using a binary search algorithm to determine if a given genetic code is in the list?
The list must be sorted based on the genetic code values. why? In order for a binary search on a list to work as intended, the list must be sorted.
The procedure BinarySearch (numList, target) correctly implements a binary search algorithm on the list of numbers numList. The procedure returns an index where target occurs in numList, or -1 if target does not occur in numList. Which of the following conditions must be met in order for the procedure to work as intended?
The values in numList must be in sorted order. why? In order for a binary search on a list to work as intended, the list must be sorted.
What best explains the ability to solve problems algorithmically?
There exist some problems that cannot be solved algorithmically using any computer. Why? An undecidable problem is one for which no algorithm can be constructed that is always capable of providing a correct yes-or-no answer. Some instances of an undecidable problem may have an algorithmic solution, but there is no algorithmic solution that could solve all instances of the problem.
Which of the following best explains why it is not possible to use computers to solve every problem?
There exist some problems that cannot be solved using any algorithm. why? It can be proved that there exist some problems that cannot be solved with any algorithm. These problems cannot be solved computationally.
A team of programmers is designing software. One portion of the project presents a problem for which there is not an obvious solution. After some research, the team determines that the problem is undecidable. Which of the following best explains the consequence of the problem being undecidable?
There is no possible algorithm that can be used to solve all instances of the problem. why? An undecidable problem is one for which no algorithm can be constructed that is always capable of providing a correct yes-or-no answer. There is no algorithmic solution that can solve all instances of the problem.
The following code segment is used to move the robot in the grid.
count ←← 0 REPEAT 4 TIMES { count ←← count + 1 REPEAT count TIMES { MOVE_FORWARD() } ROTATE_LEFT() }
In the following code segment, assume that x and y have been assigned integer values. sum ←← 0 REPEAT x TIMES { REPEAT y TIMES { sum ←← sum + 1 } } At the end of which of the following code segments is the value of sum the same as the value of sum at the end of the preceding code segment?
sum ←← 0 z ←← x * y REPEAT z TIMES { sum ←← sum + 1 } sum ←← 0 REPEAT y TIMES { REPEAT x TIMES { sum ←← sum + 1 } }
Which of the following best approximates the minimum possible time to execute all three processes when the two processors are run in parallel?
80 seconds
Which of the following best describes the behavior of the program?
The program correctly displays the number of times target appears in the list. why? The variable count is initially set to 0 and is incremented only when the current item in the list (represented by the variable n) equals the value of the variable target.
Which of the following statements is true?
Both program I and program II correctly move the robot to the gray square. Why? The program I correctly moves the robot to the gray square by repeatedly moving the robot forward, rotating left, moving forward, and rotating right. Program II correctly moves the robot to the gray square by moving the robot forward to the upper-right corner of the grid, rotating left, and moving forward to the upper-left corner of the grid.
Which of the following statements best describes the correctness of the programs?
Both program I and program II correctly move the robot to the gray square. why? The program I correctly moves the robot to the gray square by repeatedly moving the robot forward, rotating left, moving forward twice, and rotating right. Program II correctly moves the robot to the gray square by moving the robot forward to the bottom right corner of the grid, rotating left, moving the robot forward to the upper right corner of the grid, rotating left, and moving forward to the gray square.
A student wants to determine whether a certain problem is undecidable. Which of the following will demonstrate that the problem is undecidable?
Show that for one instance of the problem, no algorithm can be written that is capable of providing a correct yes-or-no answer. why? A decidable problem is one for which an algorithm can be constructed to produce a correct output for all inputs. If, for one instance of the problem, this is not possible, then this problem cannot be decidable. Therefore, it must be undecidable.
A sorted list of numbers contains 128 elements. Which of the following is closest to the maximum number of list elements that can be examined when performing a binary search for a value in the list?
8 Why? The binary search algorithm starts at the middle of the list and repeatedly eliminates half the elements until the desired value is found or all elements have been eliminated. For a list with 128 elements, the list will be cut in half a maximum of 7 times (causing 8 elements to be examined). The list will start with 128 elements, then 64 elements, then 32 elements, then 16 elements, then 8 elements, then 4 elements, then 2 elements, then 1 element.
A sorted list of numbers contains 200 elements. Which of the following is closest to the maximum number of list elements that will need to be examined when performing a binary search for a particular value in the list?
8 Why? The binary search algorithm starts at the middle of the sorted list and repeatedly eliminates half the elements until the desired value is found or all the elements have been eliminated. For a list with 200 elements, the list will be cut in half a maximum of 7 times (with a total of 8 elements examined). The list starts with 200 elements, then is reduced to 100 elements, then to 50 elements, then to 25 elements, then to 12 elements, then to 6 elements, then to 3 elements, and then, finally, to 1 element.
Which of the following best describes the two code segments?
Both code segments display the correct average, but code segment I requires more arithmetic operations than code segment II. why? Both code segments display the correct average. Code segment I requires more arithmetic operations because it performs the operation within the loop, while code segment II performs the same operation only once.
A student wants to create an algorithm that can determine, given any program and program input, whether or not the program will go into an infinite loop for that input. The problem the student is attempting to solve is considered an undecidable problem. Which of the following is true?
It is not possible to create an algorithm that will solve the problem for all programs and inputs. why? An undecidable problem is one in which no algorithm can be constructed that always leads to a correct yes-or-no answer.
Consider the following algorithms. Each algorithm operates on a list containing n elements, where n is a very large integer. 1. An algorithm that accesses each element in the list twice 2. An algorithm that accesses each element in the list n times 3. An algorithm that accesses only the first 10 elements in the list, regardless of the size of the list Which of the algorithms run in a reasonable time?
I, II, and III why? For an algorithm to run in reasonable time, it must take a number of steps less than or equal to a polynomial function. Algorithm I accesses elements 2n2n times (twice for each of n elements), which is considered reasonable time. Algorithm II accesses n2n2 elements (n times for each of n elements), which is considered reasonable time. Algorithm III accesses 10 elements, which is considered reasonable time.
For which of the following lists can a binary search be used to search for an item in the list? I. ["blue", "green", "jade", "mauve", "pink"] II. [5, 5, 5, 5, 6, 7, 8, 8, 8] III. [10, 5, 3, 2, -4, -8, -9, -12]
I, II, and III why? In order for a binary search on a list to work, the list must be sorted. All three lists are sorted (the first one in alphabetical order, the third one in reverse order), so a binary search is appropriate for all of them.
Based on the values in the table, which of the following best characterizes the algorithm for drawing nn shapes, where nn is a very large number?
The algorithm runs in a reasonable amount of time because it will use approximately n2n2 steps to draw n shapes. why? As nn increases, the number of steps is approximately equal to n2n2, which would make the algorithm polynomial. An algorithm with an efficiency that approximates n2n2 is said to run in a reasonable amount of time.
The list listOne is a sorted list of numbers that contains 700 elements. The list listTwo is a sorted list of numbers that contains 900 elements. Let x represent the maximum number of list elements that will need to be examined when performing a binary search for a value in listOne, and let y represent the maximum number of list elements that will need to be examined when performing a binary search for a value in listTwo. Which of the following statements about x and y is true?
The value of x is approximately equal to the value of y. Why? The binary search algorithm starts at the middle of the list and repeatedly eliminates half of the elements until the desired value is found or all elements have been eliminated. For listOne, the list will be cut in half a maximum of 9 times, with a total of 10 elements needing to be examined. The list will start with 700 elements, then will be reduced to 350 elements, then to 175 elements, then to 87 elements, then to 43 elements, then to 21 elements, then to 10 elements, then to 5 elements, then to 2 elements, and then, finally, to 1 element. For listTwo, the list will also be cut in half a maximum of 9 times, with a total of 10 elements needing to be examined. The list will start with 900 elements, then will be reduced to 450 elements, then to 225 elements, then to 112 elements, then to 56 elements, then to 28 elements, then to 14 elements, then to 7 elements, then to 3 elements, and then, finally, to 1 element.
Which of the following statements is true?
There exist problems that no algorithm will ever be able to solve for all possible inputs. why? Some problems, such as determining if any program will eventually stop, cannot be solved by an algorithm.
Version I topScore ←← 0 idList ←← [1298702, 1356846, 8848491, 8675309] FOR EACH id IN idList { score ←← GetPrediction (id) IF (score > topScore) { topScore ←← score } } DISPLAY (topScore) Version II idList ←← [1298702, 1356846, 8848491, 8675309] topID ←← idList[1] FOR EACH id IN idList { IF (GetPrediction (id) > GetPrediction (topID)) { topID ←← id } } DISPLAY (GetPrediction (topID)) Which of the following best compares the execution times of the two versions of the program?
Version II requires approximately 5 more minutes to execute than version I.
A time stamp indicates the date and time that a measurement was taken. A data scientist has a list containing 10,000 time stamps, sorted in chronological order. Which of the following is closest to the maximum number of values that will need to be examined when performing a binary search for a value in the list?
15 Why? The binary search algorithm starts at the middle of the list and repeatedly eliminates half the elements until the desired value is found or all the elements have been eliminated. For a list with 10,000 elements, the list will be cut in half a maximum of 13 times, with the total of 14 elements needing to be examined. The list will start with 10,000 elements, then will be reduced to 5,000 elements, then to 2,500 elements, then to 1,250 elements, then to 625 elements, then to 312 elements, then to 156 elements, then to 78 elements, then to 39 elements, then to 19 elements, then to 9 elements, then to 4 elements, then to 2 elements, and then, finally, to 1 element. Of the given values, 15 is closest to 14.
Under which of the following conditions is it most beneficial to use a heuristic approach to solve a problem?
When the problem cannot be solved in a reasonable time and an approximate solution is acceptable
What best describes a challenge involved in using a parallel computing solution?
A parallel computing solution may not be appropriate for an algorithm in which each step requires the output from the preceding step. Why? The efficiency of a parallel computing solution is limited by the sequential portion of the solution. If each step is dependent on the preceding step, then each step must wait for the previous step to complete before executing. Therefore, the solution is completely sequential and does not benefit from parallel computing.
Which of the following programs is most likely to benefit from the use of a heuristic?
A program that finds the shortest driving route between two locations on a map why? Finding the shortest driving route is an optimization problem that cannot be solved in a reasonable time, and a heuristic is a technique that can find an approximate solution more quickly when exact methods are too slow.
Algorithm IGenerate all possible routes, compute their lengths, and then select the shortest possible route. This algorithm does not run in reasonable time. Algorithm IIStarting from an arbitrary delivery location, find the nearest unvisited delivery location. Continue creating the route by selecting the nearest unvisited location until all locations have been visited. This algorithm does not guarantee the shortest possible route and runs in time proportional to n2n2. Which of the following best categorizes algorithm II?
Algorithm II uses a heuristic approach to provide an approximate solution in reasonable time. why? Algorithm II runs in time proportional to n2n2, which is considered reasonable time because n2n2 is a polynomial. This is considered a heuristic approach because it finds an approximate solution in reasonable time when the technique that finds an exact solution (algorithm I) does not run in reasonable time.
Three different numbers need to be placed in order from least to greatest. For example, if the numbers are ordered 9, 16, 4, they should be reordered as 4, 9, 16. Which of the following algorithms can be used to place any three numbers in the correct order?
If the first number is greater than the middle number, swap them. Then, if the middle number is greater than the last number, swap them. Then, if the first number is greater than the middle number, swap them. Why? After comparing the first number and the middle number and potentially swapping them, and then comparing the middle number and the last number and potentially swapping them, the largest number will be in the third position. Next, after comparing the first number and the middle number and potentially swapping them, the three numbers will be ordered from least to greatest.
The programmer wants to reduce the number of operations that are performed when the program is run. Which change will result in a correct program with a reduced number of operations performed?
Interchanging line 7 and line 8 why? . In the current program, line 7 is performed for each value in the list numbers. If lines 7 and 8 were interchanged, the average would be calculated only once.
A program is used to assign processes to each of the processors. Which of the following describes how the program should assign the four processes to optimize execution time?
Processes W and Z should be assigned to one processor and processes X and Y should be assigned to the other processor. Why? Execution time is optimized when the workload of the two processors is as close to equal as possible so that one processor does not finish too early and has to wait for the other processor to finish. The closest-to-equal workloads are achieved by assigning processes W and Z to one processor (taking 70 seconds) and assigning processes X and Y to the other processor (taking 75 seconds). Using this solution, all four processes will finish executing in 75 seconds.
Suppose that a list of numbers contains values [-4, -1, 1, 5, 2, 10, 10, 15, 30]. Which of the following best explains why a binary search should NOT be used to search for an item in this list?
The elements of the list are not sorted. why? In order for a binary search on a list to work, the elements of the list must be sorted.
What best describes the ability of parallel computing solutions to improve efficiency?
can be broken down into parallel portions is still limited by a sequential portion. Why? Parallel computing solutions consist of a parallel portion and a sequential portion. When increasing the use of parallel computing in a solution, the efficiency of the solution is limited by the sequential portion. This means that at some point, adding parallel portions will no longer meaningfully increase efficiency.