CHAPTER 5 (16-33, 34-70) REVIEW QUESTIONS Flashcards

For Exam 2

1
Q
  1. To __________ a value means to increase it by one, and to __________ a value means to decrease it by one.
A
  1. increment, decrement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. When the increment or decrement operator is placed before the operand (or to the operand’s left), the operator is being used in __________ mode.
A
  1. prefix
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. When the increment or decrement operator is placed after the operand (or to the operand’s right), the operator is being used in __________ mode.
A
  1. postfix
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. The statement or block that is repeated is known as the __________ of the loop.
A
  1. body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Each repetition of a loop is known as a(n) __________.
A
  1. iteration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. A loop that evaluates its test expression before each repetition is a(n) __________ loop.
A
  1. pretest
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. A loop that evaluates its test expression after each repetition is a(n) __________ loop.
A
  1. posttest
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. A loop that does not have a way of stopping is a(n) __________ loop.
A
  1. infinite or endless
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. A(n) __________ is a variable that “counts” the number of times a loop repeats.
A
  1. counter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. A(n) __________ is a sum of numbers that accumulates with each iteration of a loop.
A
  1. running total
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. A(n) __________ is a variable that is initialized to some starting value, usually zero, and then has numbers added to it in each iteration of a loop.
A
  1. accumulator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. A(n) __________ is a special value that marks the end of a series of values.
A
  1. sentinel
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. The __________ loop always iterates at least once.
A
  1. do-while
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. The __________ and __________ loops will not iterate at all if their test expressions are false to start with.
A
  1. while and for
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. The __________ loop is ideal for situations that require a counter.
A
  1. for
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. Inside the for loop’s parentheses, the first expression is the __________ , the second expression is the __________ , and the third expression is the __________.
A
  1. initialization, test, update
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. A loop that is inside another is called a(n) __________ loop.
A
  1. nested
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. The __________ statement causes a loop to terminate immediately.
A
  1. break
19
Q
  1. The __________ statement causes a loop to skip the remaining statements in the current iteration.
A
  1. continue
20
Q
  1. T F The operand of the increment and decrement operators can be any valid mathematical expression.
A
  1. false
21
Q
  1. T F The cout statement in the following program segment will display 5:
    int x = 5;
    cout &laquo_space;x++;
A
  1. true
22
Q
  1. T F The cout statement in the following program segment will display 5:
    int x = 5;
    cout &laquo_space;++x;
A
  1. false
23
Q
  1. T F The while loop is a pretest loop.
A
  1. true
24
Q
  1. T F The do-while loop is a pretest loop.
A
  1. false
25
Q
  1. T F The for loop is a posttest loop.
A
  1. false
26
Q
  1. T F It is not necessary to initialize counter variables.
A
  1. false
27
Q
  1. T F All three of the for loop’s expressions may be omitted.
A
  1. true
28
Q
  1. T F One limitation of the for loop is that only one variable may be initialized in the initialization expression.
A
  1. false
29
Q
  1. T F Variables may be defined inside the body of a loop.
A
  1. true
30
Q
  1. T F A variable may be defined in the initialization expression of the for loop.
A
  1. true
31
Q
  1. T F In a nested loop, the outer loop executes faster than the inner loop.
A
  1. false
32
Q
  1. T F In a nested loop, the inner loop goes through all of its iterations for every single iteration of the outer loop.
A
  1. true
33
Q
  1. T F To calculate the total number of iterations of a nested loop, add the number of iterations of all the loops.
A
  1. false
34
Q
  1. T F The continue statement causes a terminated loop to resume.
A
  1. false
34
Q
  1. T F The break statement causes a loop to stop the current iteration and begin the next one.
A
  1. false
35
Q
  1. T F In a nested loop, the break statement only interrupts the loop it is placed in.
A
  1. true
36
Q
  1. T F When you call an ofstream object’s open member function, the specified file will be erased if it already exists.
A
  1. true
37
Q

Find the Errors. Each of the following programs has errors. Find as many as you can.

  1. // Find the error in this program.
    #include <iostream>
    using namespace std;
    int main()
    {
    int num1 = 0, num2 = 10, result;
    num1++;
    result = ++(num1 + num2);
    cout << num1 << " " << num2 << " " << result;
    return 0;
    }</iostream>
A
  1. The statement result = ++(num1 + num2); is invalid.
38
Q

Find the Errors. Each of the following programs has errors. Find as many as you can.

  1. // This program adds two numbers entered by the user.
    #include <iostream>
    using namespace std;
    int main()
    { int num1, num2;
    char again;
    while (again == 'y' || again == 'Y')
    cout << "Enter a number: ";
    cin >> num1;
    cout << "Enter another number: ";
    cin >> num2;
    cout << "Their sum is << (num1 + num2) << endl;
    cout << "Do you want to do this again? ";
    cin >> again;
    return 0;
    }</iostream>
A
  1. The while loop tests the variable again before any values are stored in it.

The while loop is missing its opening and closing braces.

39
Q

Find the Errors. Each of the following programs has errors. Find as many as you can.

  1. // This program uses a loop to raise a number to a power.
    #include <iostream>
    using namespace std;
    int main()
    {
    int num, bigNum, power, count;
    cout << "Enter an integer: ";
    cin >> num;
    cout << "What power do you want it raised to? ";
    cin >> power;
    bigNum = num;
    while (count++ < power);
    bigNum *= num;
    cout << "The result is << bigNum << endl;
    return 0;
    }</iostream>
A
  1. The while statement should not end with a semicolon.
    It could also be argued that bigNum should be defined a long.
    count should be initialized to 1.
40
Q

Find the Errors. Each of the following programs has errors. Find as many as you can.

  1. // This program averages a set of numbers.
    #include <iostream>
    using namespace std;
    int main()
    {
    int numCount, total;
    double average;
    cout << "How many numbers do you want to average? ";
    cin >> numCount;
    for (int count = 0; count < numCount; count++)
    {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    total += num;
    count++;
    }
    average = total / numCount;
    cout << "The average is << average << endl;
    return 0;
    }</iostream>
A

The variable total is not initialized to 0.

The statement that calculates the average performs integer division. It should use a type cast to cast either total or numCount to a double.

The variable count is incremented in the for loop's update expression and again within the for loop.
41
Q

Find the Errors. Each of the following programs has errors. Find as many as you can.

  1. // This program displays the sum of two numbers.
    #include <iostream>
    using namespace std;
    int main()
    {
    int choice, num1, num2;
    do
    {
    cout << "Enter a number: ";
    cin >> num1;
    cout << "Enter another number: ";
    cin >> num2;
    cout << "Their sum is " << (num1 + num2) << endl;
    cout << "Do you want to do this again?\n";
    cout << "1 = yes, 0 = no\n";
    cin >> choice;
    } while (choice = 1)
    return 0;
    }</iostream>
A

The expression tested by the do-while loop should be choice == 1 instead of choice = 1.

42
Q

Find the Errors. Each of the following programs has errors. Find as many as you can.

  1. // This program displays the sum of the numbers 1-100.
    #include <iostream>
    using namespace std;
    int main()
    {
    int count = 1, total;
    while (count <= 100)
    total += count;
    cout << "The sum of the numbers 1-100 is ";
    cout << total << endl;
    return 0;
    }</iostream>
A

The variable total is not initialized to 0.
The while loop does not change the value of count, so it iterates an infinite number of times.