Sunteți pe pagina 1din 8

Answer Key

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.21 What is a compound Boolean expression?


A compound Boolean expression is one in which the AND operator and/or the OR operator are used
to connect multiple Boolean expressions.
Ex.
Assuming the variables a = 2, b = 7, name = joe
a > 5 OR b < 10 AND name = joe
The compound Boolean expression above would evaluate in the following manner:
[(2 > 5) OR (7 < 10)] AND name = joe
[False OR True] AND True
True AND True = True

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 AND False

True AND True

False AND True

False AND False

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

6 <= c AND a > 3

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

4.27 What values can you store in a Boolean variable?


True OR False (one or the other).

4.28 What is a flag variable?


A flag variable is a Boolean variable that signals whether a specified condition exists in the program.
The decision structure evaluates the condition expression and sets the flag variable accordingly:
If numberOfMonkeys > 10 Then
Set manyMonkeys = True
Else
Set manyMonkeys = False
End If
Then the program can later check the flag to see if the condition has been met:
If manyMonkeys Then
Display Too many monkeys!
End If
The above If statement is equivalent to the following:
If manyMonkeys == True Then
Display Too many monkeys!
End If

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 _____________ structure provides one alternative path of execution.

a.
b.
c.
d.

sequence
single alternative decision
one path alternative
single execution decision

3.

In pseudocode, the If-Then statement is an example of a _____________.

a.
b.
c.
d.

sequence structure
decision structure
pathway structure
class structure

4.

A(n) ____________ expression has a value of either true or false.

a.
b.
c.
d.

binary
decision
unconditional
Boolean

5.

The symbols >, <, and == are all ____________ operators.

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.

single alternative decision


dual alternative decision
sequence
You use a(n) _____________ statement in pseudocode to write a single alternative decision
structure.
a. Test-Jump
b. If-Then
c. If-Then-Else
d. If-Call

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.

You can write any program using only sequence structures.

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.

A decision structure can be nested inside another decision structure.


TRUE

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.

Declare Integer number = 0


Declare String romanNum =
Display Enter a number from 1 - 10:
Input number
If NOT (number >= 1 and number <=10) Then // Check if value is in range
// first
Display Invalid number.
Else
Case 1:
romNum = I
Case 2:

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

S-ar putea să vă placă și