What this quiz covers
This quiz focuses on Calling Procedures, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science Principles.
Consider the following procedure.
PROCEDURE checkEligibility(age, hasLicense) { IF (age >= 16 AND hasLicense = true) { RETURN("Eligible to drive") } ELSE { RETURN("Not eligible to drive") } }
Which of the following procedure calls will return the string "Eligible to drive"?
Which of the following procedure calls will return the string "Eligible to drive"?
AP Computer Science Principles Quiz
Practice Calling Procedures in AP Computer Science Principles with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
This quiz focuses on Calling Procedures, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science Principles.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
Consider the following procedure.
PROCEDURE checkEligibility(age, hasLicense) { IF (age >= 16 AND hasLicense = true) { RETURN("Eligible to drive") } ELSE { RETURN("Not eligible to drive") } }
Which of the following procedure calls will return the string "Eligible to drive"?
Which of the following procedure calls will return the string "Eligible to drive"?
Explanation: Correct. To return "Eligible to drive", the condition age >= 16 AND hasLicense = true must be true. In this option, age is 17 (which is >= 16) and hasLicense is true. Both parts of the AND condition are true, so the entire condition is true. The other options fail one or both parts of the condition.
Consider the following procedure.
PROCEDURE getBonus(score) { RETURN(score * 10) }
The following code segment is executed.
finalScore ← 50 + getBonus(5) DISPLAY(finalScore)
What is displayed when the code segment is executed?
Explanation: Correct. The expression getBonus(5) is evaluated first. The procedure call returns 5 * 10, which is 50. The assignment statement then becomes finalScore ← 50 + 50, which assigns 100 to finalScore. This value is then displayed. Distractor D incorrectly calculates (50 + 5) * 10. Distractor B incorrectly calculates 50 + 5. Distractor C incorrectly calculates 50 * 10.
Consider the following procedure.
PROCEDURE showGreeting(name) { DISPLAY("Welcome, ") DISPLAY(name) }
A programmer calls the procedure with the following statement.
showGreeting("Explorer")
What is displayed as a result of executing the code?
Explanation: Correct. The procedure call passes the argument "Explorer" to the parameter name. The procedure then displays the string literal "Welcome, " followed by the value of name, which is "Explorer". The DISPLAY command adds a space after each output, resulting in "Welcome, Explorer". Distractor B incorrectly displays the parameter name instead of its value. Distractor C only shows a portion of the output. Distractor D shows the code statement itself, not its output.
Consider the following procedure and procedure call.
PROCEDURE displayItem(name, cost, inStock) { /* implementation not shown */ }
displayItem("Laptop", 1200, true)
When the procedure displayItem is called, what value is assigned to the cost parameter?
Explanation: Correct. Arguments in a procedure call are matched to parameters based on their position. The second argument in the call is 1200, which corresponds to the second parameter in the procedure definition, cost. Therefore, cost is assigned the value 1200. The other distractors represent the values assigned to the other parameters or the parameter name itself.
Consider the following code segment.
Line 1: PROCEDURE calculate(num1, num2) Line 2: { Line 3: RETURN(num1 * num2) Line 4: } Line 5: Line 6: result ← calculate(5, 10)
In the code segment, which of the following best describes the relationship between the values 5 and 10 on Line 6 and the variables num1 and num2 on Line 1?
5 and 10 are arguments that are passed to the parameters num1 and num2. (correct answer)5 and 10 are parameters that are passed to the arguments num1 and num2.5 and 10 are variables that are used to define the procedure's parameters num1 and num2.5 and 10 are return values from the procedure that are assigned to the parameters num1 and num2.Explanation: Correct. Parameters are the variables listed in a procedure's definition (num1, num2). Arguments are the actual values (5, 10) passed to the procedure when it is called. Distractor B incorrectly reverses the terms. Distractor C incorrectly identifies the values as variables. Distractor D incorrectly describes the flow of data; values are passed into, not returned to, parameters.
Consider the following procedures.
PROCEDURE double(val) { RETURN(val * 2) }
PROCEDURE calculate(a, b) { temp ← double(a) + b DISPLAY(temp) }
What is displayed as a result of the call calculate(5, 3) ?
What is displayed as a result of the call calculate(5, 3) ?
Explanation: Correct. The procedure calculate is called with a as 5 and b as 3. Inside calculate, the procedure double is called with the argument a, which is 5. double(5) returns 5 * 2 = 10. The variable temp is then assigned the value of 10 + b, which is 10 + 3 = 13. The value of temp, 13, is then displayed.
A programmer creates the following procedure to move a robot on a grid.
PROCEDURE makeTurn(direction, steps) { IF (direction = "left") { ROTATE_LEFT() } ELSE { ROTATE_RIGHT() } REPEAT steps TIMES { MOVE_FORWARD() } }
Which of the following procedure calls will cause the robot to turn right and then move forward 4 squares?
Explanation: Correct. This call provides "right" for the direction parameter and 4 for the steps parameter. The IF condition ("right" = "left") is false, so the ELSE block executes, causing a right turn. The REPEAT loop then executes 4 times, moving the robot forward 4 squares. Distractor B provides the arguments in the wrong order. Distractor C provides too few arguments. Distractor D provides right as a variable name instead of the string literal "right".
Consider the following procedure, which is intended to determine if a number is positive.
PROCEDURE isPositive(num) { IF (num > 0) { RETURN(true) } ELSE { RETURN(false) } }
A program uses this procedure to count the positive numbers in a list.
myList ← [-2, 0, 5, 10, -8] count ← 0 FOR EACH value IN myList { IF (isPositive(value)) { count ← count + 1 } } DISPLAY(count)
What is displayed when the code segment is executed?
Explanation: Correct. The loop iterates through the list. For each element, it calls isPositive. isPositive(-2) is false. isPositive(0) is false. isPositive(5) is true, so count becomes 1. isPositive(10) is true, so count becomes 2. isPositive(-8) is false. The final value of count, which is 2, is displayed.
Consider the following procedure and code segment.
PROCEDURE calculate(val1, val2) { DISPLAY(val1 - val2) }
x ← 20 y ← 5 calculate(x / 2, y * 3)
What is displayed when the code segment is executed?
Explanation: Correct. The arguments passed to the calculate procedure must be evaluated first. The first argument is x / 2, which is 20 / 2 = 10. The second argument is y * 3, which is 5 * 3 = 15. The procedure then assigns 10 to val1 and 15 to val2. Inside the procedure, val1 - val2 is 10 - 15, which equals -5. This value is displayed.
Consider the following procedure.
PROCEDURE process(num) { result ← num + 5 DISPLAY(result) }
The following code is executed.
x ← 10 process(x) process(3)
What is the sequence of values displayed?
Explanation: Correct. The first call is process(x), where x is 10. The procedure calculates 10 + 5 and displays 15. The second call is process(3). The procedure calculates 3 + 5 and displays 8. The variable x is not modified by the procedure call. Distractor B incorrectly assumes the value from the first call affects the second. Distractor C displays the arguments, not the results. Distractor D incorrectly repeats the first result.
Consider the following procedure.
PROCEDURE printPattern(symbol, count) { REPEAT count TIMES { DISPLAY(symbol) } }
What is displayed as a result of the call printPattern("*", 4)?
What is displayed as a result of the call printPattern("*", 4)?
Explanation: Correct. The printPattern procedure is called with symbol as "" and count as 4. The REPEAT loop executes 4 times. In each iteration, DISPLAY("*") is executed. The DISPLAY command prints the value followed by a space, so the final output is " * * * ". Distractor C is incorrect because DISPLAY adds a space. Distractor B incorrectly prints the parameter name. Distractor D misunderstands how the procedure functions.
Consider the following procedure.
PROCEDURE checkValue(num) { IF (num > 10) { DISPLAY("High") } ELSE { DISPLAY("Low") } }
What is displayed by the call checkValue(10)?
What is displayed by the call checkValue(10)?
Explanation: Correct. The procedure is called with the argument 10. The condition num > 10 is evaluated as 10 > 10, which is false. Therefore, the code in the ELSE block is executed, and "Low" is displayed. Distractor B is incorrect because the condition is not met. Distractor C is incorrect because only one branch of an IF/ELSE statement can execute.
The following code segment is intended to calculate and display a discount.
Line 1: PROCEDURE calculateDiscount(price, percent) Line 2: { Line 3: discountAmount ← price * (percent / 100) Line 4: DISPLAY(discountAmount) Line 5: } Line 6: Line 7: total ← 200 Line 8: rate ← 15 Line 9: calculateDiscount(total, rate)
In the code segment shown, which line contains the procedure call?
Explanation: Correct. Line 9, calculateDiscount(total, rate), is the statement that invokes or 'calls' the procedure, passing the values of the variables total and rate as arguments. Line 1 is the procedure definition, which declares the procedure and its parameters. Lines 3 and 4 are statements inside the procedure's body.
Consider the following procedure.
PROCEDURE subtract(a, b) { RETURN(a - b) }
What is displayed by the following statement?
DISPLAY(subtract(10, subtract(5, 2)))
What is displayed by the following statement?
Explanation: Correct. The statement has a nested procedure call. The inner call, subtract(5, 2), is evaluated first. It returns 5 - 2, which is 3. The outer call then becomes subtract(10, 3). This call returns 10 - 3, which is 7. The DISPLAY statement then displays 7.
A program contains the following procedure and code segment.
PROCEDURE updateScore(currentScore) { currentScore ← currentScore + 100 RETURN(currentScore) }
score ← 500 newScore ← updateScore(score) DISPLAY(score) DISPLAY(newScore)
What is displayed when the code segment is executed?
Explanation: Correct. In AP CSP's model, arguments are passed by value. When updateScore(score) is called, a copy of score's value (500) is sent to the currentScore parameter. The procedure calculates 500 + 100 and returns 600. This return value is assigned to newScore. The original score variable is not modified. Thus, score is still 500 and newScore is 600.
A programmer wrote the following procedure to update a value.
PROCEDURE doubleValue(x) { x ← x * 2 DISPLAY(x) }
The following code segment is then executed.
myVar ← 10 doubleValue(myVar) DISPLAY(myVar)
What is the output of the code segment?
Explanation: Correct. The call doubleValue(myVar) passes the value of myVar (10) to the parameter x. Inside the procedure, x becomes 10 * 2 = 20, and 20 is displayed. Because parameters are passed by value, the original variable myVar is not affected by the change to x inside the procedure. Therefore, when DISPLAY(myVar) is executed after the call, its original value of 10 is displayed.
Program A contains the following code.
DISPLAY("Drawing a square") /* code to draw a square / DISPLAY("Drawing a triangle") / code to draw a triangle */
Program B contains the following code.
PROCEDURE draw(shapeName) { DISPLAY("Drawing a ") DISPLAY(shapeName) /* code to draw the shape */ }
draw("square") draw("triangle")
Which of the following statements best describes an advantage of the procedure call used in Program B compared to the code in Program A?
Explanation: Correct. Program B abstracts the action of drawing into a procedure. This avoids repeating similar blocks of code. If a change is needed (e.g., changing the display message), it only needs to be done once in the procedure definition, making the program easier to maintain and modify. Distractor B is incorrect; procedure calls can add overhead and don't guarantee faster execution. Distractor C is a possible side effect but not the primary advantage, which is about structure and maintainability, not just length. Distractor D is incorrect; both programs explicitly handle the same two shapes.
Consider the following procedure.
PROCEDURE findMax(num1, num2) { IF (num1 > num2) { RETURN(num1) } ELSE { RETURN(num2) } }
What value is displayed by the statement DISPLAY(findMax(findMax(10, 25), 15))?
What value is displayed by the statement DISPLAY(findMax(findMax(10, 25), 15))?
Explanation: Correct. The inner procedure call findMax(10, 25) is evaluated first. Since 25 is greater than 10, the procedure returns 25. The expression then becomes findMax(25, 15). In this call, since 25 is greater than 15, the procedure returns 25. Finally, the value 25 is displayed.
Based on the provided scenario, in what order are procedures called to produce the weather report?
Explanation: This question tests AP Computer Science Principles skills: understanding and calling procedures. In programming, procedures are blocks of code designed to perform a specific task; they are called within a program to execute their defined operations. In the provided scenario, procedures FilterByDate, ComputeAverages, and GenerateReport work together to produce a weather report. Data flows through these procedures via parameters and return values in a logical sequence. Choice B is correct because it accurately reflects the logical order: first filter the data to the relevant date range, then compute averages on that filtered subset, and finally generate the report from those computed values. Choice C is incorrect because computing averages before filtering would waste resources calculating values for data that will be discarded, and could produce incorrect results. To help students: Emphasize understanding the role of each procedure within the program and practice tracing data flow through parameters and return values. Encourage students to think about efficiency and logical dependencies when determining the correct order of procedure calls.
A program is executing a sequence of statements. When a statement containing a procedure call is executed, what happens next in the flow of control?
Explanation: Correct. A procedure call interrupts the normal sequential execution. The flow of control transfers to the body of the called procedure. After the procedure finishes, control returns to the point immediately following the call. Distractor B describes sequential execution without a call. Distractor C is incorrect because procedure calls are meant to be executed. Distractor D incorrectly describes program termination.