charpter5 Flashcards

1
Q

5.1 What will the following program segments display?
A) x = 2;
y = x++;
cout

A
A) 32
B) 33
C) 23
D) 34
E) It is true!
F) It is true!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

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
3
Q

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

A
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
4
Q

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

A
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
5
Q
5.5 What will the following program segments display?
A) int count = 10;
do
{
cout
A

A) Hello World
B) 10
C) 8 4

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

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

A

Initialization, test, and update.

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

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

A) int count = 1;

B) count

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

5.8 What will the following program segments display?

A) for (int count = 0; count

A
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
9
Q

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

A

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
10
Q

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

A

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
11
Q

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

A

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
12
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
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
13
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
14
Q

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

A

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
15
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
16
Q

5.16 What is an output le? What is an input le?

A

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

17
Q

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

A
  1. Open the file
  2. Process the file
  3. Close the file
18
Q

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

A

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.

19
Q

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

A

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.

20
Q

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

A

ofstream

21
Q

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

A

ifstream

22
Q

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

A
ofstream outputFile("num.txt");
for (int num = 1; num << num << endl;
outputFile.close();
23
Q

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

A
ifstream inputFile("num.txt");
int num;
while (inputFile >> num)
cout << num << endl;
inputFile.close();
24
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

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