Developing Procedures MCQ Practice

Question 1 of 10

A procedure is documented as follows: averageOfTwo(a, b) returns the arithmetic mean of a and b. A caller executes result ← averageOfTwo(10, 20). Which value does result hold if the procedure matches its documentation?

  1. A. 15
  2. B. 20
  3. C. 30
  4. D. 10

Question 2 of 10

Consider the procedure below.

PROCEDURE maxOfThree(x, y, z)
{
    m ← x
    IF (y > m)
    {
        m ← y
    }
    IF (z > m)
    {
        m ← z
    }
    RETURN (m)
}

What does maxOfThree(4, 9, 2) return?

  1. A. 4
  2. B. 9
  3. C. 2
  4. D. 15

Question 3 of 10

Which statement best describes procedural abstraction for a caller of DISPLAY(greeting())?

  1. A. Procedural abstraction forbids procedures from having RETURN statements.
  2. B. The caller must rewrite every internal assignment inside greeting before calling it.
  3. C. The caller uses the name greeting and relies on what it returns, without depending on the hidden steps that compute that return value.
  4. D. The caller must know the line numbers of every statement inside greeting.

Question 4 of 10

A student writes the incomplete procedure below. The intended contract is: clamp(n, lo, hi) returns n if lo ≤ n ≤ hi, returns lo if n < lo, and returns hi if n > hi.

PROCEDURE clamp(n, lo, hi)
{
    IF (n < lo)
    {
        RETURN (lo)
    }
    IF (n > hi)
    {
        RETURN (???)
    }
    RETURN (n)
}

Which expression should replace ??? so that clamp(12, 0, 10) returns 10?

  1. A. 0
  2. B. n
  3. C. lo
  4. D. hi

Question 5 of 10

PROCEDURE doublePlusOne(n) is defined to RETURN (2 * n + 1). A program executes x ← doublePlusOne(3) then DISPLAY(x). What is displayed?

  1. A. 7
  2. B. 9
  3. C. 3
  4. D. 6

Question 6 of 10

The table lists three calls to PROCEDURE score(a, b), which returns a + 2 * b.

Call Returned value
score(1, 4) 9
score(5, 0) 5
score(2, 3) ?

What value replaces the question mark?

  1. A. 6
  2. B. 8
  3. C. 10
  4. D. 5

Question 7 of 10

A library author changes the body of PROCEDURE totalCost(price, taxRate) so it runs faster, but the returned value for every pair of arguments stays the same. Which claim is correct?

  1. A. Changing internals always changes every displayed output even when RETURN values are identical.
  2. B. Every caller must be rewritten because internals changed.
  3. C. Callers that depend only on the documented return values can keep the same calls.
  4. D. Procedural abstraction requires callers to hard-code the old internal steps.

Question 8 of 10

Consider:

PROCEDURE mystery(n)
{
    IF (n MOD 2 = 0)
    {
        RETURN (n / 2)
    }
    ELSE
    {
        RETURN (3 * n + 1)
    }
}

What does mystery(5) return?

  1. A. 2
  2. B. 5
  3. C. 15
  4. D. 16

Question 9 of 10

PROCEDURE areaRect(w, h) returns w * h. A student replaces its body with a longer sequence that still returns w * h for every pair of numbers. Why can other procedures that call areaRect(w, h) remain unchanged?

  1. A. Because callers use the procedure name and contract, not the internal steps.
  2. B. Because procedural abstraction bans parameters.
  3. C. Because RETURN statements are ignored by callers.
  4. D. Because every procedure must display its intermediate variables.

Question 10 of 10

Which procedure body correctly implements the contract “isAdult(age) returns true if age ≥ 18 and false otherwise”?

  1. A. PROCEDURE isAdult(age) { RETURN (age > 21) }
  2. B. PROCEDURE isAdult(age) { RETURN (age ≥ 18) }
  3. C. PROCEDURE isAdult(age) { RETURN (age = 18) }
  4. D. PROCEDURE isAdult(age) { RETURN (18) }