|
|
|
|
|
|
|
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: |
|
|
|
 |
|
|
|
|
x = TRUE, y = FALSE, z = TRUE |
|
|
|
 |
|
|
|
|
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. |
|
|
|
 |
|
|
|
|
_____ a. x && y | | x && z |
|
|
|
 |
|
|
|
|
_____ b. (x | | !y) && (!x | | z) |
|
|
|
 |
|
|
|
|
_____ c. x | | y && z |
|
|
|
 |
|
|
|
|
_____ d. !(x | | y) && z |
|
|
|
|
|
|
|
|
2. Given these values for variables i, j, p, and q: |
|
|
|
 |
|
|
|
|
i = 10, j = 19, p = TRUE, q = FALSE |
|
|
|
 |
|
|
|
|
add parentheses (if necessary) to the expressions below so that they evaluate to TRUE. |
|
|
|
 |
|
|
|
|
a. i == j | | p |
|
|
|
 |
|
|
|
|
b. i >= j | | i <= j && p |
|
|
|
 |
|
|
|
|
c. !p | | p |
|
|
|
 |
|
|
|
|
d. !q && q |
|
|
|
|
|
|
|
|
3. Given these values for the int variables i, j, m, and n: |
|
|
|
 |
|
|
|
|
i = 6, j = 7, m = 11, n = 11 |
|
|
|
 |
|
|
|
|
what is the output of the following code? |
|
|
|
 |
|
|
|
|
cout << Madam;
if (i < j)
if (m != n)
cout << How;
else
cout << Now; |
|
|
|
|
|