Test Bank Docx Loops And Files Ch.5 Gaddis - Test Bank | C++ Control Structures 9e by Tony Gaddis. DOCX document preview.

Test Bank Docx Loops And Files Ch.5 Gaddis

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis)

Chapter 5 Loops and Files

TRUE/FALSE

1. The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions.

2. A while loop's body can contain multiple statements, as long as they are enclosed in braces.

3. A while loop is somewhat limited because the counter can only be incremented by one each time through the loop.

4. An initialization expression may be omitted from the for loop if no initialization is required.

5. The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.

6. If you want to stop a loop before it goes through all of its iterations, the break statement may be used.

7. You may not use both break and continue statements within the same set of nested loops.

8. The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon.

9. The update expression of a for loop can contain more than one statement, for example:

for(i = 5; i <= 10; i++, total+= sales)

10. Multiple relational expressions cannot be placed into the test condition of a for loop.

11. You may nest while and do-while loops but you may not nest for loops.

12. You may not use the break statement in a nested loop.

13. An output file is a file that data is written to.

14. It is possible to define a file stream object and open a file in one statement.

15. In C++ 11 you can pass a string object as an argument to a file stream object's open member function.

16. string objects have a member function named c_str that returns the contents of the object formatted as a null-terminated C-string.

MULTIPLE CHOICE

1. These are operators that add and subtract one from their operands.

a.

plus and minus

b.

++ and --

c.

binary and unary

d.

conditional and relational

e.

binary and ternary

2. This operator increments the value of its operand and then uses the value in context.

a.

prefix increment

b.

postfix increment

c.

prefix decrement

d.

postfix decrement

e.

None of these

3. This is a control structure that causes a statement or group of statements to repeat.

a.

decision statement

b.

loop

c.

cout object

d.

selection structure

e.

None of these

4. The two important parts of a while loop are the expression that is tested for a true or false value and

a.

a statement or block that is repeated as long as the expression is true

b.

a statement or block that is repeated only if the expression is false

c.

one line of code that is repeated once, if the expression is true

d.

a statement or block that is repeated once, if the expression is true

5. Something within a while loop must eventually cause the condition to become false or a(n) __________ results.

a.

null value

b.

infinite loop

c.

unexpected exit

d.

compiler error

e.

None of these

6. The while loop is a __________ loop.

a.

post-test

b.

pre-test

c.

infinite

d.

limited

e.

None of these

7. If you place a semicolon after the test expression in a while loop, it is assumed to be a(n)

a.

pre-test loop

b.

post-test loop

c.

null statement

d.

infinite loop

e.

None of these

8. The statements in the body of a while loop may never be executed while the statements in the body of a do-while loop will be executed

a.

at least once

b.

at least twice

c.

never

d.

as many times as the user wishes

e.

None of these

9. When the increment operator precedes its operand, as ++num, the expression is in __________ mode.

a.

postfix

b.

prefix

c.

preliminary

d.

binary

e.

None of these

10. This means to increase a value:

a.

decrement

b.

increment

c.

modulus

d.

parse

e.

None of these

11. A statement that may be used to stop a loop's current iteration and begin the next one is

a.

break

b.

terminate

c.

re-iterate

d.

continue

e.

None of these

12. A statement that causes a loop to terminate early is

a.

break

b.

terminate

c.

re-iterate

d.

continue

e.

None of these

13. A variable that is regularly incremented or decremented each time a loop iterates is a

a.

constant

b.

counter

c.

control

d.

null terminator

e.

None of these

14. A special value that marks the end of a list of values is a

a.

constant

b.

counter

c.

variable

d.

sentinel

e.

None of these

15. What is the output of the following code segment?

n = 1;

while (n <= 5)

cout << n << ' ';

n++;

a.

1 2 3 4 5

b.

1 1 ... and on forever

c.

1 2 3 4 5 6

d.

1 2 3 4

e.

2 3 4 5

16. In the following statement, which operator is used first?

while (x++ < 10)

a.

++

b.

<

c.

neither; the expression is invalid

d.

cannot tell without the rest of the code

17. What will the following code display?

int number = 6;

number++;

cout << number << endl;

a.

6

b.

5

c.

7

d.

0

18. What will the following code display?

int number = 6;

++number;

cout << number << endl;

a.

6

