CS-1143 Chapter 5
What will the following loop display? int x = 0; while (x < 5){ cout << x << endl; x++; }
0 1 2 3 4
How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++); cout << "Hello!" << endl;
1
What is the output of the following code segment? n = 1;while (n != 5) cout << n << ' '; n++;
1 1 1 1 1 1 1 1 1......
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
What is the output of the following code segment? n = 1; while (n <= 5){ cout << n << ' '; n++; }
1 2 3 4 5
How many times will the following loop display "Hello"? for (int i = 1; i < 20; i++) cout << "Hello!" << endl;
19
Which of these are posttest loops?
do / while loop
A loop that has no way of stopping is a / an
infinite loop
A for statement contains three expressions: initialization, test, and:
update
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;
42
What will the following code display? int number = 42; cout << ++number << endl;
43
What will the following code display? int x = 0, count = 0; for (count = 0; count < 5 ; count++); { x += count; } cout << x << endl;
5
The body of a do / while loop will be executed zero or more times.
False
The body of a for loop will be executed at least once.
False
The body of a while loop will be executed at least once.
False
You may nest while and do-while loops, but you may not nest for loops.
False
You may not use the break statement in a nested loop.
False
A do / while loop's body can contain multiple statements, as long as they are enclosed in braces.
True
A for loop's body can contain multiple statements, as long as they are enclosed in braces.
True
A while loop's body can contain multiple statements, as long as they are enclosed in braces.
True
An initialization expression may be omitted from the for loop if no initialization is required.
True
The body of a do / while loop will be executed one or more times.
True
The body of a for loop will be executed zero or more times.
True
The body of a while loop will be executed zero or more times.
True
The scope of a variable declared in a for loop's initialization expression is limited to the body of the loop.
True
When a continue statement is encountered in an if statement, control is transferred to
continue does not have any effect on if statements
while ( true ){ . . . } is one way to deliberately write a / an
infinite loop
A loop in which the body is executed at least once is a / an
post-test loop
A loop in which the test expression is evaluated after each repetition is a / an
post-test loop
The do / while loop is an example of a / an
post-test loop
A loop in which the body is executed zero or more times is a / an
pre-test loop
A loop in which the text expression is evaluated before each repetition is a / an
pre-test loop
The while loop is an example of a / an
pre-test loop
When a continue statement is encountered in a do / while loop, control is transferred to
the loop condition
When a continue statement is encountered in a while loop, control is transferred to
the loop condition
When a continue statement is encountered in a for loop, control is transferred to
the loop update block
Question text When a break statement is encountered in a do / while loop, control is transferred to
the statement following the end of the do / while loop
When a break statement is encountered in a for loop, control is transferred to
the statement following the end of the for loop
When a break statement is encountered in a while loop, control is transferred to
the statement following the end of the while loop.
The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.
true
Which of these are pretest loops? Choose all that apply.
while for