C++ - Ch 5 Quiz

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What will the following code display? int x = 0; for (int count = 0; count < 3; count++){ x += count; } cout << x << endl;

3

What will the following code display? int number = 42; cout << number++ << endl;

42

What will the following code display? int x = 0, count = 0; for (count = 0; count < 5 ; count++); { x += count; } cout << x << endl;

5

What will the following code display? int number = 40; number++; cout << ++number << endl;

42

What will the following code display? int x = 0; for (int count = 0; count <= 5; count++){ x += count; } cout << x << endl;

15

How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl;

20

How many times will the following loop display "Hello"? for (int i = 20; i > 0; i--) cout << "Hello!" << endl;

20

The body of a do / while loop will be executed zero or more times. True False

False

The body of a while loop will be executed at least once. True False

False

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

True

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

c. update

Which of these are pretest loops? Choose all that apply. a. do / while b. if / else c. while d. for

c. while d. for

Which of these are posttest loops? a. for b. if / else c. while d. do / while

d. do / while

How many times will the following loop display "Hello"? for (int i = 0; i <= 20; i++) cout << "Hello!" << endl;

21

What will the following code display? int x = 0; for (int count = 1; count <= 10; count+=2){ x += count; } cout << x << endl;

25

What will the following code display? int number = 42; cout << ++number << endl;

43

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

False

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

False

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

True

The body of a for loop will be executed zero or more times. True False

True

The body of a while loop will be executed zero or more times. True False

True

The scope of a variable declared in a for loop's initialization expression is limited to the body of the loop. True False

True

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

True

What is the output of the following code segment? n = 1; while (n < 5){ cout << n << ' '; n++; } a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

a. 1 2 3 4

A loop that has no way of stopping is a / an a. infinite loop b. post-test loop c. pre-test loop d. mid-test loop

a. infinite loop

while ( true ){ . . . } is one way to deliberately write a / an a. infinite loop b. pre-test loop c. mid-test loop d. post-test loop

a. infinite loop

When a continue statement is encountered in a while loop, control is transferred to a. the loop condition b. the statement following the end of the while loop c. the next statement in the loop body d. continue does not have any effect on while loops

a. the loop condition

When a continue statement is encountered in a for loop, control is transferred to a. the loop update block b. the statement following the end of the for loop c. the next statement in the loop body d. continue does not have any effect on for loops

a. the loop update block

What is the output of the following code segment? n = 1; while (n <= 5){ cout << n << ' '; n++; } a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

b. 1 2 3 4 5

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

c. opened

A loop in which the test expression is evaluated after each repetition is a / an a. infinite loop b. pre-test loop c. post-test loop d. mid-test loop

c. post-test loop

The for loop is an example of a / an a. mid-test loop b. post-test loop c. pre-test loop d. infinite loop

c. pre-test loop

What is the output of the following code segment? n = 1; while (n != 5) cout << n << ' '; n++; a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

d. 1 1 1 1 1 1 1 1 1 ...

What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' '; n++; a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

d. 1 1 1 1 1 1 1 1 1 ...

When a continue statement is encountered in an if statement, control is transferred to a. the true clause of the if statement b. the false (or else) clause of the if statement c. the statement that follows the close of the if statement d. continue does not have any effect on if statements

d. continue does not have any effect on if statements

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

c. a nested loop

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

False

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

True

A for loop's body can contain multiple statements, as long as they are enclosed in braces. True False

True

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

True

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

True

The do / while loop is considered to be a / an a. post-test loop b. post-test loop c. pre-test loop d. infinite loop e. mid-test loop

a. post-test loop

The while loop is an example of a / an a. pre-test loop b. infinite loop c. post-test loop d. mid-test loop

a. pre-test loop

When a continue statement is encountered in a do / while loop, control is transferred to a. the loop condition b. the statement following the end of the do / while loop c. the next statement in the loop body d. continue does not have any effect on do / while loops

a. the loop condition

What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; } a. The loop will display numbers starting at 0, for infinity. b. 0 1 2 3 4 c. 01 2 3 4 d. 0 1 2 3 4 5

b. 0 1 2 3 4

A loop in which the body is executed zero or more times is a / an a. post-test loop b. pre-test loop c. mid-test loop d. infinite loop

b. pre-test loop

When a break statement is encountered in a do / while loop, control is transferred to a. the loop condition b. the statement following the end of the do / while loop c. the next statement in the loop body d. break does not have any effect on do / while loops

b. the statement following the end of the do / while loop

When a break statement is encountered in a for loop, control is transferred to a. the for loop update block b. the statement following the end of the for loop c. the next statement in the loop body d. break does not have any effect on for loops

b. the statement following the end of the for loop

When a break statement is encountered in a while loop, control is transferred to a. the loop condition b. the statement following the end of the while loop c. the next statement in the loop body d. break does not have any effect on while loops

b. the statement following the end of the while loop


Set pelajaran terkait

Micro Exam 2: Streptococcus and Enterococcus

View Set

Taiwan Driving Exam Terms Multiple Choice

View Set