CS 150 Chapter 5 (final)

8 September 2022
4.7 (114 reviews)
50 test answers

Unlock all answers in this set

Unlock answers (46)
question
False
answer
The statement in the body of a while loop acts as a decision maker.
question
False
answer
The following while loop terminates when j > 20. j = 0; while (j < 20) j++;
question
True
answer
The number of iterations of a counter-controlled loop is known in advance.
question
True
answer
Assume all variables are properly declared. The output of the following C++ code is 2 3 4 5. n = 1; while (n < 5) { n++; cout << n << " "; }
question
True
answer
In a counter-controlled while loop, the loop control variable must be initialized before the loop.
question
True
answer
In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered.
question
False
answer
In a sentinel-controlled while loop, the body of the loop continues to execute until the EOF symbol is read.
question
True
answer
The control variable in a flag-controlled while loop is a bool variable.
question
True
answer
The control statements in the for loop include the initial statement, loop condition, and update statement.
question
False
answer
Assume that all variables are properly declared. The following for loop executes 20 times.
question
cstdlib
answer
To generate a random number, you can use the function rand of the header file ____________________.
question
unsigned
answer
The function srand takes as input a(n) ____________________ int, which acts as the seed for the algorithm.
question
istream
answer
The function eof is a member of the data type ____________________.
question
indexed
answer
A for loop is typically called a counted or ____________________ for loop.
question
semantic
answer
A semicolon at the end of the for statement (just before the body of the loop) is a(n) ____________________ error.
question
True
answer
The for loop body executes indefinitely if the loop condition is always ____________________.
question
nesting or nested
answer
Putting one control structure statement inside another is called ____________________.
question
pretest
answer
In a while and for loop, the loop condition is evaluated before executing the body of the loop. Therefore, while and for loops are called ____________________ loops.
question
posttest
answer
A do...while loop is a(n) ____________________ loop, since the loop condition is evaluated after executing the body of the loop.
question
do...while or do while or do/while or do-while
answer
The ____________________ loop has an exit condition but no entry condition.
question
To exit early from a loop. β€’ To skip the remainder of a switch structure.
answer
The ____________________ statement is typically used for two purposes:
question
continue
answer
If a(n) ____________________ statement is placed in a do...while structure, the loop-continue test is evaluated immediately after this statement.
question
patch
answer
A software ____________________ is a piece of code written on top of an existing piece of code intended to fix a bug in the original code.
question
invariant
answer
A loop ____________________ is a set of statements that remains true each time the loop body is executed.
question
off-by-one or off-by-1 or off by one or off by 1
answer
In a(n) "____________________" problem, either the loop executes one too many or one too few times.
question
looping
answer
In ____ structures, the computer repeats particular statements a certain number of times depending on some condition(s).
question
25 1
answer
What is the output of the following C++ code? count = 1; num = 25; while (count < 25) { num = num - 1; count++; } cout << count << " " << num << endl;
question
10
answer
What is the output of the following C++ code? num = 10; while (num > 10) num = num - 2; cout << num << endl;
question
155
answer
What is the output of the following C++ code? num = 100; while (num <= 150) num = num + 5; cout << num << endl;
question
infinite
answer
A loop that continues to execute endlessly is called a(n) ____ loop.
question
counter-controlled
answer
Consider the following code. int limit; int reps = 0; cin >> limit; while (reps < limit) { cin >> entry; triple = entry * 3; cout << triple; reps++; } cout << endl; This code is an example of a(n) ____ while loop.
question
110
answer
Suppose sum and num are int variables, and the input is 18 25 61 6 -1. What is the output of the following code? sum = 0; cin >> num; while (num != -1) { sum = sum + num; cin >> num; } cout << sum << endl;
question
flag
answer
A(n) ____-controlled while loop uses a bool variable to control the loop.
question
srand(time(0)); num = rand() % 50;
answer
Which of the following statements generates a random number between 0 and 50?
question
EOF-controlled
answer
Consider the following code. (Assume that all variables are properly declared.) cin >> ch; while (cin) { cout << ch; cin >> ch; } This code is an example of a(n) ____ while loop.
question
34
answer
What is the next Fibonacci number in the following sequence? 1, 1, 2, 3, 5, 8, 13, 21, ...
question
i = 1;
answer
What is the initial statement in the following for loop? (Assume that all variables are properly declared.) int i; for (i = 1; i < 20; i++) cout << "Hello World"; cout << "!" << endl;
question
10 11
answer
What is the output of the following C++ code? int j; for (j = 10; j <= 10; j++) cout << j << " "; cout << j << endl;
question
24
answer
Suppose sum, num, and j are int variables, and the input is 4 7 12 9 -1. What is the output of the following code? cin >> sum; cin >> num; for (j = 1; j <= 3; j++) { cin >> num; sum = sum + num; } cout << sum << endl;
question
125
answer
Suppose j, sum, and num are int variables, and the input is 26 34 61 4 -1. What is the output of the code? sum = 0; cin >> num; for (int j = 1; j <= 4; j++) { sum = sum + num; cin >> num; } cout << sum << endl;
question
do...while
answer
Which of the following is a repetition structure in C++?
question
the staement
answer
Which executes first in a do...while loop?
question
40
answer
What is the value of x after the following statements execute? int x = 5; int y = 30; do x = x * 2; while (x < y);
question
This is an infinite loop.
answer
What is the output of the following loop? count = 5; cout << 'St'; do { cout << 'o'; count--; } while (count <= 5);
question
do...while
answer
____ loops are called posttest loops.
question
do...while loop
answer
Which of the following loops is guaranteed to execute at least once?
question
do...while loop
answer
Which of the following loops does not have an entry condition?
question
break
answer
The ____ statement can be used to eliminate the use of certain (flag) variables.
question
loop-continue test
answer
What executes immediately after a continue statement in a while and do-while loop?
question
for loop
answer
When a continue statement is executed in a ____, the update statement always executes.