Test Bank for Problem Solving with C++: The Object of Programming, 10/e Chapter 3 More Flow of Control
TRUE/FALSE
1. A boolean expression may evaluate to more than 2 values ANSWER: FALSE
2. A function may return a boolean value. ANSWER: TRUE
3. In an enumerated data type, different constants may not have the same value. ANSWER: FALSE
4. The compiler always pairs an else with _______________
ANSWER: the nearest previous if not already paired with an else. 5. All switch statements can be converted into nested if-else statements ANSWER: TRUE
6. All nested if-else statements can be converted into switch statements. ANSWER: FALSE
7. A break statement in a switch stops your program. ANSWER: FALSE
8. It is illegal to make function calls inside a switch statement. ANSWER: FALSE
9. A semicolon by itself is a valid C++ statement. ANSWER: TRUE
10. The break statement causes all loops to exit. ANSWER: FALSE Short Answer
1. A ____________ expression is an expression that can be thought of as being true or false.
ANSWER: boolean
2. _________ is a type whose values are defined by a list of constants of type int. ANSWER: enumerated data type
3. The code following the ________ case is executed if none of the other cases are matched in a switch statement. ANSWER: default
4. A compound statement that contains variable declarations is called a __________. ANSWER: block
5. Variables defined inside a set of braces are said to be _______ to that block of code.
ANSWER: local
6. Each repetition of a loop body is called ____________. ANSWER: an iteration
7. A _________ loop always executes the loop body at least once, irregardless of the loop condition.
ANSWER: do-while
8. A switch statement variable must be ________
ANSWER: an integer, bool, char or enumerated type
9. A loop that iterates one too many or one too few times is said to be ________ ANSWER: off by one
Multiple Choice
Test Bank for Problem Solving with C++: The Object of Programming, 10/e Chapter 3 More Flow of Control
1. Which boolean operation is described by the following table? A B Operation True True True True False True False True True False False False a. or b. and c. not
d. none of the above ANSWER: A
2. Which boolean operation is described by the following table? A B Operation True True True True False False False True False False False False a. or b. and c. not
d. none of the above ANSWER: B
3. Which of the following symbols has the highest precedence?
a. ++ b. || c. && d. -
ANSWER: A
4. If a programming language does not use short-circuit evaluation, what is the output of the following code fragment if the value of myInt is 0?
int other=3, myInt;
if(myInt !=0 && other % myInt !=0) cout << \else cout << \
a. other is even b. other is odd c. 0
d. run-time error, no output ANSWER: D
5. What is the value of the following expression? (true && (4/3 || !(6)))
Test Bank for Problem Solving with C++: The Object of Programming, 10/e Chapter 3 More Flow of Control
a. true b. false c. 0
d. illegal syntax ANSWER: A
6. if x is 0, what is the value of (!x ==0)?
a. false b. true
c. unable to determine d. A ANSWER: A
7. Which of the following are equivalent to (!(x<15 && y>=3))?
a. (x>15 && y<=3) b. (x>=15 && y < 3) c. (x>=15 || y < 3) d. (x>15 || y < 3) e. C and D ANSWER: C
8. Which of the following boolean expressions tests to see if x is between 2 and 15 (including 2 and 15)?
a. (x<=15 || x>=2) b. (2 <=x || x <=15) c. (x >=2 && x <=15) d. (2 <= x <= 15) ANSWER: C
9. Given the following enumerated data type definition, what is the value of SAT? enum myType{SUN,MON,TUE,WED,THUR,FRI,SAT,NumDays};
a. 7 b. 6 c. 8 d. 5
e. unknown ANSWER: b
10. Given the following enumerated data type definition, what is the value of SAT? enum myType{SUN=3,MON=1,TUE=3,WED,THUR,FRI,SAT,NumDays};
a. 7 b. 6 c. 8 d. 5
e. unknown ANSWER: A
11. What is the output of the following code fragment if x is 15? if(x < 20) if(x <10) cout << \ else
Test Bank for Problem Solving with C++: The Object of Programming, 10/e Chapter 3 More Flow of Control
cout << \
a. less than 10 b. nothing c. large
d. no output, syntax error ANSWER: C
12. What is the output of the following code fragment? int i=5; switch(i) {
case 0: i=15;break; case 1: i=25;break; case 2: i=35;break; case 3: i=40; default: i=0; }
cout << i < a. 15 b. 25 c. 35 d. 40 e. 0 f. 5 ANSWER: E 13. What is wrong with the following switch statement? int ans; cout <<\cin >> ans; switch (ans) { case 'y': case 'Y': cout << \case 'n': case 'N': cout << \default: cout <<\} a. ans is a int b. break; is illegal syntax c. nothing d. there are no break statements on 2 cases. ANSWER: A 14. Which of the following data types can be used in a switch controlling expression? a. int b. char Test Bank for Problem Solving with C++: The Object of Programming, 10/e Chapter 3 More Flow of Control c. float d. enum e. double f. d and e g. a and b h. a,b and d i. all of the above ANSWER: H 15. What is the output of the following code fragment? int x=0; { int x=13; cout << x <<\ } cout << x << endl; a. 13,13 b. 0,13 c. 13,0 d. nothing, there is a syntax error. ANSWER: C 16. What is the output of the following code fragment? { int x=13; cout << x <<\ } cout << x << endl; a. 13,13 b. 0,13 c. 13,0 d. nothing, there is a syntax error. ANSWER: D 17. What is the value of x after the following code executes? int x=10; if(x++ >10) { x =13; } a. 10 b. 9 c. 13 d. 11 ANSWER: A 18. What is the value of x after the following code executes? int x=10; if( ++x >10) {
用C++解决问题第十版Chapter 3



