Chapter 5 Study Questions
Chapter 5 Study Questions
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 p is a Boolean variable, which of the following logical expressions always has
the value false?
A) p && p
B) p || p
C) p && !p
D) p || !p
E) 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 isEmpty, which of the following is a valid C++
assignment statement?
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 beta
lies between 0 and 100?
A) 0<beta<100
B) 0<beta&&beta<100
C) (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 shortcircuit 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 int variables i, j, and k contain the values 10, 3, and 20, respectively,
what is the value of the following logical expression: j < 4 || j == 5 && i <= k
A) 3
B) false
C) 20
D) true
25. After execution of the following code, what will be the value of angle if the input
value is 10?
n>>angle;
if(angle>5)
angle=angle+5;
elseif(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;
elseif(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;
elseif(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;//Assumeusertypes30
if((int1>100)&&(int2=50))
int3=int1+int2;
else
int3=int1int2;
cout<<int1<<''<<int2<<''<<int3;
A) 120 30 150
B) 120 30 90
C) 120 50 170
D) 120 50 70
E) 120 30 70
30. Consider the following If statement, which is syntactically correct but uses poor
style and indentation:
if(x>=y)if(y>0)x=x*y;elseif(y<4)x=x
y;
Assume that x and y are intvariables 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;elseif(y<4)x=x
y;
Assume that x and y are intvariables 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) Curly
B) Moe Larry Curly
C) Larry Curly
D) no output; there is a compile-time error
E) inFile!=myfile.dat
38. 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.
39. In order to test the boundaries of the following condition, what data values
would you use for the variable alpha? (alpha is of type int.)
alpha>=1
A) 0, 1, and INT_MAX
B) 1, 2, and INT_MAX
C) INT_MIN, 1, and INT_MAX
D) INT_MIN, 0, 1, and INT_MAX
E) INT_MIN, 1, 2, and INT_MAX
40. A(n) ____________________ expression is an expression composed of logical
(Boolean) values and operations.
41. The operators <, ==, >=, and != are examples of ____________________
operators. (Do not answer "binary.")
42. 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.
43. The operators &&, ||, and ! are known as ____________________ operators.
44. In programming languages that use ____________________ evaluation of a
logical expression, all subexpressions are evaluated before applying any logical
operators.
45. Write a C++ logical expression that is true if the variable testScore is greater
than or equal to 90 and less than or equal to 100: ____________________
71. Write a statement that stores a 0 in answer if one is greater than two.
72. Write a statement that stores 100 in answer if grade is greater than 100.
73. Write the statement that sets dataOk to True if data is less than or equal to 100.
74. If you want to have more than one statement executed within an If statement,
what syntax do you use?
75. What is a compound statement?
76. What is the safest way to compare data types such as ints, floats, and chars?
77. When placing an If statement within and If statement, a computer programmer is creating a
___________.
78. True or False? The following represents a list showing the correct order of precedence (from
highest to lowest) for the arithmetic, relational, and logical operators with the assignment
operator included as well
! Unary + Unary
/%
+
< <= > >=
== !=
&&
||
=
79. What does the following If-Then C ++ statement mean?
if (age < 18)
cout << Not an
voter. ,, endl;
eligible
cout
<<
80. True or False? Implementing a test plan guarantees that a program is completely correct.
Answer Key
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
True
False
False
True
False
False
False
False
False
True
True
False
False
True
False
B
D
C
C
E
B
D
B
D
D
E
D
A
C
D
E
C
A
E
A
B
C
B
D
logical (Boolean)
relational
short-circuit (conditional)
logical (Boolean)
full
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
or
dataOk=(data<=100);(preferredway)
74. Enclose the statements within braces.
75. Statements enclosed within braces that are treated as one statement
76. The safest approach is always to compare ints with ints, floats with floats,
chars with chars and so on. If one mixes data types in a comparison, implicit type coercion
takes place, similar to how it does in arithmetic expressions.
77. nested control structure
78. True
79. If age is less than 18, first print Not an eligible and then print voter. If age is not less
than 18, then skip the first output statement and go directly to voter.
80. False