COSC 1320 C++ PROGRAMMING Homework 3

Part I True/False

1. To decrement a number means to increase its value by one.

a. True b. False

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

a. True b. False

3. The block of code that follows a while condition can contain an unlimited number of statements, provided they are enclosed in braces.

a. True b. False

4. A while loop requires a semicolon after the test expression and before the body of the loop.

a. True b. False

5. A while loop is somewhat limited, because the counter can only count up, not down.

a. True b. False

6. A while loop requires a semicolon after the test expression and before the body of the loop.

a. True b. False

7. A while loop is somewhat limited, because the counter can only count up, not down.

a. True b. False

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

a. True b. False

9. When a loop is nested inside another loop, the inner loop goes through all its iterations for each iteration of the outer loop.

a. True b. False 10. A while loop is somewhat limited because the counter can only be increased by one each time through the loop.

a. True b. False

11. When a loop is nested inside another loop, the outer loop goes through all its iterations for each iteration of the inner loop.

a. True b. False

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

a. True b. False

13. The increment and decrement operators can be used in relational expressions as well as in mathematical expressions and in cout statements.

a. True b. False

14. The continue statement lets you leave a loop and continue with the statement following it.

a. True b. False

15. A do-while loop requires a semicolon at the end of the loop after the test expression.

a. True b. False

Part II Multiple Choice

16. _____ and _____ 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. None of these

17. The ++ operator

a. is a unary operator b. adds one to the value of its operand c. can operate in prefix or postfix mode d. All of the above e. b and c, but not a 18. What will the following code print?

num = 8; cout << --num << " "; cout << num++;

a. 7 7 b. 7 8 c. 8 8 d. 8 7 e. None of these

19. What will the following code print?

num = 5; cout << num++ << " "; cout << num--;

a. 5 5 b. 5 6 c. 6 6 d. 6 5 e. None of these

20. The while loop has two important parts: an 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. statement or block that is repeated only if the expression is false c. single line of code that is repeated once, if the expression is true d. statement or block that is repeated once, if the expression is true e. None of these

21. The while loop is a(n) ______loop.

a. pretest b. posttest c. infinite d. limited e. None of these

22. The statements in the body of a while loop might never be executed, whereas the statements in the body of a do-while loop are always executed:

a. at least once b. at least twice c. exactly once d. forever until the user hits the break key e. None of these

23. A sentinel is a special value that

a. is used for data validation b. must be Boolean c. marks the end of a list of values d. must be a negative number e. None of the above 24. For data validation, it is best to use

a. an if statement b. a while loop c. a for loop d. a nested loop e. a switch statement

25. The statements in the body of a do-while loop will always be executed

a. at least once b. at least twice c. regardless of the value of the test expression d. forever until the user hits the break key e. None of these