ch5 Flashcards

1
Q
ch5 checkpoint
5.1 What will the following program segments display?
A) x = 2;
y = x++;
cout << x << y;
A

A) 32

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

B) x = 2;
y = ++x;
Cout&laquo_space;x &laquo_space;y;

A

33

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

C) x = 2;
y = 4;
cout&laquo_space;x++«–y;

A

C) 23

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

D) x = 2;
y = 2 * x++;
cout &laquo_space;x &laquo_space;y;

A

D) 34

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
E) x = 99;
if (x++ < 100)
cout "It is true!\n";
else
cout << "It is false!\n";
A

E) It is true!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
F) x = 0;
if (++x)
cout << "It is true!\n";
else
cout << "It is false!\n";
A

F) It is true!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

5.2 Write an input validation loop that asks the user to enter a number in the range of
10 through 25.

A

5.2 int number;
cout &laquo_space;“Enter a number in the range 10 through 25: “;
cin&raquo_space; number;
while (number < 10 || number > 25)
{
cout &laquo_space;“Error! The number must be in the range “
&laquo_space;“of 10 through 25. Enter a valid number: “;
cin&raquo_space; number;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

5.3 Write an input validation loop that asks the user to enter ‘Y’ , ‘y’ , ‘N’ , or ‘n’ .

A
5.3 char letter;
cout << "Enter Y for yes or N for no: ";
cin >> letter;
while (letter != 'Y' && letter != 'y' &&
letter != 'N' && letter != 'n')
{
cout << "Error! Enter either Y or N: ";
cin >> letter;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

5.4 Write an input validation loop that asks the user to enter “Yes” or “No” .

A
5.4 string input;
cout << "Enter Yes or No: ";
cin >> input;
while ( (input != "Yes") && (input != "No") )
{
cout << "Error! Enter either Yes or No: ";
cin >> input;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
5.5 What will the following program segments display?
A) int count = 10;
do
{
cout << "Hello World\n";
count++;
} while (count < 1);
A

5.5 A) Hello World

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

B) int v = 10;
do
cout &laquo_space;v &laquo_space;end1;
while (v < 5);

A

B) 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
C) int count = 0, number = 0, limit = 4;
do
{
number += 2;
count++;
} while (count < limit);
cout << number << " " << count << endl;
A

C)

8 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

5.6 Name the three expressions that appear inside the parentheses in the for loop’s
header.

A

5.6 Initialization, test, and update.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

5.7 You want to write a for loop that displays I love to program 50 times. Assume
that you will use a counter variable named count.
A) What initialization expression will you use?
B) What test expression will you use?
C) What update expression will you use?
D) Write the loop.

A

5.7
A) int count = 1;
B) count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

5.8 What will the following program segments display?

A) for (int count = 0; count

A
5.8 A) 0246810
B) -5-4-3-2-101234
C) 5
8
11
14
17
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

5.9 Write a for loop that displays your name 10 times.

A

