Algortihims
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
How many times will this function display "N is not yet equal to 64"? (A) 5 (B) 6 (C) 7 (D) 8
(B) 6
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
2. Consider the following program. Predict the output produced by this program. (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: 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
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) second <- first (B) second <- temp (C) temp <- first (D) temp <- second
(C) temp <- first
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
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) A linear search algorithm would be more appropriate than a binary search algorithm because the list is not sorted. B) A binary search algorithm would be more appropriate because it is more efficient than a linear search algorithm in all cases. C) A binary search algorithm would be more appropriate than a linear one because it is more efficient for a large list. D) You may use either the linear search or the binary search algorithm because both will yield correct results.
A) A linear search algorithm would be more appropriate than a binary search algorithm because the list is not sorted.
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? I. IF ( s >= 70) { APPEND(highScores, s) } II. IF ( scores >= 70) { APPEND(highScores, scores) } III. IF ( s >= 70) { APPEND(highScores, scores) } A) I only B) II only C) III only D) I and III
A) I only
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? A) count <- 0 B) grade <- INPUT() C) count <- count + 1 D) IF (grade = 85)
A) 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: A) determine the number of cards with the suit equal to hearts. B) compute the average value of the cards. C) determine the number of cards in the hand. D) compute the sum of the cards' value.
A) determine the number of cards with the suit equal to hearts
6. Consider the following scenarios. Which of the following would include both Selection and Iteration? A) A teacher trying to assign an ID number to each student in the class of 25 students. B) A teacher trying to find the tallest student in the class of 25 students by arranging the heights of the students in ascending order. C) 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. D) A teacher trying to select a male student randomly from the class of 25 students.
B) A teacher trying to find the tallest student in the class of 25 students by arranging the heights of the students in ascending order.
10. Which of the following is NOT true of algorithms? A) Some problems cannot be solved using any algorithm. B) Every decidable problem can be solved by an algorithm in a reasonable (polynomial) time. C) Every algorithm can be constructed using only sequencing, selection, and iteration. D)The language used to express an algorithm does not affect whether an algorithmic solution exists.
B) Every decidable problem can be solved by an algorithm in a reasonable (polynomial) time.
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"]
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. A) heuristic, algorithm B) algorithm, heuristic C) abstraction, heuristic D) heuristic, abstraction
B) algorithm, heuristic
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? I. 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. II. 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. A) Both algorithms work correctly to find all the scholarships offered by a specific organization on a specific date. B) Algorithm I always works correctly, but Algorithm II only works correctly when the data concerning the date is sorted. C) Algorithm II always works correctly, but Algorithm I only works correctly when the data concerning the date is sorted. D) Neither algorithm will correctly find all the scholarships offered by a specific organization on a specific date.
C) Algorithm II always works correctly, but Algorithm I only works correctly when the data concerning the date is sorted.
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? I. Initialize population to 0 before entering the loop II. Initialize population to 0 as the first line of the loop III. Insert the code years ← years + 1 within the loop A) I only B) II only C) III only D) I and III
C) 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. I. IF (scores[s] < 70) { REMOVE(scores, s) } II. IF (scores[i] < 70) { REMOVE(scores, i) } i ← i + 1 III. i ← i + 1 IF (scores[i] < 70) { REMOVE(scores, i) i ← i - 1 } A) I only B) II only C) III only D) I and III
C) III only
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(); } A) NOT red OR NOT pedestrians B) NOT (red OR pedestrians) C) NOT red AND NOT pedestrians D) Red OR NOT pedestrians
C) NOT red AND NOT pedestrians B) NOT (red OR pedestrians)
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? A) 30 minutes B) 180 minutes C) 240 minutes D) 270 minutes
D) 270 minutes
8. Which statement correctly distinguishes between linear search and binary search? A) Linear search is faster for long lists of items but requires that the list of items be sorted in advance. B) Linear search is faster for long lists of items, while binary search is more suitable for short lists of items. C) Binary search is slower for shorter lists but has the advantage of not requiring the list items to be sorted in advance. D) Binary search is faster for long lists of items but requires that the list items be sorted in advance.
D) Binary search is faster for long lists of items but requires that the list items be sorted in advance.
Consider the following statements about algorithms. Choose the statement that is NOT true. A) Algorithms are a tool for solving well defined computational problems. B) Some problems cannot be solved using any algorithms. C) An algorithm may use a heuristic to find an approximate solution when an exact solution cannot be found. D) For any particular problem, there is one and only one algorithm that can solve it correctly.
D) For any particular problem, there is one and only one algorithm that can solve it correctly.
7. What does the binary sequence 1010 1101 1101 represent in a computer? A) The hexadecimal number ADD (decimal 2781) B) A variable named ADD C) The ADD instruction D) It cannot be determined. It depends on the context in which it is used.
D) It cannot be determined. It depends on the context in which it is used.
6. What is the difference between the encryption and decryption key in a symmetric key encryption system? A) The decryption key is the reverse of the encryption key. B) The decryption key is longer than the encryption key. C) The decryption key is shorter than the encryption key. D) The keys are identical.
D) The keys are identical.