Algorithm Quiz

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

1. Predict the output of the following code block list ← [27, 44, 13, 16, 29, 32, 55] list1 ← [0] FOR EACH item IN list { IF (item MOD 2 = 0) { APPEND(list1, item) } } DISPLAY(list1) (A) 0, 44, 16, 32 (B) 44, 16, 32 (C) 0, 27, 44, 13, 16, 29, 32, 55 (D)27, 44, 13, 16, 29, 32, 55

(A) 0, 44, 16, 32

An amusement park ride has height requirements given in the table below: A programmer is creating an algorithm to set the variable canRide to a Boolean value based on the information in the table. When the prospective rider can go on the ride, canRide is to have the value of true and false otherwise. The programmer uses the variable height for the height in feet of the prospective rider. The Boolean variable withAdult is true when the prospective rider is accompanied by an adult and false otherwise. Which of the following code segments correctly sets the value of canRide? (A) (B) (C) (D)

(B)

How many times will this function display "N is not yet equal to 64"? (A) 5 (B) 6 (C) 7 (D) 8

(B) 6

The following program is intended to count how many matched there are in a list. The procedure does not work as intended. match ← "cheese" PROCEDURE CountMatch(match,list) { counter ← 0 FOR EACH item IN list { IF item = match { counter ← 1 } } return counter } For which of the following values of list will CountMatch NOT return the intended value? (A) ["cheese", "bread", "milk"] (B) ["flour", "cheese", "milk", "cheese"] (C) ["eggs", "milk", "butter"] (D) ["carrots", "eggs", "cheese"]

(B) ["flour", "cheese", "milk", "cheese"]

Consider the following code segment. Which of the following replacements for < Missing code block > will result in an infinite loop? (A) k + 2 (B) k + 4 (C) k + 5 (D) k + 10

(B) k + 4

The code segment below is intended to swap the values of the variables first and second using a temporary variable, temp. Which of the following can be used to replace < MISSING CODE > so the code segment works as intended? (A) (B) (C) (D)

(C)

2. Consider the following program. Predict the output produced by this program. *check doc for photo* (A) 8 (B) 0 (C) 18 (D) 9

(C) 18

Consider the following program, which uses the variable a. What is displayed as a result of executing the program? (A) Apple (B) Orange (C) Pear (D) None of the above

(C) Pear

Consider the following program: *check doc* Which of the following replacements for missing condition results in an infinite loop? (A) j MOD 3 = 0 (B) j MOD 5 = 0 (C) j MOD 4 = 0 (D) j MOD 7 = 0

(C) j MOD 4 = 0

Consider the following program: Which of the following cannot be a possible value for B when the code finishes? (A) 24 (B) 47 (C) 55 (D) 79

(D) 79

The program below prints report cards for Marymount High School. The procedure PrintRun (month, year) takes 30 minutes to run. All other program steps can be assumed to happen nearly instantaneously. Assume that todayMonth and todayYear are the current month and year. FOR EACH year IN [2017, 2018] { FOR EACH month IN ["January", "April", "July", "October"] { PrintRun (month, year) } } PrintRun (todayMonth, todayYear) Which of the following best approximates the amount of time it takes the program to execute? 30 minutes 180 minutes 240 minutes 270 minutes

270 minutes

4. As a computer science student, you are writing a program to help your administration search for a student from a list of students in your school. Assume the student records in the list are not sorted. Which statement is true regarding your choice of program algorithm? A linear search algorithm would be more appropriate than a binary search algorithm because the list is not sorted. A binary search algorithm would be more appropriate because it is more efficient than a linear search algorithm in all cases. A binary search algorithm would be more appropriate than a linear one because it is more efficient for a large list. You may use either the linear search or the binary search algorithm because both will yield correct results.

A linear search algorithm would be more appropriate than a binary search algorithm because the list is not sorted.

Consider the following scenarios. Which of the following would include both Selection and Iteration? A teacher trying to assign an ID number to each student in the class of 25 students. A teacher trying to find the tallest student in the class of 25 students by arranging the heights of the students in ascending order. A teacher trying to find the average height of the students in the class of 25 students by adding all the student heights and dividing by 25. A teacher trying to select a male student randomly from the class of 25 students.

A teacher trying to find the tallest student in the class of 25 students by arranging the heights of the students in ascending order.

An online retailer uses two different algorithms to sort a list of n items by price. The table below shows the approximate number of steps it takes each algorithm to sort a list of size n. Based on the values in the table, which of the following best characterizes the algorithms for very large values of n? Algorithm A runs in a reasonable time, but Algorithm B does not. Algorithm B runs in a reasonable time, but Algorithm A does not. Both Algorithms run in a reasonable time. Neither algorithm will run in a reasonable time.

