Name: 
 

C++ Control structures



True/False
Indicate whether the statement is true or false.
 

 1. 

The characters in a string variable, as defined in the book using the apstring library, can be examined/modified using a for loop.
 

 2. 

When a true loop controls a condition that is never changed to false then an infinite loop is caused.
 

 3. 

Nesting of the three kinds of loops - do...while, while and for - are not permitted.
 

 4. 

A sentinal value is a value which can cause a loop exit, but is not controlled by a loop variable.
 

 5. 

A for loop is a pre-test loop.
 

 6. 

A while loop must always be executed at least once.
 

 7. 

A for loop can always be rewritten as a while loop.
 

 8. 

A while loop can always be rewritten as a do..while loop with the same results.
 

 9. 

Somewhere in a while loop, provision must be made for the value of the tested expression to be altered.
 

 10. 

Initial values for the variables in the tested expression of a while statement do not have to be initialized before the while statement is encountered.
 

 11. 

Sentinels are data values used to signal either the start or the end of a data series.
 

 12. 

Once a while loop is entered, the statements within the compound statement are executed as long as the tested condition is true.
 

 13. 

(; count <= 20;) is valid for the contents of a for statement’s parentheses.
 

 14. 

for loop variables can be declared and initialized within a for loop initializing list.
 

 15. 

A loop contained within another loop is a nested loop.
 

Multiple Choice
Identify the choice that best completes the statement or answers the question.
 

 16. 

Which is not a part of the for loop heading?
a.
initialization expression
c.
statement
b.
continuation condition
d.
update expression
 

 17. 

Which part of the for loop heading increments/decrements the control variable?
a.
update expression
c.
initialization expression
b.
continuation condition
d.
statement
 

 18. 

Which part of the for loop heading determines the upper bound of the control variable?
a.
initialization expression
c.
update expression
b.
statement
d.
continuation condition
 

 19. 

Which of these operators are unary?
a.
/
c.
+
b.
*
d.
++
 

 20. 

For the block of code below, which could be a precondition:totalSum = 0; ?
for (inc = 0; inc <= area  * 2; ++inc)
      totalSum = totalSum + (3.1415 * (1/4));
a.
totalSum  = 0
c.
inc = 0
b.
area * 2
d.
3.1415
 

 21. 

Which is not a loop structure?
a.
for
c.
while
b.
do..while
d.
select
 

 22. 

An accumulator is ____.
a.
data validator
c.
a variable that sums values
b.
a variable whose value never changes
d.
a variable that decrements values
 

 23. 

Which should not be an attribute of a loop control variable?
a.
double
c.
char
b.
int
d.
variable declaration
 

 24. 

Which of the following is an example of a properly written for statement?
a.
for  (int M = 1.5; M < 10; M--)
b.
for (int C = 2; C < 10; C)
c.
for ( int Num > 10; N = 0; N++)
d.
for (int J = 20; J < 2; J--)
 

 25. 

In a for loop, ____.
a.
a variable of type float can be used to control execution
b.
the loop variable must always be 1 the first time the loop is executed
c.
the loop stops executing when the terminating condition becomes false
d.
the loop stops executing when the terminating condition becomes true
 

 26. 

What is the output of the following for loop?
for (int  Y =  2; Y<=10; Y++)
cout <<Y << "  ";
a.
1  2  3  4  5  6  7  8  9  10
c.
2  4  6  8  10
b.
2  3  4  5  6  7  8  9  10
d.
2  3  4  5  6  7  8  9
 

 27. 

In a ____ loop, the loop control variable is usually increased by one each time through the loop.
a.
do...while
c.
for
b.
forward
d.
while
 

 28. 

How many times will this loop execute?
for (int a = 2; a <= 4; a++)
      for (int b = 1; b <= 5; b++)
         cout << a << "  " << b << endl;
a.
0
c.
10
b.
5
d.
15
 

 29. 

The real power of most computer programs resides in their ability to repeat the same calculation or sequence of instructions over and over, each time using different ____ , without the necessity of rerunning the program.
a.
data
c.
compilers
b.
programming languages
d.
interpreters
 

 30. 

