Introduction to C++ Programming, Y. Daniel Liang
Chapter 3 Selection Statements
Section 3.2 boolean Data Type and Operations
1
The "less than or equal to" comparison operator is __________.
A.
<
B.
<=
C.
=<
D.
<<
E.
!=
2
The equal comparison operator is __________.
A.
<>
B.
!=
C.
==
D.
^=
3
The word true is ________.
A.
a C++ keyword
B.
a Boolean literal
C.
same as value 1
D.
same as value 0
Section 3.3 if Statements
4
_______ is the code with natural language mixed with Java code.
A.
A program
B.
A statement
C.
Pseudocode
D.
A flowchart diagram
5
Which of the following code displays the area of a circle if the radius is positive.
    if (radius != 0) cout << radius * radius * 3.14159;
    if (radius >= 0) cout << radius * radius * 3.14159;
    if (radius > 0) cout << radius * radius * 3.14159;
    if (radius <= 0) cout << radius * radius * 3.14159;
A.
B.
C.
D.
Section 3.4 Logical Operators
6
Which of the Boolean expressions below is incorrect?
A.
(true) && (3 => 4)
B.
!(x > 0) && (x > 0)
C.
(x > 0) || (x < 0)
D.
(x != 0) || (x = 0)
E.
(-10 < x < 0)
7
To check whether a char variable ch is an uppercase letter, you write ___________.
A.
(ch >= 'A' && ch >= 'Z')
B.
(ch >= 'A' && ch <= 'Z')
C.
(ch >= 'A' || ch <= 'Z')
D.
('A' <= ch <= 'Z')
8
Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?
A.
1 < x < 100 && x < 0
B.
((x < 100) && (x > 1)) || (x < 0)
C.
((x < 100) && (x > 1)) && (x < 0)
D.
(1 > x > 100) || (x < 0)
9
Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x++ > 10)?
A.
9
B.
10
C.
11
10
Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10).
A.
9
B.
10
C.
11
11
Analyze the following code:
if (x < 100) && (x > 10)
   cout << "x is between 10 and 100" << endl;
A.
The statement has syntax errors because (x<100) & (x > 10) must be enclosed inside parentheses.
B.
The statement has syntax errors because (x<100) & (x > 10) must be enclosed inside parentheses and the println(?) statement must be put inside a block.
C.
The statement compiles fine.
D.
The statement compiles fine, but has a runtime error.
E.
& should be replaced by && to avoid having a syntax error.
Section 3.5 if . . . else Statements
12
Analyze the following code:
bool even = false;
if (even = true)
{
   cout << "It is even!" << endl;
}
A.
The program has a syntax error.
B.
The program has a runtime error.
C.
The program runs fine, but displays nothing.
D.
The program runs fine and displays It is even!.
13
Analyze the following code:
Code 1:
bool even;
if (number % 2 == 0)
   even = true;
else
   even = false;
Code 2:
bool even = (number % 2 == 0);
A.
Code 1 has syntax errors.
B.
Code 2 has syntax errors.
C.
Both Code 1 and Code 2 have syntax errors.
D.
Both Code 1 and Code 2 are correct, but Code 2 is better.
Section 3.6 Nested if Statements
14
Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement? (Please indent the statement correctly first.)
if (x > 0)
    if (y > 0)
       cout << "x > 0 and y > 0" << endl;
else if (z > 0)
       cout << "x < 0 and z > 0" << endl;
