< previous page page_243 next page >

Page 243
5. On a telephone, each of the digits 2 through 9 has a segment of the alphabet associated with it. What kind of control structure would you use to decide which segment a given letter falls into and to print out the corresponding digit? (pp. 209212)
6. What is one postcondition that every program should have? (pp. 230234)
7. In what phase of the program development process should you carry out an execution trace? (pp. 234237)
8. You've written a program that prints out the corresponding digit on a phone given a letter of the alphabet. Everything seems to work right except that you can't get the digit 5 to print out; you keep getting the digit 6. What steps would you take to find and fix this bug? (pp. 237239)
Answers 1. letter < Z 2. letter >= A && letter <= Z 3. The If-Then-Else form 4. The If-Then form 5. A nested If statement 6. The user has been notified of invalid data values. 7. The implementation phase 8. Carefully review the section of code that should print out 5. Check the branching condition and the output statement there. Try some sample values by hand.
Exam Preparation Exercises
1. Given these values for Boolean variables x, y, and z:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
x = TRUE, y = FALSE, z = TRUE
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
evaluate the following logical expressions. In the blank next to each expression, write a T if the result is TRUE or an F if the result is FALSE.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
_____ a. x && y | | x && z
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
_____ b. (x | | !y) && (!x | | z)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
_____ c. x | | y && z
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
_____ d. !(x | | y) && z
2. Given these values for variables i, j, p, and q:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
i = 10, j = 19, p = TRUE, q = FALSE
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
add parentheses (if necessary) to the expressions below so that they evaluate to TRUE.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
a. i == j | | p
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. i >= j | | i <= j && p
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
c. !p | | p
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
d. !q && q
3. Given these values for the int variables i, j, m, and n:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
i = 6, j = 7, m = 11, n = 11
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
what is the output of the following code?
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
cout << Madam;
if (i < j)
    if (m != n)
        cout << How;
    else
        cout << Now;
(continued on next page)

 
< previous page page_243 next page >