b.

5

c.

7

d.

0

19. What will the following code display?

int number = 6;

cout << number++ << endl;

a.

6

b.

5

c.

7

d.

0

20. What will the following code display?

int number = 6;

cout << ++number << endl;

a.

6

b.

5

c.

7

d.

0

21. What will the following code display?

int number = 6;

int x = 0;

x = number--;

cout << x << endl;

a.

6

b.

5

c.

7

d.

0

22. What will the following code display?

int number = 6;

int x = 0;

x = --number;

cout << x << endl;

a.

6

b.

5

c.

7

d.

0

23. A for statement contains three expressions: initialization, test, and

a.

update

b.

reversal

c.

null

d.

validation

e.

None of these

24. In a for statement, this expression is executed only once:

a.

the test

b.

null

c.

initialization

d.

validation

e.

None of these

25. You may define a __________ in the initialization expression of a for loop.

a.

constant

b.

function

c.

variable

d.

new data type

e.

None of these

26. The do-while loop is considered

a.

a pre-test loop

b.

a post-test loop

c.

an infinite loop

d.

a counter-controlled loop

e.

None of these

27. The __________ loop is ideal in situations where you want the loop to iterate at least once.

a.

while

b.

for

c.

switch

d.

do-while

e.

pre-test

28. The __________ loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop.

a.

while

b.

for

c.

switch

d.

do-while

e.

pre-test

29. The __________ loop is a good choice when you do not want the loop to iterate if the condition is false in the beginning.

a.

while

b.

infinite

c.

switch

d.

do-while

e.

post-test

30. If you want a user to enter exactly 20 values, which loop would be the best to use?

a.

while

b.

for

c.

switch

d.

do-while

e.

None of these

31. What will the following code display?

int x = 0;

while (x < 5)

{

cout << x << " ";

x++;

}

a.

0 1 2 3 4 5

b.

0 1 2 3 4

c.

0

1

2

3

4

d.

This is an infinite loop

32. What will the following code display?

int x = 0;

for (int count = 0; count < 3; count++)

x += count;

cout << x << endl;

a.

0

1

2

b.

0

c.

6

d.

3

33. How many times will the following loop display "Hello world!"?

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

cout << "Hello world!" << endl;

a.

20

b.

19

c.

21

d.

an infinite number of times

34. How many times will the following loop display "Looping again!"?

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

cout << "Looping again!" << endl;

a.

20

b.

19

c.

21

d.

an infinite number of times

35. How many times will the following loop display "Looping!"?

for (int i = 20; i > 0; i--)

cout << "Looping!" << endl;

a.

20

b.

19

c.

21

d.

an infinite number of times

36. A loop that is inside another loop is called a(n)

a.

infinite loop

b.

pre-test loop

c.

post-test loop

d.

nested loop

e.

None of these

37. A file must be __________ before data can be written to or read from it.

a.

opened

b.

closed

c.

named

d.

buffered

e.

initialized

38. A file __________ is a small holding section of memory that file-bound information is first written to.

a.

name

b.

number

c.

buffer

d.

segment

e.

None of these

39. To write information to a file, use the

a.

cout object

b.

pen object

c.

output object

d.

stream insertion operator

e.

None of these

40. To allow file access in a program, you must use __________ in the header file.

a.

#include file

b.

#include fileaccess

c.

#include fstream

d.

#include cfile

41. To write data to a file, you define an object of the __________ data type.

a.

outputFile

b.

ifstream

c.

fstream

d.

ofstream

42. To write read data from a file, you define an object of the __________ data type.

a.

inputFile

b.

ifstream

c.

fstream

d.

ofstream

43. Assuming outFile is a file stream object and number is a variable, which statement writes the contents of number to the file associated with outFile?

a.

write(outFile, number);

b.

outFile >> number;

c.

outFile << number;

d.

number >> outFile;

44. Assuming dataFile is a file stream object, the following statement:

dataFile.close();

a.

is illegal in C++

b.

needs a filename argument to execute correctly

c.

closes a file

d.

is legal but risks losing valuable data

e.

None of these

Document Information

Document Type:
DOCX
Chapter Number:
5
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 5 Loops And Files
Author:
Tony Gaddis

Connected Book

Test Bank | C++ Control Structures 9e

By Tony Gaddis

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party