搜档网
当前位置:搜档网 › Chapter3_6e

Chapter3_6e

Chapter3_6e
Chapter3_6e

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

a.or

b.and

c.not

d.none of the above

ANSWER: A

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 << "other is odd\n";

else

cout << "other is even\n";

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)))

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 << "less than 10 ";

else

cout << "large\n";

a.less than 10

b.nothing

https://www.sodocs.net/doc/e52129066.html,rge

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 <<"Type y for yes on n for no\n";

cin >> ans;

switch (ans)

{

case 'y':

case 'Y': cout << "You said yes\n"; break;

case 'n':

case 'N': cout << "You said no\n"; break;

default: cout <<"invalid answer\n";

}

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

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)

{

x =13;

}

a.10

b.9

c.13

d.11

ANSWER: C

19.How many times is "Hi" printed to the screen

for(int i=0;i<14;i++);

cout <<"Hi\n";

a.13

b.15

c.14

d. 1

ANSWER: D

20.Given the following code, what is the final value of i?

int i;

for(i=0; i<=4;i++)

{

cout << i << endl;

}

a. 3

b. 4

c. 5

d.0

ANSWER: C

21.Given the following code, what is the final value of i?

int i,j;

for(i=0;i<4;i++)

{

for(j=0;j<3;j++)

{

if(i==2)

break;

}

}

a. 3

b. 4

c. 5

d.0

ANSWER: B

22.Which of the following is not a good reason for choosing a certain loop control?

a.What the loop does

b.The minimum number of iterations of the loop

c.The condition for ending the loop

d.If the loop is in a function

ANSWER: D

23.If you want a loop to quit iterating if x < 10 and y > 3, what would be the proper

loop condition test?

a.(x < 10 && y > 3)

b.(x >10 || y < 3)

c.(x >=10 && y <=3)

d.(x >=10 || y <=3)

ANSWER: D

24.If you need to write a do-while loop that will ask the user to enter a number

between 2 and 5 inclusive, and will keep asking until the user enters a correct number, what is the loop condition?

a.(2<=num<=5)

b.(2<5

c.(2 <= number && number <= 5)

d.(2 < number || number > 5)

e.(2 > number && number > 5)

ANSWER: D

25.Which loop structure always executes at least once?

a.do-while

b.for

c.while

d.sentinel

ANSWER: A

26.Which of the following are allowed in the third section of the for loop statement?

a.i++

b.i--

c.i +=2

d.cout << "Hello\n"

e.all of the above

f.none of the above

ANSWER: E

27.Which of the following data types may be used in a switch statement?

a.int

b.char

c.enum

d.long

e.all of the above

f. a and d

ANSWER: E

28.Which of the following are valid case statements in a switch?

a.case 1:

b.case x<4:

c.case 'ab':

d.case 1.5:

ANSWER: A

29.When testing a program with a loop, which of the following tests should be done?

a.no iterations of the loops

b.one less than the maximum number of iterations

c.the maximum number of iterations

d.one more than the maximum number of iterations

e. A and B

f.A, B and C

ANSWER: F

30.What is wrong with the following for loop?

for(int i=0;i<10;i--)

{

cout << "Hello\n";

}

a.can not use a for-loop for this

b.i is not initialized

c.infinite loop

d.off-by-one error

ANSWER: C

相关主题