Deck 5: Loops and Files
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/60
Play
Full screen (f)
Deck 5: Loops and Files
1
The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon.
False
2
Multiple relational expressions cannot be placed into the test condition of a for loop.
True
3
In C++ 11 you can pass a string object as an argument to a file stream object's open member function.
True
4
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
A) decision statement
B) loop
C) cout object
D) selection structure
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
5
If you want to stop a loop before it goes through all of its iterations, the break statement may be used.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
6
The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
7
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
A) prefix increment
B) postfix increment
C) prefix decrement
D) postfix decrement
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
8
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) 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
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
9
An initialization expression may be omitted from the for loop if no initialization is required.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
10
The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
11
The update expression of a for loop can contain more than one statement, for example:
for(i = 5; i <= 10; i++, total+= sales)
for(i = 5; i <= 10; i++, total+= sales)
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
12
An output file is a file that data is written to.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
13
It is possible to define a file stream object and open a file in one statement.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
14
You may not use the break statement in a nested loop.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
15
You may nest while and do-while loops but you may not nest for loops.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
16
A while loop's body can contain multiple statements, as long as they are enclosed in braces.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
17
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
A) plus and minus
B) ++ and --
C) binary and unary
D) conditional and relational
E) binary and ternary
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
18
You may not use both break and continue statements within the same set of nested loops.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
19
string objects have a member function named c_str that returns the contents of the object formatted as a null-terminated C-string.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
20
A while loop is somewhat limited because the counter can only be incremented by one each time through the loop.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
21
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
A) break
B) terminate
C) re-iterate
D) continue
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
22
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
A) null value
B) infinite loop
C) unexpected exit
D) compiler error
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
23
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
A) at least once
B) at least twice
C) never
D) as many times as the user wishes
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
24
What will the following code display?
Int number = 6;
++number;
Cout << number << endl;
A) 6
B) 5
C) 7
D) 0
Int number = 6;
++number;
Cout << number << endl;
A) 6
B) 5
C) 7
D) 0
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
25
This means to increase a value:
A) decrement
B) increment
C) modulus
D) parse
E) None of these
A) decrement
B) increment
C) modulus
D) parse
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
26
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
While (x++ < 10)
A) ++
B) <
C) neither; the expression is invalid
D) cannot tell without the rest of the code
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
27
A for statement contains three expressions:
Initialization, test, and
A) update
B) reversal
C) null
D) validation
E) None of these
Initialization, test, and
A) update
B) reversal
C) null
D) validation
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
28
What will the following code display?
Int number = 6;
Cout << ++number << endl;
A) 6
B) 5
C) 7
D) 0
Int number = 6;
Cout << ++number << endl;
A) 6
B) 5
C) 7
D) 0
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
29
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
Int number = 6;
Int x = 0;
X = number--;
Cout << x << endl;
A) 6
B) 5
C) 7
D) 0
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
30
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
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
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
31
A statement that causes a loop to terminate early is
A) break
B) terminate
C) re-iterate
D) continue
E) None of these
A) break
B) terminate
C) re-iterate
D) continue
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
32
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
A) pre-test loop
B) post-test loop
C) null statement
D) infinite loop
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
33
What will the following code display?
Int number = 6;
Number++;
Cout << number << endl;
A) 6
B) 5
C) 7
D) 0
Int number = 6;
Number++;
Cout << number << endl;
A) 6
B) 5
C) 7
D) 0
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
34
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
A) constant
B) counter
C) control
D) null terminator
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
35
The while loop is a __________ loop.
A) post-test
B) pre-test
C) infinite
D) limited
E) None of these
A) post-test
B) pre-test
C) infinite
D) limited
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
36
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
Int number = 6;
Int x = 0;
X = --number;
Cout << x << endl;
A) 6
B) 5
C) 7
D) 0
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
37
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
A) constant
B) counter
C) variable
D) sentinel
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
38
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
A) postfix
B) prefix
C) preliminary
D) binary
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
39
In a for statement, this expression is executed only once:
A) the test
B) null
C) initialization
D) validation
E) None of these
A) the test
B) null
C) initialization
D) validation
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
40
What will the following code display?
Int number = 6;
Cout << number++ << endl;
A) 6
B) 5
C) 7
D) 0
Int number = 6;
Cout << number++ << endl;
A) 6
B) 5
C) 7
D) 0
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
41
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;
A) write(outFile, number);
B) outFile >> number;
C) outFile << number;
D) number >> outFile;
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
42
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
For (int i = 0; i <= 20; i++)
Cout << "Looping again!" << endl;
A) 20
B) 19
C) 21
D) an infinite number of times
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
43
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
For (int i = 20; i > 0; i--)
Cout << "Looping!" << endl;
A) 20
B) 19
C) 21
D) an infinite number of times
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
44
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
A) cout object
B) pen object
C) output object
D) stream insertion operator
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
45
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
A) while
B) for
C) switch
D) do-while
E) pre-test
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
46
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
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
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
47
To write read data from a file, you define an object of the __________ data type.
A) inputFile
B) ifstream
C) fstream
D) ofstream
A) inputFile
B) ifstream
C) fstream
D) ofstream
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
48
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
A) name
B) number
C) buffer
D) segment
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
49
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) a pre-test loop
B) a post-test loop
C) an infinite loop
D) a counter-controlled loop
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
50
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
A) while
B) for
C) switch
D) do-while
E) pre-test
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
51
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
For (int i = 0; i < 20; i++)
Cout << "Hello world!" << endl;
A) 20
B) 19
C) 21
D) an infinite number of times
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
52
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
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
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
53
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
A) while
B) infinite
C) switch
D) do-while
E) post-test
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
54
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
A) constant
B) function
C) variable
D) new data type
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
55
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
A) infinite loop
B) pre-test loop
C) post-test loop
D) nested loop
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
56
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
A) while
B) for
C) switch
D) do-while
E) None of these
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
57
A file must be __________ before data can be written to or read from it.
A) opened
B) closed
C) named
D) buffered
E) initialized
A) opened
B) closed
C) named
D) buffered
E) initialized
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
58
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
A) #include file
B) #include fileaccess
C) #include fstream
D) #include cfile
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
59
To write data to a file, you define an object of the __________ data type.
A) outputFile
B) ifstream
C) fstream
D) ofstream
A) outputFile
B) ifstream
C) fstream
D) ofstream
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
60
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
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
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck