Calling Procedures - AP Computer Science Principles

  1. What value is displayed by the statement DISPLAY(findMax(findMax(10, 25), 15))?
    Answer: 25 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.
  2. What is displayed by the call checkValue(10)?
    Answer: Low 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.
  3. Which of the following procedure calls will cause the robot to turn right and then move forward 4 squares?
    Answer: makeTurn("right", 4) 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".
  4. 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?
    Answer: The program immediately jumps to the first line of the called procedure and executes its statements. 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.
  5. What is displayed when the code segment is executed?
    Answer: 500 followed by 600 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.
  6. What is the output of the code segment?
    Answer: 20 followed by 10 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.
  7. Which of the following statements best describes an advantage of the procedure call used in Program B compared to the code in Program A?
    Answer: Program B uses a procedure to reduce code duplication and make the program easier to modify. 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.
  8. Which of the following procedure calls will return the string "Eligible to drive"?
    Answer: checkEligibility(17, true) 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.
  9. What is the primary benefit of using parameters when defining a procedure in a program?
    Answer: They allow the procedure to be reused with different input values, making the code more general. Correct. Parameters act as placeholders for data. By using parameters, a programmer can write a single procedure that can operate on a wide range of different data values passed as arguments, promoting code reuse and generalization. Distractor B is incorrect; using procedures can sometimes add a small amount of overhead and does not guarantee faster execution. Distractor C is a function of some specific programming languages (static typing), but it is not the primary benefit of parameters themselves. Distractor D is incorrect; parameters are local to the procedure and do not necessarily reduce the overall number of variables needed.
  10. What is the sequence of values displayed?
    Answer: 15 followed by 8 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.
  11. What is displayed when the code segment is executed?
    Answer: -5 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.
  12. What is displayed as a result of executing the code?
    Answer: Welcome, Explorer 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.
  13. In the context of the program described, identify an error in the procedure call sequence for inventory removal.​
    Answer: Calling RemoveItem before CheckStock can allow negative inventory. 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 such as RemoveItem and CheckStock are defined to handle inventory management tasks. Data flows through these procedures via parameters and return values, and the order of calls is critical for maintaining data integrity. Choice A is correct because it accurately identifies a logical error: removing an item before checking if sufficient stock exists could result in negative inventory values, which is typically an error condition. Choice B is incorrect because calling TrackItem after RemoveItem would not prevent output - it would simply track the removal that already occurred. 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 consider the logical dependencies between procedures and identify potential error conditions when procedures are called in the wrong order.
  14. In the code segment shown, which line contains the procedure call?
    Answer: Line 9 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.
  15. What is displayed when the code segment is executed?
    Answer: 100 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.
  16. 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?
    Answer: The values 5 and 10 are arguments that are passed to the parameters num1 and num2. 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.
  17. Based on the provided scenario, in what order are procedures called to produce the weather report?​
    Answer: FilterByDate, then ComputeAverages, then GenerateReport. 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.
  18. What is displayed as a result of the call printPattern("*", 4)?
    Answer: 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.
  19. In the context of the program described, in what order are procedures called to compute each student's final grade?​
    Answer: ComputeAverage, then ApplyLatePenalty, then PrintGrade. 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 such as ComputeAverage, ApplyLatePenalty, and PrintGrade are defined to handle tasks like calculating averages, applying penalties, and outputting final grades. Data flows through these procedures via parameters and return values in a logical sequence. Choice C is correct because it accurately reflects the logical order: first compute the average score, then apply any late penalties to that average, and finally print the resulting grade. Choice A is incorrect because it suggests printing the grade before calculating it, which would be impossible as PrintGrade needs the final computed value to display. 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 create flowcharts showing the logical sequence of procedure calls to visualize dependencies.
  20. What is displayed when the code segment is executed?
    Answer: 2 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.

Start this quiz