Hogan Chapter 5
These are operators that add and subtract one from their operands. plus and minus ++ and -- binary and unary conditional and relational
++ and --
What is the output of the following code segment? n = 1; for ( ; n <= 5; ) cout << n << " "; n++; 1 2 3 4 5 1 1 1 ... and on forever 2 3 4 5 6 1 2 3 4 2 3 4 5
1 1 1 ... and on forever
What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' '; n++; 1 2 3 4 5 1 1 1... and on forever 2 3 4 5 6 1 2 3 4 5 2 3 4 5
1 1 1... and on forever
How many times will the following loop display "Hello"? for (int i = 1; i < 20; i++) cout << "Hello!" << endl; 20 19 21 An infinite number of times
19
How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl; 20 19 21 An infinite number of times
20
How many times will the following loop display "Hello"? for (int i = 20; i > 0; i--) cout << "Hello!" << endl; 20 19 21 An infinite number of times
20
How many times will the following loop display "Hello"? for (int i = 0; i <= 20; i++) cout << "Hello!" << endl; 20 19 21 An infinite number of times
21
Question 32 0.2 / 0.2 pts What will the following code display? int number = 6 int x = 0; x = --number; cout << x << endl; 6 5 7 0
5
Question 31 0.2 / 0.2 pts What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl; 6 5 7 0
6
What will the following code display? int number = 6; cout << number++ << endl; 6 5 7 0
6
Question 30 0.2 / 0.2 pts What will the following code display? int number = 6; cout << ++number << endl; 6 5 7 0
7
What will the following code display? int number = 6; ++number; cout << number << endl; 6 5 7 0
7
What will the following code display? int number = 6; number++; cout << number << endl; 6 5 7 0
7
Look at the following statement. while (x++ < 10) Which operator is used first? ++ < Neither. The expression is invalid. No answer text provided.
<
A while loop is somewhat limited, because the counter can only be incremented by one each time through the loop. True False
False
The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon. True False
False
The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions. True False
False
The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop. True False
False
A while loop's body can contain multiple statements, as long as they are enclosed in braces. True False
True
An initialization expression may be omitted from the for loop if no initialization is required. True False
True
An output file is a file that data is written to. True False
True
If you want to stop a loop before it goes through all its iterations, the break statement may be used. True False
True
A loop that is inside another loop is called: an infinite loop a pre-test loop a post-test loop a nested loop
a nested loop
The while loop has two important parts: an expression that is tested for a true or false value, and: a statement or block that is repeated as long as the expression is true a statement or block that is repeated only if the expression is false one line of code that is repeated once, if the expression is true 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
What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; } a. 0 c. 01 2 3 4 1 2 3 4 5 b. 0 d. The loop will display numbers 1 starting at 0, for infinity. 2 3 4 a b c d
b
This statement causes a loop to terminate early. stop break null terminate
break
Assuming dataFile is a file stream object, the statement dataFile.close(); is illegal in C++ needs a filename argument to execute correctly closes a file is legal but risks losing valuable data
closes a file
This statement may be used to stop a loop's current iteration and begin the next one. break terminate return continue
continue
This is a variable that is regularly incremented or decremented each time a loop iterates. constant counter control statement null terminator
counter
What will the following code display? int x = 0; for (int count = 0; count < 3; count++) x += count; cout << x << endl; a. 0 c. 6 1 2 b. 0 d. 3 a b c d
d
If you want a user to enter exactly 20 values, which loop would be the best to use? do-while for while infinite
for
This loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop. do-while while for infinite
for
To allow file access in a program, you must #include this header file. file fileaccess fstream cfil
fstream
This means to increase a value by one. decrement increment modulus parse
increment
Something within a while loop must eventually cause the condition to become false, or a(n) __________ results. null value infinite loop unexpected exit compiler error
infinite loop
The while loop contains an expression that is tested for a true or false value, and a statement or block that is repeated as long as the expression is false is true does not evaluate to true or false evaluates to true or false
is true
This is a control structure that causes a statement or group of statements to repeat. decision statement constant loop cout object
loop
If you place a semicolon after the test expression in a while loop, it is assumed to be a(n): pre-test loop post-test loop null statement infinite loop
null statement
To write data to a file, you define an object of this data type. outputFile ifstream fstream ofstream
ofstream
Assume a file stream object has been created when answering the following question. A file must be ________ to the file stream object before data can be written to or read from it. closed and unlinked opened and linked buffered and attached initialized and introduced
opened and linked
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? write(outFile, number); outFile >> number; outFile << number; number >> outFile;
outFile << number;
The do-while loop is a __________ loop that is ideal in situations where you always want the loop to iterate at least once. post-test pre-test infinite null-terminated
post-test
The do-while loop is considered a(n) _________ loop. pre-test post-test infinite limited
post-test
The while loop is this type of loop? post-test pre-test infinite limited
pre-test
When the increment operator precedes its operand, as in ++num1, the expression is in this mode. postfix prefix preliminary binary
prefix
When used as part of an expression and/or in an assignment operation this operator increments the value of its operand before the operand is used in the expression, or assignment operation. prefix increment postfix increment prefix decrement postfix decrement
prefix increment
This is a special value that marks the end of a list of values. constant variable loop sentinel
sentinel
A for statement contains three expressions: initialization, test, and update reversal null validation
update
You may define a __________ in the initialization expression of a for loop. constant function variable new data type
variable
This is a pre-test loop that is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. do-while while for infinite
while