AP CSP Unit 7
A computer science student completes a program and asks a classmate for feedback. The classmate suggests rewriting some of the code to include more procedural abstraction. Which of the following is NOT a benefit of procedural abstraction? A Making the code more readable B Making the code run faster C Providing more opportunities for code reuse D Reducing the amount of duplicated code
b
3. What will be printed to the console after this program runs? (Lesson 2) var numbers = [ 2, 5, 3, 1, 6 ] function changeNums (numList, addNum, subtractNum) for (var i = 0; i < numList.length ; i ++) { if(numList[i] % 3 == 0) { numList[i] = numList[i] + addNum ; ) else { numList[i] = numList[i] = subtractNum; } changeNums( numbers, 3, 2) console.log(numbers)
0 3 6 -1 9
Based on this API how many strings are imputed to calculate the birth month? (lesson 5) // calculate birth month based on the day of the month, day of the week, and the birth year // dayMonth {number} - a day of a month from 1 to 31 // dayWeek {string} - the name of the day of the week // year {number} - the birth year // return {string} - the month you were born BirthdayLibrary.birthMonth(dayMonth, dayWeek, year);
1
What is printed to the console? (Lesson 3) console.log(15 % 4);
3
specifications for how the functons in a lbrary behave and can be used.
API
An application program interface (API) provides a procedure Max, which returns the greater of its two integer arguments. A programmer would like to find the greatest of three integer values , , and . Which of the following expressions will produce the desired result in every case? A B C D
a
Consider the following procedure. PROCEDURE doSomething(num1, num2) { DISPLAY(num1) RETURN(num1) DISPLAY(num2) } Consider the following statement. DISPLAY(doSomething(10, 20)) What is displayed as a result of executing the statement above? A 10 10 B 10 20 C 10 10 20 D 10 20 10
a
Dividing a program into separate subprograms (such as libraries) is known as:a. modularityb. iterationc. APId. documentation
a
What is one of the benefits of using a library in a program?a. simplifies creating a complex programb. increases development timec. removes all testingd. reduces abstractions in the program
a
The following spinner is used in a game. The region labeled "blue" represents 1414 of the spinner. The regions labeled "orange" and "purple" are equal in size. Which of the following code segments can be used to simulate the behavior of the spinner? Select two answers. A B C D
a and d
The following table shows the value of expression based on the values of input1 and input2. Value of input1Value of input2Value of expressiontruetruefalsetruefalsetruefalsetruetruefalsefalsetrue Which of the following expressions are equivalent to the value of expression as shown in the table? Select two answers. A (NOT input1) OR (NOT input2) B (NOT input1) AND (NOT input2) C NOT (input1 OR input2) D NOT (input1 AND input2)
a and d
Which of the following are ways in which a programmer can use abstraction to manage the complexity of a program? Select two answers. A Replacing each instance of repeated code with a call to a procedure B Replacing longer variable names with shorter variable names to reduce typing errors C Replacing several lines of documentation with a single line of documentation D Replacing several lines of documentation with a single line of documentation , , , and with a list of stringscalled
a and d
Write a piece of code using the MOD operator to return "true" if a number is even. (Lesson 3)
a. num % 2=0
the value passed to the parameter
argument
A programmer notices the following two procedures in a library. The procedures do similar, but not identical, things. Procedure MaxTwo (x, y) returns the greater of its two integer parameters. Procedure MaxThree (x, y, z) returns the greatest of its three integer parameters. Which of the following procedures is a generalization of the MaxTwo and MaxThree procedures? A Procedure Min (x, y), which returns the lesser of its two integer parameters B Procedure Max (numList), which returns the maximum value in the list of integers numList C Procedure MaxFour (w, x, y, z), which returns the greatest of its four integer parameters D Procedure OverMax (numList, max), which returns the number of integers in numList that exceed the integer value max
b
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? 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.
b
The procedure Draw (length, direction) is used to draw a line segment length units long in a given direction (left, right, up, or down), starting at the current cursor position. The cursor is then repositioned at the end of the line segment that was drawn. Consider the following program, where the cursor starts in the upper left corner of a grid of dots. The dots are spaced one unit apart. Draw (1, right) Draw (2, down) Draw (1, left) Draw (1, right) Draw (1, up) Draw (1, left) Which of the following represents the figure that is drawn by the program? A B C D
b
A student is developing a program that allows users to look up the definitions of words that appear in a book. The student plans to perform a large number of searches through a dictionary containing words and their definitions. The student will use a procedure written by a computer scientist to quickly search the dictionary (and knows that the procedure will return a definition if one is available). The student cannot modify the search procedure written by the computer scientist but can call the procedure by supplying a word. Which of the following is a true statement about the student's use of the computer scientist's search procedure? A The student is changing the search procedure's internal abstractions. B The student is modifying the search procedure to take a definition as an argument and return the corresponding word. C The student is reusing the computer scientist's procedural abstraction by knowing what the procedure does without knowing how it does it. D The student is reusing the computer scientist's procedural abstraction by using duplicate code each time a search needs to occur.
c
Algorithms can be created in all the following ways EXCEPT:a. creating from an ideab. combining existing algorithmsc. removing sequencing, selection, and iteration from an algorithmd. modifying existing algorithms
c
Consider the following code segment. Which of the following initial values of the variable y would result in the variable z being set to 2 after the code segment is executed? A 1 B 2 C 3 D 4
c
In the procedure Mystery below, the parameter number is a positive integer. Which of the following best describes the result of running the procedure Mystery? A The procedure returns true when the initial value of number is 2, and it otherwise returns false. B The procedure returns true when the initial value of number is greater than 2, and it otherwise returns false. C The procedure returns true when the initial value of number is even, and it otherwise returns false. D The procedure returns true when the initial value of number is odd, and it otherwise returns false.
c
Which of the following are benefits of procedural abstraction? Select two answers. A Procedural abstraction prevents programmers from accidentally using the intellectual property of other programmers. B Procedural abstraction eliminates the need for programmers to document their code. C Procedural abstraction makes it easier for people to read computer programs. D Procedural abstraction provides an opportunity to give a name to a block of code that describes the purpose of the code block.
c and d
Which of the following is NOT true about procedural abstraction?a. Procedural abstraction improves code readabilityb. Procedural abstraction manages complexity by allowing for code reusec. Procedural abstraction improves the speed at which a program executesd. Procedural abstraction allows a solution to a large problem to be based on the solution of smaller subproblems
c. procedural abstraction improves the speed at which a program executes
A programmer notices the following two procedures in a library. The procedures do similar, but not identical, things. Procedure returns the value . Procedure returns the value . Which of the following procedures is a generalization of the procedures described above? A Procedure , which returns the value n + m B Procedure , which returns the value C Procedure , which returns the value D Procedure , which returns the value
d
A student wrote the procedure below, which is intended to ask whether a user wants to keep playing a game. The procedure does not work as intended. Which of the following best describes the result of running the procedure? A The procedure returns when the user inputs the value and returns otherwise. B The procedure returns when the user inputs the value and returns otherwise. C The procedure returns no matter what the input value is. D The procedure returns no matter what the input value is.
d
Consider the following code segment. What is displayed as a result of executing the code segment? A 10 20 30 40 B 21 30 40 50 C 21 40 30 40 D 21 40 30 50
d
Consider two lists of numbers called list1 and list2. A programmer wants to determine how many different values appear in both lists. For example, if list1 contains [10, 10, 20, 30, 40, 50, 60] and list2 contains [20, 20, 40, 60, 80], then there are three different values that appear in both lists (20, 40, and 60). The programmer has the following procedures available. Procedure CallExplanationCombine (myList1, myList2)This procedure creates a new list containing the elements from myList1 followed by the entries from myList2. The resulting list is returned. For example, if myList1 contains [2, 4, 6] and myList2 contains [1, 5], the procedure will return the list [2, 4, 6, 1, 5].RemoveAllDups (myList)This procedure creates a new list containing the elements of myList with any duplicate values removed. The resulting list is returned. For example, if myList contains [3, 2, 4, 2, 2, 5, 6, 4], the procedure will return the list [3, 2, 4, 5, 6]. Which of the following can be used to assign the intended value to count ? A bothList ←← Combine (list1, list2) uniqueList ←← RemoveAllDups (bothList) count ←← LENGTH (bothList) - LENGTH (uniqueList) B newList1 ←← RemoveAllDups (list1) newList2 ←← RemoveAllDups (list2) bothList ←← Combine (newList1, newList2) count ←← LENGTH (list1) + LENGTH (list2) - LENGTH (bothList) C newList1 ←← RemoveAllDups (list1) newList2 ←← RemoveAllDups (list2) bothList ←← Combine (newList1, newList2) count ←← LENGTH (newList1) + LENGTH (newList2) - LENGTH (bothList) D newList1 ←← RemoveAllDups (list1) newList2 ←← RemoveAllDups (list2) bothList ←← Combine (newList1, newList2) uniqueList ←← RemoveAllDups (bothList) count ←← LENGTH (bothList) - LENGTH (uniqueList)
d
The code fragment below is intended to display "odd" if the positive number num is odd. Which of the following can be used to replace <MISSING CONDITION> so that the code fragment will work as intended? A (num MOD 1) = 0 B (num MOD 1) = 1 C (num MOD 2) = 0 D (num MOD 2) = 1
d
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.
d
Using existing algorithms as building blocks for new algorithms has all the following benefits EXCEPT:a. reduces development timeb. reduces testingc. simplifies debuggingd. removes procedural abstraction
d
Which call of the function correctly follows the instructions laid out in the API?a. moveElement("button1", 2, 23);b. moveElement("button1", "left", "thirty");c. moveElement("button1", 30, "two");d. moveElement("button1", "down", 25);
d
Write 3 function calls that would provide the most helpful test of this function (lesson 7) function findMin (num1, num2) if (num1< num2){ return num1 } else { return num2 }
findMin(-1,1) findMin(1,-1) findMin(1,1)
a group of functions(procedures) that may be used in creating new programs
library
This function finds the minimum number in a list. What should <MISSING CODE SEGMENT> be replaced with in order for this function to operate as expected? (lesson 3) function min(numList){ var min = numList[0]; for(var i=0; i<numList.length; i++){ if(numList[i] < min){ <MISSING CODE SEGMENT> } } return min;
min= numList[i]
the subdivision of a computer program into seperate subprograms
modularity
Here is the API for a robot library. (Lesson 6) // moves the robot forward function moveForward(); // turns the robot to the left function rotateLeft(); // turns the robot to the right function rotateRight(); // checks if a robot can move in any direction // direction {string} - the direction to be checked // return {Boolean} - true if the robot can move in that direction, otherwise returns false function canMove(direction); Write a code segment that will guarantee that the robot makes it to the gray square without hitting a wall or a barrier (black square)
move forward move forward rotate right
Where should return false; be written in the code? (lesson 2 & 3) function checkVowel(character){ var vowels = ["a", "e", "i", "o", "u"]; for(var i=0; i<vowels.length; i++){ if(vowels[i] == character){ return true; OPTION A } OPTION B } OPTION C } OPTION D
option c
a variable in a function definition. Used as a placeholder for values that will be passed through the function
parameter
extracting shared features to generalize functionality
procedural abstraction
used to return the flow of control to the point where the procedure(also known as a function) was called and to return the value of expression.
return
listAverage() returns the average number in a list - write a function that would correctly do this. (Lesson 3)
return is inside function, but under the blue block of code