Name:
Class:
Date:
Page 1
Indicate whether the statement is true or false.
1. With nested repetition structures, both loops must be the same type (example: both must be pretest loops, or both must
be posttest loops).
a.
True
b.
False
2. The algorithm for a clock uses a nested repetition structure.
a.
True
b.
False
3. A pretest loop and posttest loop produce the same results; they just use different methods.
a.
True
b.
False
4. The output of the following code is sumX: 3 sumY: 2.
int sumX = 0;
int sumY = 0;
do
{
sumX++;
sumY++;
sumX += sumY;
} while (sumX < 2);
cout << “sumX: << sumX << ” sumY: ” << sumY;
a.
True
b.
False
5. C++ provides the do until statement for coding a posttest loop.
a.
True
b.
False
6. The instructions in a posttest loop will always be executed at least once, but the instructions in a pretest loop may never
be processed.
a.
True
b.
False
7. With the do while statement, the loop body may never be executed.
a.
True
b.
False
8. The condition in the do while statement must evaluate to a Boolean value.
a.
True
b.
False
9. The braces in a do while statement are required, much like in a switch statement.
Name:
Class:
Date:
Page 2
a.
True
b.
False
10. In a clock algorithm, the minute hand is controlled by the outer loop and the hour hand is controlled by the inner loop.
a.
True
b.
False
Indicate the answer choice that best completes the statement or answers the question.
11. In which type of repetition structure is it possible that the instructions in the loop body might never be processed?
a.
for loop
b.
do while loop
c.
posttest loop
d.
switch loop
12. What is the value of the variable innerNum at the end of the execution of the following code?
int outerNum;
int innerNum;
for (outerNum = 1; outerNum < 2; outerNum++)
{
for (innerNum = 1; innerNum < 2; innerNum++)
cout << “innerNum:<< innerNum << endl;
}
a.
1
b.
2
c.
3
d.
4
13. With a do while statement, the looping condition indicates the requirement for _____ the loop body instructions.
a.
reversing
b.
printing
c.
repeating
d.
exiting
14. What is wrong with the following code?
int xNum, yNum;
do
for (xNum = 1; xNum < 10; xNum += 1)
yNum += 2;
while (yNum < 20);
a.
the do loop needs opening and closing braces
b.
the for loop needs opening and closing braces
c.
yNum should be initialized
d.
a for loop cannot be inside a do while loop
15. The outer loop of a nested for loop ends with which of the following characters?
a.
closing brace
b.
semicolon
c.
double slash
d.
period
16. Which for clause is correct?
a.
for (int i = 1; i < 10; i += 1)
b.
for (i < 10; int i = 1; i += 1)
c.
for (int i = 1; i += 1)
d.
for (int i = 1)
Name:
Class:
Date:
Page 3
17. Which of the following is true about nested repetition structures?
a.
both must be pretest loops
b.
both must be posttest loops
c.
each loop can be either pretest or posttest
d.
one loop must be pretest and the other posttest
18. What is the result of the following code?
int xNum = 0;
int yNum = 0;
do
{
xNum = xNum + yNum;
yNum += 1;
cout << “xNum:<< xNum;
} while (xNum > 2);
a.
endless loop
b.
no output, the loop is never executed
c.
xNum: 2 is printed
d.
xNum: 0 is printed
19. Which of the following is a repetition structure that performs a posttest loop?
a.
while
b.
do while
c.
if then
d.
for
20. What is the output of the following code?
int xNum = 0;
int yNum = 0;
do
{
xNum += yNum;
} while (xNum == yNum++);
cout << xNum;
a.
0
b.
1
c.
2
d.
3
21. What, if anything is required to make the following code valid?
int x = 0;
do
x += 15;
while (x < 100);
a.
nothing; the code is valid
b.
add beginning and end braces
c.
remove the parentheses around the condition
d.
initialize x to a non-zero value
22. Which of the following applications would require a nested repetition structure?
a.
egg timer
b.
clock
c.
thermometer
d.
weathervane
23. What type of loop is often used in programs that allow the user to select from a menu?
a.
pretest
b.
posttest
Name:
Class:
Date:
Page 4
c.
for
d.
while
24. What is assigned to the answer variable after the following code is executed?
int answer = 0;
int yNum = 0;
do
{
for (int xNum = 1; xNum < 3; xNum +=1)
answer = answer * xNum;
yNum += 1;
} while (yNum < 3);
a.
0
b.
6
c.
18
d.
36
25. How many times will the instruction in the loop body of the following code be processed?
int x = 0;
do
{
x += 3;
} while (x <= 3);
a.
0
b.
1
c.
2
d.
infinite
26. What is the result of the following code?
int xNum, yNum, zNum;
yNum = 0;
zNum = 0;
do
{
for (xNum = 1; xNum < yNum; yNum += 1)
zNum += xNum;
} while (zNum < 100);
cout << “zNum:<< zNum;
a.
endless loop
b.
zNum: 100
c.
zNum: 110
d.
syntax error
27. What is the output of the following code?
int xNum = 0;
int yNum = 0;
do
{
for(yNum = 0; yNum < 3; yNum += 1)
xNum += yNum;
} while (xNum < 10);
cout << xNum;
a.
10
b.
11
Name:
Class:
Date:
Page 5
c.
12
d.
13
28. What type of loop statement should you use if the instructions in the loop body must be processed at least once?
a.
for
b.
do while
c.
while
d.
repeat until
29. How many times will the instruction in the loop body of the following code be processed?
int x = 3;
do
{
x++;
} while (x <= 3);
a.
0
b.
1
c.
2
d.
infinite
30. What is wrong, if anything, with the following code?
double price = 0;
cout << “Item price: “;
cin >> price;
do
{
cout << “Total cost: $” << price + price * .05 << endl;
cout << “Item price: “;
} while (price > 0);
a.
syntax error
b.
missing the update read
c.
a priming read is required
d.
nothing is wrong
31. What is required to fix the following invalid code?
do
{
x++;
} while (x<y)
a.
add a semicolon after the condition
b.
remove the braces
c.
put the condition after the do clause
d.
add //begin loop after the do clause
32. What type of value is the outcome of the condition in a do while statement?
a.
true
b.
false
c.
Boolean
d.
character
33. What type of structure is created when a pretest loop is inside another pretest loop?
a.
integrated selection
b.
nested repetition
c.
iterative repetition
d.
optional selection
34. What, if anything, is wrong with the following code?
int final = 1;
int varA = 0;
Name:
Class:
Date:
Page 6
int varB;
do while (varA < 3)
{
for (varB = 1; varB < 3; varB +=1)
final = final * varB;
varA += 1;
}
a.
the for loop needs opening and closing braces
b.
nothing is wrong
c.
a semicolon is required after the while loop
d.
the while condition is in the wrong place
35. A posttest loop is also referred to as which of the following?
a.
top-driven loop
b.
bottom-driven loop
c.
starting loop
d.
endless loop
36. The do while statement ends with what character?
a.
comment
b.
semicolon
c.
colon
d.
ending brace
37. The code below prints 12 asterisks on the screen, one per line. Modify the code to print 2 lines containing 6 asterisks
each.
for (int outer = 1; outer <= 4; outer = outer + 1)
for (int inner = 1; inner <= 3; inner = inner + 1)
cout << “*” << endl;
38. Using a C++ do while statement, print five asterisks on the screen, one at a time, on the same line. Use the count
variable below as your counter variable. Advance the cursor to the next line when finished.
int count = 1;
39. Using a C++ for statement, print five asterisks on the screen, one at a time, on the same line. Use the count variable
below as your counter variable. Advance the cursor to the next line when finished.
40. The code below prints 15 asterisks on the screen, one per line. Modify the code to print 3 lines containing 5 asterisks
each.
for (int outer = 1; outer <= 5; outer = outer + 1)
for (int inner = 1; inner <= 3; inner = inner + 1)
cout << “*” << endl;
41. Write the C++ code using nested for loops that prints the product of the outer loop counter and the inner loop counter;
one per line. The outer loop counter should go from 1 to 10 and the inner loop counter should go from the value of the
outer loop counter to 10. Both counters should increment by 1. Use outer for the outer loop counter and inner for the inner
loop counter.
42. Why are posttest loops good for displaying a menu of items to the user?
Name:
Class:
Date:
Page 7
43. Given the code below, write the C++ do while statement to print the numbers 1 to 10 out one per line on the
screen. Use the count variable as your counter variable.
int count = 1;
44. Write the C++ code using nested do while loops that accepts a price for an item from a user and then prints out the
final cost of the item using a tax rate from .05 to .10 (5% to 10%). Stop processing when the price entered by the user is
not greater than 0.
45. Given the code below, write the do while loop to continue to enter values for the variable age, and print them out, as
long as the value entered is greater than zero.
int age = 0;
Name:
Class:
Date:
Page 8
Name:
Class:
Date:
Page 9
Name:
Class:
Date:
Page 10