Documente Academic
Documente Profesional
Documente Cultură
CPT 168
Fall 2012
Homework #11
Chapter 4: 4.1-4.7, Checkpoint questions 4.21-4.28 and Review Questions, Multiple Choice, T/F &
Programming Exercise #1
4.22 The following truth table shows various combinations of the values true and false connected by a
logical operator. Complete the truth table by circling T or F to indicate whether the result of such a
combination is true or false.
Logical Expression
Result (circle T or F)
True OR False
True OR True
Logical Expression
Result (circle T or F)
False OR True
False OR False
NOT True
NOT False
4.23 Assume the variables a = 2, b = 4, and c = 6. Circle the T or F for each of the following
conditions to indicate whether its value is true or false.
Expression
Result (circle T or F)
a == 4 OR b > 2
1 != b AND c != 3
a >= -1 OR a <= b
NOT (a > 2)
4.24 Explain how short-circuit evaluation works with the AND and OR operators.
With the AND operator: The left side of the expression is evaluated first. If it evaluates to False, since
there is no way the entire and expression can be True, the right side of the expression is not
evaluated.
With the OR operator: The left side of the expression is evaluated first. If it evaluates to True, the
entire or expression will evaluate to true no matter what the value of the second expression. So the
second expression is not evaluated.
4.25 Write an If-Then statement that displays the message The number is valid if the variable
speed is within the range 0 through 200.
If speed >= 0 AND speed <= 200 Then
Display The Number is valid
4.26 Write an If-Then statement that displays the message The number is not valid if the
variable speed is outside the range 0 through 200.
If NOT (speed >= 0 AND speed <= 200) Then
Display The number is not valid
Review Questions
1.
A _____________ structure can execute a set of statements only under certain circumstances.
a.
b.
c.
d.
sequence
circumstantial
decision
Boolean
2.
a.
b.
c.
d.
sequence
single alternative decision
one path alternative
single execution decision
3.
a.
b.
c.
d.
sequence structure
decision structure
pathway structure
class structure
4.
a.
b.
c.
d.
binary
decision
unconditional
Boolean
5.
a.
b.
c.
d.
relational
logical
conditional
ternary
6.
a.
A(n) ____________ structure tests a condition and then takes one path if the condition is true, or
another path if the condition is false.
If-Then statement
b.
c.
d.
7.
8.
You use a(n) _____________ statement in pseudocode to write a dual alternative decision
structure.
a. Test-Jump
b. If-Then
c. If-Then-Else
d. If-Call
9.
a.
b.
c.
d.
10.
A _____________ structure allows you to test the value of a variable or an expression and then
use that value to determine which statement or set of statements to execute.
variable test decision
single alternative decision
dual alternative decision
multiple alternative decision
A(n) _____________ section of a Select Case statement is branched to if none of the case
values match the expression listed after the Select statement.
a. Else
b. Default
c. Case
d. Otherwise
J
11. AND, OR, and NOT are ____________ operators.
a.
b.
c.
d.
relational
logical
conditional
ternary
12.
a.
b.
c.
d.
13.
a.
b.
c.
d.
14.
a.
b.
c.
d.
15.
a.
b.
c.
d.
A compound Boolean expression created with the _______________ operator is true only if both
of its sub-expressions are true.
AND
OR
NOT
BOTH
A compound Boolean expression created with the _______________ operator is true if either of
its sub-expressions is true.
AND
OR
NOT
BOTH
The _______________ operator takes a Boolean expression as its operand and reverses its
logical value.
AND
OR
NOT
EITHER
A _______________ is a Boolean variable that signals when some condition exists in the
program.
flag
signal
sentinel
siren
True or False
1.
FALSE
2.
A program can be made of only one type of control structure. You cannot combine structures.
FALSE
3.
A single alternative decision structure tests a condition and then takes one path if the condition is
true, or another path if the condition is false.
FALSE
4.
5.
A compound Boolean expression created with the AND operator is true only when both subexpressions are true.
TRUE
Programming Exercise
1.
Roman Numerals
Design a program that prompts the user to enter a number within the range of 1 through 10. The
program should display the Roman numeral version of that number. If the numbers is outside the
range of 1 through 10, the program should display an error message.
romNum = II
Case 3;
romNum = III
Case 4:
romNum = IV
Case 5:
romNum = V
Case 6:
romNum = VI
Case 7:
romNum = VII
Case 8:
romNum = VIII
Case 9:
romNum = IX
Default:
romNum = X
End Select
Display romNum
End If