A.
x > 0 and y > 0;
B.
x < 0 and z > 0;
C.
x < 0 and z < 0;
D.
no printout.
15
Suppose income is 4001, what is the output of the following code:
if (income > 3000)
{
   cout << "Income is greater than 3000" << endl;
}
else if (income > 4000)
{
   cout << "Income is greater than 4000" << endl;
}
A.
no output
B.
Income is greater than 3000
C.
Income is greater than 3000 followed by Income is greater than 4000
D.
Income is greater than 4000
E.
Income is greater than 4000 followed by Income is greater than 3000
Section 3.9 switch Statements
16
What is y after the following switch statement is executed?
x = 3;
switch (x + 3)
{
   case 6: y = 0;
   case 7: y = 1;
   default: y += 1;
}
A.
1
B.
2
C.
3
D.
4
17
What is the printout of the following switch statement?
     char ch = 'a';
    
     switch (ch)
     {
       case 'a':
       case 'A':
         cout << ch << endl; break;
       case 'b':
       case 'B':
         cout << ch << endl; break;
       case 'c':
       case 'C':
         cout << ch << endl; break;
       case 'd':
       case 'D':
         cout << ch << endl;
     }
A.
abcd
B.
a
C.
aa
D.
ab
18
What is the printout of the following switch statement?
     char ch = 'b';
    
     switch (ch)
     {
       case 'a':
         cout << ch << endl;
       case 'b':
         cout << ch << endl;
       case 'c':
         cout << ch << endl;
       case 'd':
         cout << ch << endl;
     }
A.
abcd
B.
bcd
C.
b
D.
bb
E.
bbb
19
Analyze the following program fragment:
int x;
double d = 1.5;
switch (d)
{
   case 1.0: x = 1;
   case 1.5: x = 2;
   case 2.0: x = 3;
}
A.
The program has a syntax error because the required break statement is missing in the switch statement.
B.
The program has a syntax error because the required default case is missing in the switch statement.
C.
The switch control variable cannot be double.
D.
No errors.
Section 3.9 Conditional Expressions
20
What is y after the following statement is executed?
x = 0;
y = (x > 0) ? 10 : -10;
A.
-10
B.
0
C.
10
D.
20
E.
Illegal expression
21
Analyze the following code fragments that assign a Boolean value to the variable even.
Code 1:
if (number % 2 == 0)
   even = true;
else
   even = false;
Code 2:
even = (number % 2 == 0) ? true: false;
Code 3:
even = number % 2 == 0;
A.
Code 2 has a syntax error, because you cannot have true and false literals in the conditional expression.
B.
Code 3 has a syntax error, because you attempt to assign number to even.
C.
All three are correct, but Code 1 is preferred.
D.
All three are correct, but Code 2 is preferred.
E.
All three are correct, but Code 3 is preferred.
Section 3.11 Formatting Output
22
You can use _______ to set the width of a print field.
A.
setw(width)
B.
setprecision(n)
C.
fixed
D.
showpoint
E.
left
23
Which of the following is a stream manipulator function?
A.
setw(width)
B.
setprecision(n)
C.
fixed
D.
showpoint
E.
left
24
Which of the following justifies the output to the left?
A.
setw(width)
B.
right
C.
fixed
D.
showpoint
E.
left
25
The following code displays ________.
cout << "a" << setw(6) << "abc";
A.
a abc
B.
aabc
C.
a
D.
abc
26
The following code displays ________.
cout << "a" << setw(6) << left << "abc";
A.
a abc
B.
aabc
C.
a
D.
abc
Section 3.12 Operator Precedence and Associativity
27
The order of the precedence (from high to low) of the operators +, *, &&, ||, & is:
A.
&&, ||, &, *, +
B.
*, +, &&, ||, &
C.
*, +, &, &&, ||
D.
*, +, &, ||, &&
E.
&, ||, &&, *, +
28
Which of the following operators are right-associative.
A.
*
B.
+
C.
%
D.
&&
E.
=
29
Which of the following operators are left-associative.
A.
*
B.
+
C.
%
D.
&&
E.
=
30
What is the value of the following expression?
true || true && false
A.
true
B.
false
31
When Java evaluates 1 + 2 + 3 + (4 + 5) + 6 * 7, which operation is performed first?
A.
4 + 5
B.
6 * 7
C.
1 + 2
D.
2 + 3
32
According to the Java expression evaluation rule, Which of the following operator in the expression 3 + 4 + 4 * 5 is executed first?
A.
the first +.
B.
the second +.
C.
the *.
D.
It could be either the first + or the *.