Quiz 4 Solutions

Wed Nov 15

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

  1. Evaluate the following prefix expression. What result does it produce?

    (- (* 3 (+ 1 7)) (+ 9 4))

    ⇒ (- (* 3 (+ 1 7)) (+ 9 4))
    ⇒ (- (* 3 8) (+ 9 4))
    ⇒ (- 24 (+ 9 4))
    ⇒ (- 24 13)
    ⇒ 11

  2. Evaluate the following postfix expression. What result does it produce?

    2 5 2 + 4 * 1 - *

    Evaluate the operators left to right. Each one refers to the previous two things in the list.

    2 5 2 + 4 * 1 - *
    2 7 4 * 1 - *
    2 28 1 - *
    2 27 *
    54

  1. Above is a tree representing an arithmetic expression. Write the corresponding expression in:
  • Prefix notation: (* (* 2 5) (+ 7 2))
  • Infix notation: 2 * 5 * (7 + 2)

  1. Convert the following infix expression to postfix notation:

    3 * (2 + 8) - (6 + 1)

    The postfix expression is:

    3 2 8 + * 6 1 + -