C++ Chapter 5

Ace your homework & exams now with Quizwiz!

31. 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 d. This is an infinite loop

#N/A

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

ANS: False

11. You may nest while and do-while loops but you may not nest for loops.

ANS: False

12. You may not use the break statement in a nested loop.

ANS: False

3. A while loop is somewhat limited because the counter can only be incremented by one each time through the loop.

ANS: False

5. The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.

ANS: False

7. You may not use both break and continue statements within the same set of nested loops.

ANS: False

8. The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon.

ANS: False

10. Multiple relational expressions cannot be placed into the test condition of a for loop.

ANS: True

13. An output file is a file that data is written to.

ANS: True

14. It is possible to define a file stream object and open a file in one statement.

ANS: True

15. In C++ 11 you can pass a string object as an argument to a file stream object's open member function.

ANS: True

16. string objects have a member function named c_str that returns the contents of the object formatted as a null-terminated C-string.

ANS: True

2. A while loop's body can contain multiple statements, as long as they are enclosed in braces.

ANS: True

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

ANS: True

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

ANS: True

9. The update expression of a for loop can contain more than one statement, for example: for(i = 5; i <= 10; i++, total+= sales)

ANS: True

33. 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

a. 20

35. 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

a. 20

19. What will the following code display? int number = 6; cout << number++ << endl; a. 6 b. 5 c. 7 d. 0

a. 6

21. 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

a. 6

4. 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

8. 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

12. 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

37. 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

2. 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

23. A for statement contains three expressions: initialization, test, and a. update b. reversal c. null d. validation e. None of these

a. update

29. 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

1. 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

b. ++ and --

15. 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

b. 1 1 ... and on forever

22. 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

b. 5

16. 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

b. <

26. 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

b. a post-test loop

13. 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

b. counter

28. 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

b. for

30. 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

b. for

42. To write read data from a file, you define an object of the __________ data type. a. inputFile b. ifstream c. fstream d. ofstream

b. ifstream

10. This means to increase a value: a. decrement b. increment c. modulus d. parse e. None of these

b. increment

5. 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

b. infinite loop

3. 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

b. loop

6. The while loop is a __________ loop. a. post-test b. pre-test c. infinite d. limited e. None of these

b. pre-test

9. 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

b. prefix

40. 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

c. #include fstream

34. 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

c. 21

17. What will the following code display? int number = 6; number++; cout << number << endl; a. 6 b. 5 c. 7 d. 0

c. 7

18. What will the following code display? int number = 6; ++number; cout << number << endl; a. 6 b. 5 c. 7 d. 0

c. 7

20. What will the following code display? int number = 6; cout << ++number << endl; a. 6 b. 5 c. 7 d. 0

c. 7

38. 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

c. buffer

44. 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

c. closes a file

24. In a for statement, this expression is executed only once: a. the test b. null c. initialization d. validation e. None of these

c. initialization

7. 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

c. null statement

43. 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;

c. outFile << number;

25. 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

c. variable

32. What will the following code display? int x = 0; for (int count = 0; count < 3; count++) x += count; cout << x << endl; a. 0 b. 0 c. 6 d. 3

d. 3

11. 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

d. continue

27. 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

d. do-while

36. 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

d. nested loop

41. To write data to a file, you define an object of the __________ data type. a. outputFile b. ifstream c. fstream d. ofstream

d. ofstream

14. 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

d. sentinel

39. 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

d. stream insertion operator


Related study sets

Self-Determination Theory (EXSC)

View Set

BEHV 5612 - Study Guide: Social Validity (Wolf)

View Set

Immunity Inflammation Infection PrepU

View Set

Evolve: Maternity - Women's Health/Disorders

View Set

Prep U Chapter 34: Assessment and Management of Patients with Inflammatory Rheumatic Disorders

View Set