Algorithm A runs in a reasonable time, but Algorithm B does not.

A high school guidance counselor is accessing the online database of available scholarships within the state where the school is located. The database contains the following information: - Date when the scholarship is awarded - The amount of scholarship awarded - Name of the scholarship or organization The counselor is looking for other scholarships that are available to the students from a certain organization within a certain date. Which of the following algorithms can be used to find all the scholarships that are offered by a specific organization on a specific date? Make a new list by filtering the data so only the scholarships from a specific organization are added to the list. Then perform multiple binary searches to find all the scholarships that are offered on a specific date, adding each new scholarship that meets the search criteria to the final list. Make a new list by filtering the data so only the scholarships from a specific organization are added to the list, then perform multiple linear searches to find all the scholarships that are offered on a specific date, adding each new scholarship that meets the criteria to the final list. Both algorithms work correctly to find all the scholarships offered by a specific organization on a specific date. Algorithm I always works correctly, but Algorithm II only works correctly when the data concerning the date is sorted. Algorithm II always works correctly, but Algorithm I only works correctly when the data concerning the date is sorted. Neither algorithm will correctly find all the scholarships offered by a specific organization on a specific date.

Algorithm II always works correctly, but Algorithm I only works correctly when the data concerning the date is sorted.

8. Which statement correctly distinguishes between linear search and binary search? Linear search is faster for long lists of items but requires that the list of items be sorted in advance. Linear search is faster for long lists of items, while binary search is more suitable for short lists of items. Binary search is slower for shorter lists but has the advantage of not requiring the list items to be sorted in advance. Binary search is faster for long lists of items but requires that the list items be sorted in advance.

Binary search is faster for long lists of items but requires that the list items be sorted in advance.

10. Which of the following is NOT true of algorithms? Some problems cannot be solved using any algorithm. Every decidable problem can be solved by an algorithm in a reasonable (polynomial) time. Every algorithm can be constructed using only sequencing, selection, and iteration. The language used to express an algorithm does not affect whether an algorithmic solution exists.

Every decidable problem can be solved by an algorithm in a reasonable (polynomial) time.

Consider the following statements about algorithms. Choose the statement that is NOT true. Algorithms are a tool for solving well defined computational problems. Some problems cannot be solved using any algorithms. An algorithm may use a heuristic to find an approximate solution when an exact solution cannot be found. For any particular problem, there is one and only one algorithm that can solve it correctly.

For any particular problem, there is one and only one algorithm that can solve it correctly.

Predict the output of the following code block scores ← [99, 67, 65, 78, 85] highScores ← [] FOR EACH s IN scores { < MISSING STATEMENTS > } Which of the following replacements for < MISSING STATEMENTS > will result in the list of highScores having all elements with values greater than or equal to 70? IF ( s >= 70) { APPEND(highScores, s) } IF ( scores >= 70) { APPEND(highScores, scores) } IF ( s >= 70) { APPEND(highScores, scores) } I only II only III only I and III

I only

A student is writing a program to solve a problem. She decides to draw a flowchart to visually represent the algorithm. The flowchart uses the oval block to represent the start or end of the algorithm, a diamond to represent the conditional or decision step which determines the execution path of the algorithm, a rectangle to represent one or more processing steps, and a parallelogram to represent an input statement. Which of the following statements is equivalent to the algorithm in the flowchart? IF ((number MOD 2 = 0) OR (number MOD 3 = 0)) { DISPLAY ("Number is divisible by 6") } IF ((number MOD 2 = 0) AND (number MOD 3 = 0)) { DISPLAY ("Number is divisible by 6") } IF ((number MOD 2 = 0) OR (number MOD 3 = 0)) { DISPLAY ("Number is divisible by 6") } IF ((number MOD 2 = 0) OR (number MOD 3 = 0)) { DISPLAY ("Number is divisible by 6") }

IF ((number MOD 2 = 0) AND (number MOD 3 = 0)) { DISPLAY ("Number is divisible by 6") }

The grid below shows a robot represented as a triangle, initially facing up. The robot can move into a white or gray square but cannot move into a black region. The code segment below uses the procedure ReachedGraySquare(), which evaluates to true if the robot is in the gray square and evaluates to false otherwise. REPEAT UNTIL (ReachedGraySquare()) { < MISSING CODE > } Which of the following replacements for < MISSING CODE > can be used to move the robot to the gray square? IF (CAN_MOVE (right)) { ROTATE_RIGHT () } MOVE_FORWARD() IF (CAN_MOVE (left)) { ROTATE_LEFT () } MOVE_FORWARD() IF (CAN_MOVE (right)) { ROTATE_RIGHT () } IF (CAN_MOVE (left)) { ROTATE_LEFT () } MOVE_FORWARD() IF (CAN_MOVE (forward)) { MOVE_FORWARD () } ROTATE_RIGHT()

