Chapter 5
CONDITIONS, LOGICAL EXPRESSIONS, AND SELECTION CONTROL STRUCTURES
QUESTIONS
True/False
1. An example of a logical (Boolean) expression is an arithmetic expression followed by a relational operator followed by an arithmetic expression.
2. Boolean variables cannot store the result of a comparison of two variables.
3. The only logical expressions that can be assigned to Boolean variables are the literal values TRUE and FALSE.
4. If ch1 contains the value 'C' and ch2 contains the value 'K', the value of the C++ expression
ch1 <= ch2
is 1 (TRUE).
5. If P and Q are logical expressions, the expression P AND Q is TRUE if either P or Q is TRUE or both are TRUE.
6. The expression
!(n < 5) is equivalent to the expression n > 5.7. To test whether
someInt equals 25 or 30, the C++ expressionsomeInt == 25 || 30
has the correct semantics but produces a syntax (compile-time) error.
8. According to DeMorgan's Law, the expression
!(x <= y || s > t)
is equivalent to
x <= y && s > t
9. The statement
if (grade == 'A' || grade == 'B' || grade == 'C')
cout << "Fail";
else
cout << "Pass";
prints
Pass if grade is 'A', 'B', or 'C' and prints Fail otherwise.10. If a C++ If statement begins with
if (age = 30)
the If condition is an assignment expression, not a relational expression.
11. The code segment
if (speed <= 40)
cout << "Too slow";
if (speed > 40 && speed <= 55)
cout << "Good speed";
if (speed > 55)
cout << "Too fast";
could be written equivalently as
if (speed <= 40)
cout << "Too slow";
else if (speed <= 55)
cout << "Good speed";
else
cout << "Too fast";
12. If the code fragment
if (a >= 10)
if (a < 20)
a = a + 2;
else
a = a + 1;
is indented according to the manner in which it is executed, the correct indentation is
if (a >= 10)
if (a < 20)
a = a + 2;
else
a = a + 1;
13. Testing is performed only in the implementation phase of a program's life cycle.
14. Given the module description
PRINT AVERAGE (Level 1)
Print a heading
Print sumOfValues / countOfValues
the following is an appropriate module precondition: "sumOfValues is assigned AND countOfValues does not equal 0."
15. Given the module description
PRINT AVERAGE (Level 1)
Print a heading
IF countOfValues isn't 0
Print sumOfValues / countOfValues
ELSE
Print an error message
the following is an appropriate module precondition: "sumOfValues is assigned AND countOfValues does not equal 0."
Multiple Choice
16. Which of the following does not constitute a logical (Boolean) expression?
a. an arithmetic expression followed by a relational operator followed by an arithmetic expression
b. an arithmetic expression followed by a logical operator followed by an arithmetic expression
c. a Boolean variable or constant
d. a logical expression followed by a binary logical operator followed by a logical expression
e. a unary logical operator followed by a logical expression
17. Which of the following is not a C++ relational operator?
a.
==b.
<c.
!=d.
&&e.
>=18. If
a.
p && pb.
p || pc.
p && !pd.
p || !pe. b and d above
19. If DeMorgan's Law is used to negate the expression
(i < j) && (k == l)
then the result is:
a.
(i < j) || (k == l)b.
(i > j) && (k != l)c.
(i >= j) || (k != l)d.
(i > j) || (k != l)e.
(i >= j) && (k != l)20. Given a Boolean variable
a.
isEmpty = TRUE;b.
isEmpty = !isEmpty;c.
isEmpty = m > n;d. a and b above
e. a, b, and c above
21. Which logical operator (op) is defined by the following table? (T and F denote TRUE and FALSE.)
P Q P op Q
------------
T T T
T F F
F T F
F F F
a. NOT
b. AND
c. OR
d. none of the above
22. Which C++ logical expression correctly determines whether the value of
a.
0 < beta < 100b.
0 < beta && beta < 100c. (
0 < beta) && (beta < 100)
d. b and c above
e. a, b, and c above
23. This question is about short-circuit evaluation of logical expressions. Consider the following expression in some imaginary programming language (not C++):
(N > 5) AND (K / N < 12)
If N equals 0 when this expression is evaluated, which of the following statements about the expression is true?
a. It causes a divide-by-zero error only if the language uses short-circuit
evaluation.
b. It causes a divide-by-zero error only if the language does not use short-circuit
evaluation.
c. It causes a divide-by-zero error whether or not the language uses short-circuit
evaluation.
d. It never causes a divide-by-zero error.
24. If the
a. 3
b. FALSE
c. 20
d. TRUE
25. After execution of the following code, what will be the value of
cin >> angle;
if (angle > 5)
angle = angle + 5;
else if (angle > 2)
angle = angle + 10;
a. 0
b. 5
c. 10
d. 15
e. 25
26. After execution of the following code, what will be the value of
angle if the input value is 10?cin >> angle;
if (angle > 5)
angle = angle + 5;
if (angle > 2)
angle = angle + 10;
a. 0
b. 5
c. 10
d. 15
e. 25
27. After execution of the following code, what will be the value of
angle if the input value is 0?cin >> angle;
if (angle > 5)
angle = angle + 5;
else if (angle > 2)
angle = angle + 10;
else
angle = angle + 15;
a. 0
b. 5
c. 10
d. 15
e. 25
28. After execution of the following code, what will be the value of
angle if the input value is 0?cin >> angle;
if (angle > 5)
angle = angle + 5;
else if (angle > 2)
angle = angle + 10;
a. 0
b. 5
c. 10
d. 15
e. 25
29. What is the output of the following C++ code fragment? (Be careful here.)
int1 = 120;
cin >> int2; // Assume user types 30
if ((int1 > 100) && (int2 = 50))
int3 = int1 + int2;
else
int3 = int1 - int2;
cout << int1 << ' ' << int2 << ' ' << int3;
a.
120 30 150b.
120 30 90c.
120 50 170d.
120 50 70e.
120 30 7030. Consider the following If statement, which is syntactically correct but uses poor style and indentation:
if (x >= y) if (y > 0) x = x * y; else if (y < 4) x = x - y;
Assume that
x and y are int variables containing the values 9 and 3, respectively, before execution of the above statement. After execution of the statement, what value will x contain?a. 9
b. 1
c. 6
d. 27
e. none of the above
31. Consider the following If statement, which is syntactically correct but uses poor style and indentation:
if (x >= y) if (y > 0) x = x * y; else if (y < 4) x = x - y;
Assume that
x and y are int variables containing the values 3 and 9, respectively, before execution of the above statement. After execution of the statement, what value will x contain?a. 9
b. -6
c. 6
d. 27
e. none of the above
32. What is the output of the following code fragment if the input value is 20? (Be careful here.)
cin >> someInt;
if (someInt > 30)
cout << "Moe ";
cout << "Larry ";
cout << "Curly";
a.
b.
Moe Larry Curlyc.
Larry Curlyd. no output; there is a compile-time error
e. no output; there is a run-time error
33. Assuming
alpha and beta are int variables, what is the output of the following code (which is indented poorly)?alpha = 3;
beta = 2;
if (alpha < 2)
if (beta == 3)
cout << "Hello";
else cout << "There";
a. Nothing is output.
b.
c.
Thered.
HelloThere34. What does the following statement print? (All variables are of type
int.)if (j < k)
if (k < j)
cout << 1;
else
cout << 2;
else
if (j < k)
cout << 3;
else
cout << 4;
a. It prints nothing unless
b. It always prints 4.
c. It prints 2 if
j equals k and 4 otherwise.d. It prints 2 if
j < k and 1 if k <= j.e. It prints 2 if
j < k and 4 otherwise.35. What is the missing If condition in the following code fragment? The program is supposed to halt if the input file does not exist.
ifstream inFile;
inFile.open("myfile.dat");
if ( )
{
cout << "Cannot open input file." << endl;
return 1;
}
a.
inFileb.
myfile.datc.
!inFiled.
!myfile.date.
inFile != myfile.dat36. The phrase "minimum complete coverage" means that we test a program with
a. at least one set of data.
b. data that executes every branch at least once.
c. data that executes all possible combinations of branches at least once.
d. all possible data values.
37. In order to test the boundaries of the following condition, what data values would you use for the variable
alpha >= 1
a. 0, 1, and
INT_MAXb. 1, 2, and
INT_MAXc.
INT_MIN, 1, and INT_MAXd.
INT_MIN, 0, 1, and INT_MAXe.
INT_MIN, 1, 2, and INT_MAXFill-In
38. A(n) ____________________ expression is an expression composed of logical (Boolean) values and operations.
39. The operators <, ==, >=, and != are examples of ____________________ operators. (Do not answer "binary.")
40. In programming languages that use ____________________ evaluation of a logical expression, evaluation proceeds in left-to-right order and stops as soon as the final truth value can be determined.
41. The operators &&, ||, and ! are known as ____________________ operators.
42. In programming languages that use ____________________ evaluation of a logical expression, all subexpressions are evaluated before applying any logical operators.
43. Write a logical expression that is TRUE if the variable testScore is greater than or equal to 90 and less than or equal to 100: ____________________
44. Write a logical expression that is FALSE if either x or y is equal to 5: ____________________
45. The order in which the computer executes statements in a program is called the ____________________.
46. An If statement is an example of a(n) ____________________ control structure.
47. An If-Then-Else-If structure represents a(n) ____________________-way branch.
49. Any statement used to alter the normally sequential flow of control is called a(n) ____________________.
50. A(n) ____________________ is an assertion that should be true after a module has finished executing.
51. After the code is written in the implementation phase, you should go over it line by line. This process is known as a(n) ____________________.
52. A(n) ____________________ is an assertion that must be true before a module begins executing.
53. Two approaches to program testing are ____________________ coverage and data coverage.
54. A(n) ____________________ is the process of going through the steps of an algorithm to confirm that they produce the required postcondition, given the stated precondition.