5.9 for (int count = 1; count &laquo_space;“Chris Coder\n”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

5.10 Write a for loop that displays all of the odd numbers, 1 through 49.

A

5.10 for (int count = 1; count &laquo_space;count &laquo_space;endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

5.11 Write a for loop that displays every fifth number, zero through 100.

A

5.11 for (int count = 0; count &laquo_space;count &laquo_space;endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

5.12 Write a for loop that repeats seven times, asking the user to enter a number. The
loop should also calculate the sum of the numbers entered.

A
5.12 int number, total = 0;
for (int count = 0; count < 7; count++)
{
cout << "Enter a number: ";
cin >> number;
total += number;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
5.13 In the following program segment, which variable is the counter variable and
which is the accumulator?
int a, x, y = 0;
for (x = 0; x > a;
y += a;
}
cout
A

5.13 The variable x is the counter variable. The variable y is the accumulator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

5.14 Why should you be careful when choosing a sentinel value?

A

5.14 You must be sure to choose a sentinel value that could not be mistaken as a member of the
data list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

5.15 How would you modify Program 5-13 so any negative value is a sentinel?

A

5.15 Change the while loop to read:

while (points >= 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

5.16 What is an output file? What is an input file?

A

5.16 An output file is a file that data is written to. An input file is a file that data is read from.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

5.17 What three steps must be taken when a file is used by a program?

A
  1. 17 1. Open the file
  2. Process the file
  3. Close the file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

5.18 What is the difference between a text file and a binary file?

A

5.18 A text file contains data that has been encoded as text, using a scheme such ASCII or
Unicode. Even if the file contains numbers, those numbers are stored in the file as a
series of characters. As a result, the file may be opened and viewed in a text editor
such as Notepad. A binary file contains data that has not been converted to text. As a
consequence, you cannot view the contents of a binary file with a text editor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

5.19 What is the difference between sequential access and random access?

A

5.19 When you work with a sequential access file, you access data from the beginning of the file
to the end of the file. When you work with a random access file, you can jump directly to
any piece of data in the file without reading the data that comes before it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

5.20 What type of file stream object do you create if you want to write data to a file?

A

5.20 ofstream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

5.21 What type of file stream object do you create if you want to read data from a file?

A

5.21 ifstream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

5.22 Write a short program that uses a for loop to write the numbers 1 through 10 to
a file.

A

5.22 ofstream outputFile(“num.txt”);
for (int num = 1; num &laquo_space;num &laquo_space;endl;
outputFile.close();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

5.23 Write a short program that opens the file created by the program you wrote for
Checkpoint 5.22, reads all of the numbers from the file, and displays them.

A
5.23 ifstream inputFile("num.txt");
int num;
while (inputFile >> num)
cout << num << endl;
inputFile.close();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q
5.24 The following code has an error. Can you correct it?
// Find the error and correct it.
ofstream outputFile;
string filename = "Test.txt";
outputFile.open(filename);
A

5.24 ofstream outputFile;
string filename = “Test.txt”;
outputFile.open(filename.c_str());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Short Answer

1. Why should you indent the statements in the body of a loop?

A
  1. By indenting the statements, you make them stand out from the surrounding code. This
    helps you to identify at a glance the statements that are conditionally executed by a loop.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
  1. Describe the difference between pretest loops and posttest loops.
A
  1. The main difference bt pretest loops and posttest loops is one in which the block is to be repeated until the specified condition is no longer true, and the condition is tested before the block is executed and a posttest loop is one in which the block is to be repeated until the specified condition is no longer true, and the condition is tested after the block is executed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q
  1. Why are the statements in the body of a loop called conditionally executed
    statements?
A
  1. Because they are only executed when a condition is true.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q
  1. What is the difference between the while loop and the do-while loop?
A
  1. Difference bt the while loop and the do-while loop is in while loop condition is checked at the starting of the loop, if it is true code is the loop is executed and this process is repeated till the condition becomes false and in the do-while loop the checking of condition is done at the end of the loop. so even if the condition is false, inside the loop is executed at least once
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q
  1. Which loop should you use in situations where you wish the loop to repeat until the
    test expression is false, and the loop should not execute if the test expression is false to
    begin with?
A
  1. The while loop.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q
  1. Which loop should you use in situations where you wish the loop to repeat until the
    test expression is false, but the loop should execute at least one time?
A
  1. the do-while loop is to repeat until the test expression is false, but the loop should execute at least one time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q
  1. Which loop should you use when you know the number of required iterations?
A
  1. The for loop.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q
  1. Why is it critical that counter variables be properly initialized?
A
  1. two reasons;
  2. counter variables are used to count the number of times the loop needs to be iterated.
  3. they are used as loop control variables
40
Q
  1. Why is it critical that accumulator variables be properly initialized?
A
  1. An accumulator is used to keep a running total of numbers. In a loop, a value is usually
    added to the current value of the accumulator. If it is not properly initialized, it will not contain
    the correct total.
41
Q
  1. Why should you be careful not to place a statement in the body of a for loop that
    changes the value of the loop s counter variable?
A
  1. placing the counter variable in body of loop that changes value of statement will lead to un-determined termination ie. loop will probably not terminate when your expect
42
Q
  1. What header file do you need to include in a program that performs file operations?
A
  1. fstream
43
Q
  1. What data type do you use when you want to create a file stream object that can write
    data to a file?
A

ofstream

44
Q
  1. What data type do you use when you want to create a file stream object that can read
    data from a file?
A
  1. ifstream
45
Q
  1. Why should a program close a file when it s finished using it?
A

an unclosed file causes any unsaved data that still held in a buffer to be saved to its file. so when it is closed after manipulation, the data in the file will not be lost even if you need to read it later in the same program

46
Q
  1. What is a file ‘s read position? Where is the read position when a file is first opened for
    reading?
A
  1. A file’s read position marks the location of the next byte that will be read from the file. When
    an input file is opened, its read position is initially set to the first byte in the file.
47
Q
  1. To __________ a value means to increase it by one, and to __________ a value means
    to decrease it by one.
A

increment/ decrement

48
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
49
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

18.postfix

50
Q
  1. The statement or block that is repeated is known as the __________ of the loop.
A
  1. body
51
Q
  1. Each repetition of a loop is known as a(n) __________.
A
  1. iteration
52
Q
  1. A loop that evaluates its test expression before each repetition is a(n) __________ loop.
A
  1. pretest
53
Q
  1. A loop that evaluates its test expression after each repetition is a(n) __________ loop.
A
  1. posttest
54
Q
  1. A loop that does not have a way of stopping is a(n) __________ loop.
A
  1. infinite or endless
55
Q
  1. A(n) __________ is a variable that counts the number of times a loop repeats.
A
  1. counter
56
Q
  1. A(n) __________ is a sum of numbers that accumulates with each iteration of a loop.
A
  1. running total
57
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
58
Q
  1. A(n) __________ is a special value that marks the end of a series of values.
A
  1. sentinel
59
Q
  1. The __________ loop always iterates at least once.
A
  1. do-while
60
Q
  1. The __________ and __________ loops will not iterate at all if their test expressions
    are false to start with.
A
  1. while and for
61
Q
  1. The __________ loop is ideal for situations that require a counter.
A

for

62
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
63
Q
  1. A loop that is inside another is called a(n) __________ loop.
A
  1. nested
64
Q
  1. The __________ statement causes a loop to terminate immediately.
A
  1. break
65
Q
  1. The __________ statement causes a loop to skip the remaining statements in the
    current iteration.
A
  1. continue
66
Q
  1. Write a while loop that lets the user enter a number. The number should be multiplied
    by 10, and the result stored in the variable product. The loop should iterate as
    long as product contains a value less than 100.
A
  1. int product = 0, num;
    while (product > num;
    product = num * 10;
    }
67
Q
  1. Write a do-while loop that asks the user to enter two numbers. The numbers should
    be added and the sum displayed. The user should be asked if he or she wishes to perform
    the operation again. If so, the loop should repeat; otherwise it should terminate.
A
char repeat;
do
{
double num1,num2;
cout > num1 >> num2;
cout > repeat;
} while (repear == 'Y' || repeat == 'y');
68
Q
  1. Write a for loop that displays the following set of numbers:
    0, 10, 20, 30, 40, 50 . . . 1000
A
  1. for (int x = 0; x &laquo_space;x;
69
Q
  1. Write a loop that asks the user to enter a number. The loop should iterate 10 times
    and keep a running total of the numbers entered.
A
int number, total = 0;
for (int i=0; i < 10; i++)
{
cout << "Enter a number: ";
cin >> number;
total +=number;
}
70
Q
  1. Write a nested loop that displays 10 rows of # characters. There should be 15 #
    characters in each row.
A
39. for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 15; col++)
cout << '#';
cout << endl;
}
71
Q
40. Convert the following while loop to a do-while loop:
int x = 1;
while (x > 0)
{
cout << "enter a number: ";
cin >> x;
}
A
int x;
do
{
cout << "enter a number: ";
cin >> x;
} while (x>0);
72
Q
  1. Convert the following do-while loop to a while loop:
    char sure;
    do
    {
    cout &laquo_space;“Are you sure you want to quit? “;
    cin&raquo_space; sure;
    } while (sure != ‘Y’ && sure != ‘N’);
A
41. char sure = 'x';
while (sure != 'Y' && sure != 'N')
{
cout << "Are you sure you want quit? "
cin >> sure;
}
73
Q
42. Convert the following while loop to a for loop:
int count = 0;
while (count < 50)
{
cout << "count is " << count << endl;
count++;
}
A

for (int count = 0; count &laquo_space;“count is &laquo_space;count &laquo_space;endl;

}

74
Q
43. Convert the following for loop to a while loop:
for (int x = 50; x > 0; x--)
{
cout << x << " seconds to go.\n";
}
A
43. int x = 50;
while (x > 0)
{
cout << x << " seconds to go.\n";
x--;
}
75
Q
  1. Write code that does the following: Opens an output file with the filename
    Numbers.txt, uses a loop to write the numbers 1 through 100 to the file, and then
    closes the file.
A

44.

76
Q
  1. Write code that does the following: Opens the Numbers.txt le that was created by
    the code you wrote in question 44, reads all of the numbers from the file and displays
    them, and then closes the file.
A
  1. ifstream inputFile(“Numbers.txt”);
    int number;
    while (inputFile&raquo_space; number)
    cout
77
Q
  1. Modify the code that you wrote in question 45 so it adds all of the numbers read from
    the file and displays their total.
A

46

78
Q
  1. T F The operand of the increment and decrement operators can be any valid
    mathematical expression.
A
  1. false
79
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,
80
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
81
Q
  1. T F The while loop is a pretest loop.
A
  1. true
82
Q
  1. T F The do-while loop is a pretest loop.
A
  1. false
83
Q
  1. T F The for loop is a posttest loop.
A
  1. f, for loop is pretest loop
84
Q
  1. T F It is not necessary to initialize counter variables.
A
  1. false
85
Q
  1. T F All three of the for loop s expressions may be omitted.
A
  1. t
86
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
87
Q
  1. T F Variables may be defined inside the body of a loop.
A
  1. t
88
Q
  1. T F A variable may be defined in the initialization expression of the for loop.
A
  1. true
89
Q
  1. T F In a nested loop, the outer loop executes faster than the inner loop.
A
  1. f. inner loops executed first
90
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
91
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. t.
92
Q
  1. T F The break statement causes a loop to stop the current iteration and begin the
    next one.
A
  1. false
93
Q
  1. T F The continue statement causes a terminated loop to resume.
A
  1. f. the continue statement causes a loop to stop the current iteration and begin the next one
94
Q
  1. T F In a nested loop, the break statement only interrupts the loop it is placed in.
A
  1. true
95
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. t