IF (CAN_MOVE (right)) { ROTATE_RIGHT () } IF (CAN_MOVE (left)) { ROTATE_LEFT () } MOVE_FORWARD()

Consider the following program code. The intent of the code is to display the number of years it will take for a population to exceed 10000. However, the code does not work as intended. population ← INPUT() years ← 1 REPEAT UNTIL (population > 10000) { population ← population * 2 } DISPLAY years Which of the following changes to the program would make the program run as intended? Initialize population to 0 before entering the loop Initialize population to 0 as the first line of the loop Insert the code years ← years + 1 within the loop I only II only III only I and III

III only

Predict the output of the following code block scores ← [99, 67, 65, 78, 85] i ← 0 FOR EACH s IN scores { < MISSING STATEMENTS > } Which of the following replacements for < MISSING STATEMENTS > will result in the list of scores having all elements with values less than 70 removed. IF (scores[s] < 70) { REMOVE(scores, s) } IF (scores[i] < 70) { REMOVE(scores, i) } i ← i + 1 i ← i + 1 IF (scores[i] < 70) { REMOVE(scores, i) i ← i - 1 } I only II only III only I and III

III only

7. What does the binary sequence 1010 1101 1101 represent in a computer? The hexadecimal number ADD (decimal 2781) A variable named ADD The ADD instruction It cannot be determined. It depends on the context in which it is used.

It cannot be determined. It depends on the context in which it is used.

An AI program has been written for a self-driving car to navigate intersections. Assume that red and pedestrians are Boolean variables. The car should proceed through the intersection only if the light is not red and there are no pedestrians in the cross-walk. Which of the following could replace <MISSING EXPRESSION> so that the code segment works as intended? Select two answers. IF (<MISSING EXPRESSION>) { ProceedThruIntersection(); } NOT red OR NOT pedestrians NOT (red OR pedestrians) NOT red AND NOT pedestrians Red OR NOT pedestrians

NOT (red OR pedestrians) NOT red AND NOT pedestrians

6. What is the difference between the encryption and decryption key in a symmetric key encryption system? The decryption key is the reverse of the encryption key. The decryption key is longer than the encryption key. The decryption key is shorter than the encryption key. The keys are identical.

The keys are identical.

9. Complete the following sentence with the best pair of word choices. A(n) ______________ is a set of steps we follow to complete a task while a(n) ____________ is a procedure intended to provide a reasonable, though not necessarily best, solution. heuristic, algorithm algorithm, heuristic abstraction, heuristic heuristic, abstraction

algorithm, heuristic

5. Trudy is writing a program to input student grades from a particular test. It will also use the variable count to keep track of the number of times a user enters the particular score of 85. Of the steps below, which should appear earliest in her pseudocode? count <- 0 grade <- INPUT() count <- count + 1 IF (grade = 85)

count <- 0

3. Which of the following operations performed by an algorithm would most likely use both selection and iteration? In a hand of number cards: determine the number of cards with the suit equal to hearts. compute the average value of the cards. determine the number of cards in the hand. compute the sum of the cards' value.

determine the number of cards with the suit equal to hearts.

You write a procedure DrawCircle(x, y, r) to draw a circle on the coordinate grid. The center of the circle is located at the coordinate (x, y) and has a radius of r units. You want to use the procedure to draw the image as shown below. Which of the following code segments can be used to draw the figure? xPos ← 3 yPos ← 3 REPEAT 3 TIMES { DrawCircle (xPos, yPos, 3) xPos ← xPos + 6 } xPos ← 3 yPos ← 3 REPEAT 3 TIMES { DrawCircle (xPos, yPos, 3) yPos ← yPos + 6 } xPos ← 3 yPos ← 3 REPEAT 3 TIMES { DrawCircle (xPos, yPos, 3) xPos ← xPos + 6 yPos ← yPos + 6 } xPos ← 3 yPos ← 3 REPEAT 3 TIMES { DrawCircle (xPos, yPos, 3) } xPos ← xPos + 6 yPos ← yPos + 6

xPos ← 3 yPos ← 3 REPEAT 3 TIMES { DrawCircle (xPos, yPos, 3) xPos ← xPos + 6 }


Ensembles d'études connexes

The verb Ir- to go/ ¿Adónde vas? Where are you going?

View Set

Lesson 23: Behavioral Security Concepts

View Set

#101-150 Physiological Integrity Practice Questions

View Set

Module 11 - Residential Electrical Services

View Set

PrepU Ch. 50: Diabetes Mellitus and the Metabolic Syndrome

View Set