C++ Chapter 5 Loops and Files

Ace your homework & exams now with Quizwiz!

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

for

A statement that causes a loop to terminate early is a. break b. terminate c. re-iterate d. continue e. None of these

break

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

buffer

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

false

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

false

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

false

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

false

You may not use the break statement in a nested loop

false

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

for

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

loop

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

nested loop

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

null statement

To write data to a file, you define an object of the __________ data type. a. outputFile b. ifstream c. fstream d. ofstream

ofstream

A file must be __________ before data can be written to or read from it. a. opened b. closed c. named d. buffered e. initialized

opened

The update expression of a for loop can contain more than one statement, for example: for(i = 5; i <= 10; i++, total+= sales)

true

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

true

A for statement contains three expressions: initialization, test, and a. update b. reversal c. null d. validation e. None of these

update

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

variable

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

false

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

#include fstream

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

++ and --

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

0 1 2 3 4

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

1 1 ... and on forever

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

20

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

20

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

21

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

3

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

5

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

6

What will the following code display? int number = 6; cout << number++ << endl; a. 6 b. 5 c. 7 d. 0

6

What will the following code display? int number = 6; ++number; cout << number << endl; a. 6 b. 5 c. 7 d. 0

7

What will the following code display? int number = 6; cout << ++number << endl; a. 6 b. 5 c. 7 d. 0

7

What will the following code display? int number = 6; number++; cout << number << endl; a. 6 b. 5 c. 7 d. 0

7

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

<

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

a post-test loop

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

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

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

at least once

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

closes a file

. 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

continue

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

counter

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

do-while

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

false

To write read data from a file, you define an object of the __________ data type. a. inputFile b. ifstream c. fstream d. ofstream

ifstream

This means to increase a value: a. decrement b. increment c. modulus d. parse e. None of these

increment

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

infinite loop

In a for statement, this expression is executed only once: a. the test b. null c. initialization d. validation e. None of these

initialization

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;

outFile << number;

The while loop is a __________ loop. a. post-test b. pre-test c. infinite d. limited e. None of these

pre-test

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

prefix

. 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

prefix increment

. 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

sentinel

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

stream insertion operator

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

true

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

true

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

true

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

true

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

true

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

true

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

true

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

while


Related study sets

Chapter 35: Acute Coronary Syndromes

View Set

Chapter 1 Business Finance Smart Book

View Set

AP Gov Unit 5 - College Board Review

View Set

Practice MCQ Questions from FINAL REVIEW + LECTURES

View Set

Coltman - Environmental Biology Test 3

View Set