Quiz 3 Solutions

Mon 3 Mar

You have up to 25 minutes. You may use a standard calculator if necessary, but no text book or notes.

  1. This problem is about the concept of a variable in an algorithm. Recall that a variable is a named storage location. Trace the values of X, Y, and Z through the following instructions.

    1. Set X to 3
    2. Set Y to X + 2
    3. Set Z to Y * Y (multiply)
    4. Set Y to X + 5
    Your work for problem 1 can go here.

    Your work for problem 1 can go here.

  2. This problem is like the previous one, but we add one conditional statement, which executes or doesn’t depending on a Boolean condition.

    1. Set J to 5
    2. Set K to J + 2
    3. If K > 8 then Set J to J - 2
    4. Set K to K + 1
    Your work for problem 2.

    Your work for problem 2.

    The condition K>8 is false when we encounter it, so the rest of step 3 is skipped.

  3. Carefully trace the following algorithm. What does it output?

    1. Set A to 1.
    2. Set B to 1.
    3. Output A
    4. Output B
    5. If B > 8 then stop.
    6. Set X to A + B
    7. Copy B to A
    8. Copy X to B
    9. Go back to step 4.
    Your work for problem 3.

    Your work for problem 3.

    This algorithm produces a sequence called the Fibonacci numbers. The sequence starts with 1, 1, and then each subsequent number is the sum of the previous two. The sequence has lots of interesting properties.

  4. The following statements are about pseudo-code. Mark each as true or false.

    1. Pseudo-code is based on English, but used in a very precise way to avoid ambiguity. TRUE

    2. Compared to a programming language, pseudo-code can be understood more easily by humans. TRUE

    3. Pseudo-code can also be executed by a computer. FALSE

    4. We call it pseudo code because it is very fast. FALSE