ap collegeboard unit 3 3.12 and 3.13
Consider the following procedure.
Procedure
CallExplanationdrawCircle(xPos, yPos, rad)
Draws a circle on a coordinate grid with center (xPos, yPos) and radius rad
The drawCircle procedure is to be used to draw the following figure on a coordinate grid.
Let the value of the variable x be 2, the value of the variable y be 2, and the value of the variable r be 1. Which of the following code segments can be used to draw the figure?
drawCircle(x, y, r)
drawCircle(x, y + 3, r)
drawCircle(x + 3, y, r)
drawCircle(x + 3, y + 3, r)
Consider the following procedure.
drawLine(x1, y1, x2, y2) = Draws a line segment on a coordinate grid with endpoints at coordinates (x1, y1) and (x2, y2)
The drawLine procedure is to be used to draw the following figure on a coordinate grid.
Let the value of the variable xVal be 6 and the value of the variable yVal be 5. Which of the following code segments can be used to draw the figure?
A. drawLine(1, 5, xVal, yVal)
drawline(1, 5, xVal, yVal + 2)
drawline(1, 5, xVal, yVal + 2)
B. drawLine(1, 5, xVal, yVal)
drawline(1, 5, xVal, yVal + 2)
drawline(1, 5, xVal, yVal - 2)
C. drawLine(1, 5, xVal, yVal)
drawline(1, 5, xVal + 2, yVal + 2)
drawline(1, 5, xVal + 2, yVal - 2)
D. drawLine(1, 5, xVal, yVal)
drawline(1, 5, xVal + 2, yVal + 2)
drawline(1, 5, xVal - 2, yVal - 2)
B. drawLine(1, 5, xVal, yVal)
drawline(1, 5, xVal, yVal + 2)
drawline(1, 5, xVal, yVal - 2)
What is displayed as a result of the procedure call proc2("birthday", "to you") ?
to you happy birthday
Consider the following procedures, which are used to control a device that draws lines on paper.
Procedure CallExplanationpenDown()Places the device's pen on the paper so that a line is drawn when the device movespenUp()Lifts the device's pen off of the paper so that no line is drawn when the device movesgoForward(x)Moves the device forward x unitsturnRight(x)Rotates the device in place x degrees clockwise (i.e., makes an in-place right turn)
Consider the goal of using the device to produce the following drawing, where each line shown has a length of 10 units and the horizontal lines are 10 units apart.
The device is positioned at the upper-left corner of a sheet of paper and is facing right. Which of the following code segments will produce the drawing shown?
penDown()
goForward(10)
penUp()
turnRight(90)
goForward(10)
turnRight(90)
penDown()
goForward(10)
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?
10 10
In the following procedure, the parameter str is a string and the parameter num is a number.
PROCEDURE printArgs(str, num)
{
DISPLAY(num)
DISPLAY(str)
DISPLAY(num)
}
Consider the following code segment. printArgs("**", 1) printArgs("*", 2) What is displayed as a result of executing the code segment?
1 ** 1 2 * 2
Which of the following are benefits of procedural abstraction?
Procedural abstraction makes it easier for people to read computer programs.
Procedural abstraction provides an opportunity to give a name to a block of code that describes the purpose of the code block.
A computer program contains code in several places that asks a user to enter an integer within a specified range of values. The code repeats the input request if the value that the user enters is not within the specified range. A programmer would like to create a procedure that will generalize this functionality and can be used throughout the program. The correct use of the procedure is shown below, where min is the least acceptable value, max is the greatest acceptable value, and promptString is the string that should be printed to prompt the user enter a value.
inputVal ←← getRange(min, max, promptString)
Which of the following is a correct implementation of the getRange procedure?
PROCEDURE getRange(min, max, promptString)
{
DISPLAY(promptString)
x ←← INPUT()
REPEAT UNTIL(x ≥ min AND x ≤ max)
{
DISPLAY(promptString)
x ←← INPUT()
}
RETURN(x)
}
A student wrote the following procedure to calculate the sum of the integers from 1 to 5.
The student later decides to modify the procedure to calculate the sum of the integers from 1 to max, which represents any positive integer greater than 1.
Which of the following changes should be made to the procedure to meet the student's goal?
The procedure should take max as an input parameter.
The condition in the REPEAT UNTIL block should be changed to count > max.
The condition in the REPEAT UNTIL block should be changed to max < 5
I and II
A game program contains the following code to update three score variables, health, food, and knowledge. The maximum values for the three variables are 100, 80, and 25, respectively.
health ←← health + 10
IF(health > 100)
{
health ←← 100
}
food ←← food + 1
IF(food > 80)
{
food ←← 80
}
knowledge ←← knowledge + 5
IF(knowledge > 25)
{
knowledge ←← 25
}
The game program's author would like to write a procedure that could be used to update any variable in the game (myScore) that has a maximum value (myLimit) by a given amount (myAmount). A correct call of the procedure is shown below.
myScore ←← updateScore(myScore, myAmount, myLimit)
Which of the following is a correct implementation of updateScore ?
PROCEDURE updateScore(score, amount, limit)
{
score ←← score + amount
IF(score > limit)
{
score ←← limit
}
RETURN(score)
}
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?
Making the code run faster
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?
The student is reusing the computer scientist's procedural abstraction by knowing what the procedure does without knowing how it does it.