Developing Procedures FRQ Practice

Responses receive AI feedback against a rubric after submission. Scores are practice estimates.

Question 1 of 5

A caller needs a procedure addThree(a, b, c) that returns the sum of its three parameters. (a) Write the procedure using PROCEDURE and RETURN. (b) Trace addThree(2, 5, 1) and state the returned value. (c) Explain one benefit of calling addThree instead of copying a + b + c in many places.

Question 2 of 5

PROCEDURE letterGrade(score) should return "A" if score ≥ 90, "B" if score ≥ 80 (but less than 90), and "C" otherwise. (a) Complete the missing RETURN expressions. (b) Trace letterGrade(85) and letterGrade(90).

PROCEDURE letterGrade(score)
{
    IF (score ≥ 90)
    {
        RETURN (???)
    }
    IF (score ≥ 80)
    {
        RETURN (???)
    }
    RETURN (???)
}

Question 3 of 5

PROCEDURE powerOfTwo(n) is documented to return 2^n for nonnegative integer n by repeated doubling starting from 1. (a) Write a procedure that uses a loop and RETURN to match that contract for n ≥ 0. (b) Trace your procedure for n = 4 and show the value of the accumulator after each doubling. (c) State what the caller needs to know about powerOfTwo besides its name and parameters.

Question 4 of 5

A program must compute the larger of two products: left = a * b and right = c * d, then return that larger product. (a) Write PROCEDURE largerProduct(a, b, c, d) that returns the larger of a*b and c*d. (b) Trace largerProduct(3, 4, 5, 2). (c) Explain why a caller can use largerProduct without knowing whether you compared products with IF or with a MAX helper.

Question 5 of 5

The incomplete procedure should return true only when both flags are true. (a) Replace ??? with a Boolean expression using AND. (b) Trace bothTrue(true, false) and bothTrue(true, true). (c) Give one reason to put this test in a procedure instead of repeating the AND expression.

PROCEDURE bothTrue(p, q)
{
    RETURN (???)
}