APCSP_SemReview_Algorithms

.pdf

School

BASIS San Antonio Shavano *

*We aren’t endorsed by this school

Course

101

Subject

Computer Science

Date

Apr 26, 2024

Type

pdf

Pages

79

Uploaded by SuperBison2227 on coursehero.com

1. 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? (A) Appending a value to a list of elements, which requires no list elements be examined. (B) Finding the fastest route that visits every location among locations, which requires possible routes be examined. (C) Performing a binary search for a score in a sorted list of scores, which requires that fewer than scores be examined. (D) Performing a linear search for a name in an unsorted database of people, which requires that up to entries be examined. 2. Suppose a large group of people in a room were all born in the same year. Consider the following three algorithms, which are each intended to identify the people in the room who have the earliest birthday based on just the month and day. For example, a person born on February 10 is considered to have an earlier birthday than a person born on March 5. Which of the three algorithms will identify the correct people? I. All the people in the room stand up. All standing people form pairs where possible, leaving at most one person not part of a pair. For each pair, the person with the earlier birthday remains standing, while the other person in the pair sits down. If there is a tie, both people sit down. Any individual not part of a pair remains standing. Continue doing this until only one person remains standing. That person has the earliest birthday. II. All the people in the room stand up. All standing people form pairs with another standing person that they have not previously been paired with where possible, leaving at most one person not part of a pair. For each pair, the person with the earlier birthday remains standing, while the other person in the pair sits down. If there is a tie, both people in the pair remain standing. Any individual not part of a pair remains standing. Continue doing this until only one person remains standing or all persons standing have the same birthday. Anyone still standing has the earliest birthday. III. Beginning with the number 1, ask if anyone was born on that day of any month. Continue with the numbers 2, 3, and so on until a positive response is received. If only one person responds, that person has the earliest birthday. If more than one person responds, determine which person was born in the earliest month, and that person or those persons have the earliest birthday. (A) I only (B) II only (C) I and II (D) II and III AP COMPUTER SCIENCE PRINCIPLES Test Booklet Quiz - Big Idea 3 - Algorithms AP Computer Science Principles Page 1 of 79
3. A student is creating an algorithm to display the distance between the numbers num1 and num2 on a number line. The following table shows the distance for several different values. Value of num1 Value of num2 Distance Between num1 and num2 5 2 3 1 8 7 -3 4 7 Which of the following algorithms displays the correct distance for all possible values of num1 and num2 ? (A) Step 1: Add num1 and num2 and store the result in the variable sum . Step 2: Take the absolute value of sum and display the result. (B) Step 1: Subtract num1 from num2 and store the result in the variable diff . Step 2: Take the absolute value of diff and display the result. (C) Step 1: Take the absolute value of num1 and store it in the variable absNum1 . Step 2: Take the absolute value of num2 and store it in the variable absNum2 . Step 3: Add absNum1 and absNum2 and display the result. (D) Step 1: Take the absolute value of num1 and store it in the variable absNum1 . Step 2: Take the absolute value of num2 and store it in the variable absNum2 . Step 3: Subtract absNum1 from absNum2 and display the result. 4. A certain game keeps track of the maximum and minimum scores obtained so far. If num represents the most recent score obtained, which of the following algorithms correctly updates the values of the maximum and the minimum? Test Booklet Quiz - Big Idea 3 - Algorithms Page 2 of 79 AP Computer Science Principles
(A) If num is greater than the minimum, set the minimum equal to num . Otherwise, if num is greater than the maximum, set the maximum equal to num . (B) If num is less than the minimum, set the minimum equal to num . Otherwise, if num is greater than the maximum, set the maximum equal to num . (C) If num is less than the minimum, set the minimum equal to num . Otherwise, if num is less than the maximum, set the maximum equal to num . (D) If num is greater than the minimum, set the minimum equal to num . Otherwise, if num is less than the maximum, set the maximum equal to num . 5. The figure below shows four grids, each containing a robot represented as a triangle. The robot cannot move to a black square or move beyond the edge of the grid. 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? Test Booklet Quiz - Big Idea 3 - Algorithms AP Computer Science Principles Page 3 of 79
(A) Step 1: Keep moving forward, one square at a time, until the square to the right of the robot is black. Step 2: Turn right and move one square forward. Step 3: Repeat steps 1 and 2 three more times. (B) 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. (C) Step 1: Move forward three squares. Step 2: Turn right and move one square forward. Step 3: If the square to the right of the robot is black, repeat steps 1 and 2. (D) Step 1: Move forward three squares. Step 2: Turn right and move one square forward. Step 3: If the square to the right of the robot is not black, repeat steps 1 and 2. 6. A programmer is creating an algorithm that will be used to turn on the motor to open the gate in a parking garage. The specifications for the algorithm are as follows. The gate should not open when the time is outside of business hours. The motor should not turn on unless the gate sensor is activated. The motor should not turn on if the gate is already open. Which of the following algorithms can be used to open the gate under the appropriate conditions? (A) Check if the time is outside of business hours. If it is, check if the gate sensor is activated. If it is, check if the gate is closed. If it is, turn on the motor. (B) Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is, check if the gate is open. If it is, turn on the motor. (C) Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is not, check if the gate is open. If it is not, turn on the motor. (D) Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is, check if the gate is open. If it is not, turn on the motor. Test Booklet Quiz - Big Idea 3 - Algorithms Page 4 of 79 AP Computer Science Principles
7. 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? (A) If the first number is greater than the last number, swap them. Then, if the first number is greater than the middle number, swap them. (B) 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. (C) 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 last number, swap them. (D) 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. Test Booklet Quiz - Big Idea 3 - Algorithms AP Computer Science Principles Page 5 of 79
8. Which of the following algorithms display all integers between 1 and 20, inclusive, that are not divisible by 3 ? Select two answers. (A) Step 1: Set x to 0. Step 2: Increment x by 1. Step 3: If x is not divisible by 3, then display x . Step 4: Repeat steps 2 and 3 until x is 20. (B) Step 1: Set x to 0. Step 2: If x is divisible by 3, then display x . Step 3: Increment x by 1. Step 4: Repeat steps 2 and 3 until x is greater than 20. (C) Step 1: Set x to 1. Step 2: If x is divisible by 3, then do nothing; otherwise display x . Step 3: Increment x by 1. Step 4: Repeat steps 2 and 3 until x is 20. (D) Step 1: Set x to 1. Step 2: If x is divisible by 3, then do nothing; otherwise display x . Step 3: Increment x by 1. Step 4: Repeat steps 2 and 3 until x is greater than 20. 9. A teacher has a goal of displaying the names of 2 students selected at random from a group of 30 students in a classroom. Any possible pair of students should be equally likely to be selected. Which of the following algorithms can be used to accomplish the teacher’s goal? Test Booklet Quiz - Big Idea 3 - Algorithms Page 6 of 79 AP Computer Science Principles
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help