The while statement literally loops back on itself to recheck the expression until ____.
a.
the program is terminated
c.
instructed to stop
b.
it evaluates to zero
d.
new data is entered
 

 31. 

Unless there is a change in the value of the tested expression of a while loop, the loop will ____.
a.
terminate after the first execution
c.
terminate immediately
b.
create an error message
d.
execute indefinitely
 

 32. 

Every time the statement sum = sum + num; is executed the previous value of sum is ____.
a.
saved
c.
stored
b.
lost
d.
tested
 

 33. 

Repeatedly adding numbers to a total usually involves interactive user input, ____ and an accumulator.
a.
an argument
c.
a repetition statement
b.
a parameter
d.
addition
 

 34. 

Sentinel values must be selected so as not to conflict with ____.
a.
legitimate data values
c.
other sentinels
b.
program logic
d.
stored data
 

 35. 

The ____ statement performs the same function as the while statement but uses a different form.
a.
if
c.
continue
b.
if-else
d.
for
 

 36. 

Within the parentheses of the for statement are ____ items separated by semicolons.
a.
2
c.
4
b.
3
d.
5
 

 37. 

Omitting the tested expression from a for loop results in ____.
a.
a crashed program
c.
a read error
b.
a print error
d.
an infinite loop
 

 38. 

All statements in a do loop are executed at least ____.
a.
once
c.
three times
b.
twice
d.
four times
 

 39. 

while and for statements are sometimes preferred to do statements because ____.
a.
they are shorter
c.
they are more efficient
b.
they are faster
d.
their expression testing is more visible
 

 40. 

The equality operator should not be used when testing ____ operands.
a.
integer
c.
character
b.
double-precision
d.
embedded
 

 41. 

Consider the following do while loop, whose job is to check for a valid customer identification number between the numbers 100 and 1999:
do
{
   cout << "\nEnter an identification number: ";
   cin >> idNum;
}
while ( // Missing expression goes here);

Which of the following is the correct missing expression?
a.
idNum >= 100 && idNum <= 1999
c.
idNum < 100 || idNum != 2000
b.
idNum < 100 || > 1999
d.
idNum < 100 || idNum > 1999
 

 42. 

Assume you need to write a code segment to output a product table showing all products between two variables, low and high. Here is the code, minus one line:
void products (int low, int high)
{
            int product = 0;
            for (int i = low; i<=high; i++)
            {
                        for (int j = low; j<= high; j++)
                        {
                                    // Missing line goes here
                                    cout << product;
                        }
                        cout << endl;
            }
}
What is the missing line?
a.
product = i+j;
c.
product = low*high;
b.
product = i*j;
d.
product = low+high
 

 43. 

Assume you need to write a posttest loop that asks the user for an integer. If the number is non-negative, add it to a sum.  If it is negative, stop the loop.  Output the sum.
Here is the code, minus one statement:
int sum = 0;
int number = 0;
do
{
            cout << "Input a number (negative to stop): ";
            cin >> number;
            // Missing statement goes here
} while (number>= 0);
cout << “The sum = ” << sum << endl;

What is the missing statement?
a.
if (number < 0)
   sum = sum + number;
b.
if (number > 0)
   sum = sum + number;
c.
if (number >= 0)
   sum = sum + number;
d.
if (number == 0)
   sum = sum + number;
 

Completion
Complete each statement.
 

 44. 

An example of a pretest loop with variable conditions is the ____________________ loop and an example of a posttest loop with variable conditions is the ____________________ loop.
 

 

 45. 

A posttest loop has a boolean condition checked ____________________ the loop body has been completed.
 

 

 46. 

Loops that execute a predetermined number of times are called ____________________.
 

 

 47. 

A loop that tests its controlling condition before the loop is executed for the first time is called a(n) ____________________.
 

 

 48. 

A do...while  loop is an example of a(n) ____________________.
 

 

 49. 

Assume that you are writing a program that prompts a person to enter his or her age. When you check the value entered to make certain it is between 1 and 120, you are performing data ____________________.
 

 

 50. 

A value used to indicate that all data has been used is called a(n) ____________________ value.
 

 



 
Check Your Work     Start Over