Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 05 Starting out with C++ Looping


 

Download  file with the answers

Not a member!
Create a FREE account here to get access and download this file with answers


 

Chapter Five

MULTIPLE CHOICE

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

 

2. 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 1… and on forever
c. 2 3 4 5 6
d. 1 2 3 4
e. 2 3 4 5

 

3. This operator increments the value of its operand, then uses the value in context.
a. prefix increment
b. postfix increment
c. prefix decrement
d. postfix decrement
e. None of these

 

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

 

5. The while loop is this type of loop.
a. post-test
b. pre-test
c. infinite
d. limited
e. None of these

 

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

 

7. The statements in the body of a while loop may never be executed, whereas the statements in the body of a do-while loop will be executed:
a. at least once
b. at least twice
c. as many times as the user wishes
d. never
e. None of these

 

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

 

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

 

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

 

11. The do-while loop is a __________ loop that is ideal in situations where you always want the loop to iterate at least once.
a. post-test
b. pre-test
c. infinite
d. null-terminated
e. None of these

 

12. A loop that is inside another loop is called:
a. an infinite loop
b. a pre-test loop
c. a post-test loop
d. a nested loop
e. None of these

 

13. This statement may be used to stop a loop’s current iteration and begin the next one.
a. break
b. terminate
c. re-iterate
d. continue
e. None of these

 

14. This means to increase a value by one.
a. decrement
b. increment
c. modulus
d. parse
e. None of these

 

15. When the increment operator precedes its operand, as in ++num1, the expression is in this mode.
a. postfix
b. prefix
c. preliminary
d. binary
e. None of these

 

16. Look at the following statement.

while (x++ < 10)

Which operator is used first?
a. ++
b. <
c. Neither. The expression is invalid.

 

17. This is a control structure that causes a statement or group of statements to repeat.
a. decision statement
b. constant
c. loop
d. cout object
e. None of these

 

18. The while loop contains an expression that is tested for a true or false value, and a statement or block that is repeated as long as the expression
a. is false
b. is true
c. does not evaluate to true or false
d. evaluates to true or false
e. None of these

 

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

 

20. This is a variable that is regularly incremented or decremented each time a loop iterates.
a. constant
b. counter
c. control statement
d. null terminator
e. None of these

 

21. This is a special value that marks the end of a list of values.
a. constant
b. variable
c. loop
d. sentinel
e. None of these

 

22. What is the output of the following code segment?

n = 1;
for ( ; n <= 5; )
cout << n << ‘ ‘;
n++;

a. 1 2 3 4 5
b. 1 1 1 … and on forever
c. 2 3 4 5 6
d. 1 2 3 4
e. 2 3 4 5

 

23. The do-while loop is considered a(n) _________ loop.
a. pre-test
b. post-test
c. infinite
d. limited
e. None of these

 

24. This loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop.
a. do-while
b. while
c. for
d. infinite
e. None of these

 

25. This is a pre-test loop that is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning.
a. do-while
b. while
c. for
d. infinite
e. None of these

 

26. This statement causes a loop to terminate early.
a. stop
b. break
c. null
d. terminate
e. None of these

 

27. If you want a user to enter exactly 20 values, which loop would be the best to use?
a. do-while
b. for
c. while
d. infinite
e. None of these

 

28. This statement may be used to stop a loop’s current iteration and begin the next one.
a. break
b. terminate
c. return
d. continue
e. None of these

 

29. What will the following loop display?

int x = 0;
while (x < 5)
{
cout << x << endl;
x++;
}

a. 0
1
2
3
4
5
c. 01 2 3 4
b. 0
1
2
3
4 d. The loop will display numbers starting at 0, for infinity.

 

30. What will the following code display?

int number = 6;
number++;
cout << number << endl;

a. 6 c. 7
b. 5 d. 0

 

31. What will the following code display?

int number = 6;
++number;
cout << number << endl;

a. 6 c. 7
b. 5 d. 0

 

32. What will the following code display?

int number = 6;
cout << number++ << endl;

a. 6 c. 7
b. 5 d. 0

 

33. What will the following code display?

int number = 6;
cout << ++number << endl;

a. 6 c. 7
b. 5 d. 0

 

34. What will the following code display?

int number = 6;
int x = 0;
x = number–;
cout << x << endl;

a. 6 c. 7
b. 5 d. 0

 

35. What will the following code display?

int number = 6
int x = 0;
x = –number;
cout << x << endl; a. 6 c. 7 b. 5 d. 0 ANS: B 36. To allow file access in a program, you must #include this header file. a. file b. fileaccess c. fstream d. cfile ANS: C 37. 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 ANS: C 38. This may be used to write information to a file. a. cout object b. pen object c. output object d. stream insertion operator e. None of these ANS: D 39. To write data to a file, you define an object of this data type. a. outputFile b. ifstream c. fstream d. ofstream ANS: D 40. To read data from a file, you define an object of this data type. a. inputFile b. ifstream c. fstream d. ofstream ANS: B 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;

 

42. Assuming dataFile is a file stream object, the 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

 

43. A file must be ________ before data can be written to or read from it.
a. closed
b. opened
c. buffered
d. initialized
e. None of these

 

44. 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
c. 6
b. 0 d. 3

 

45. How many times will the following loop display “Hello”?

for (int i = 0; i < 20; i++)
cout << “Hello!” << endl;

a. 20 c. 21
b. 19 d. An infinite number of times

 

46. How many times will the following loop display “Hello”?

for (int i = 1; i < 20; i++)
cout << “Hello!” << endl;

a. 20 c. 21
b. 19 d. An infinite number of times

 

47. How many times will the following loop display “Hello”?

for (int i = 0; i <= 20; i++)
cout << “Hello!” << endl; a. 20 c. 21 b. 19 d. An infinite number of times ANS: C 48. How many times will the following loop display “Hello”? for (int i = 20; i > 0; i–)
cout << “Hello!” << endl;

a. 20 c. 21
b. 19 d. An infinite number of times

 

TRUE/FALSE

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

2. True/False: A while loop’s body can contain multiple statements, as long as they are enclosed in braces.

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

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

5. True/False: The scope of a variable declared in a for loop’s initialization expression always extends beyond the body of the loop.

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

7. True/False: You may not use the break and continue statements within the same set of nested loops.

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

9. True/False: The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.

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

11. True/False: You may nest while and do-while loops, but you may not nest for loops.

12. True/False: You may not use the break statement in a nested loop.

13. True/False: An output file is a file that data is written to.

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

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!