Calling Procedures

Help Questions

AP Computer Science Principles › Calling Procedures

Questions 1 - 10
1

Which of the following procedure calls will return the string "Eligible to drive"?

checkEligibility(15, true)

checkEligibility(true, 17)

checkEligibility(16, false)

checkEligibility(17, true)

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.

2

What is displayed when the code segment is executed?

55

100

500

550

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.

3

What is displayed as a result of executing the code?

Welcome, Explorer

showGreeting("Explorer")

Explorer

Welcome, name

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.

4

When the procedure displayItem is called, what value is assigned to the cost parameter?

"Laptop"

cost

true

1200

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.

5

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?

The values 5 and 10 are variables that are used to define the procedure's parameters num1 and num2.

The values 5 and 10 are parameters that are passed to the arguments num1 and num2.

The values 5 and 10 are arguments that are passed to the parameters num1 and num2.

The values 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.

6

What is displayed as a result of the call calculate(5, 3) ?

8

10

13

16

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.

7

Which of the following procedure calls will cause the robot to turn right and then move forward 4 squares?

makeTurn("right")

makeTurn(right, 4)

makeTurn(4, "right")

makeTurn("right", 4)

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".

8

What is displayed when the code segment is executed?

2

3

4

5

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.

9

Based on the provided scenario, in what order are procedures called to produce the weather report?​

GenerateReport, then ComputeAverages, then FilterByDate.

FilterByDate, then GenerateReport, then ComputeAverages.

FilterByDate, then ComputeAverages, then GenerateReport.

ComputeAverages, then FilterByDate, then GenerateReport.

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.

10

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?

The program skips the procedure call and continues to the next statement in the sequence.

The program immediately jumps to the first line of the called procedure and executes its statements.

The program finishes executing all remaining statements in its current block before executing the procedure.

The program terminates, and the called procedure begins running as a new, separate program.

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.

Page 1 of 3