Chapter 02 Checkpoints Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q
"2.1 The following C++ program will not compile because the lines have been mixed up.
int main()
}
// A crazy mixed up program
return 0;
#include 
cout
A

“2.1

// A crazy mixed up program
#include 
using namespace std;
int main()
{
cout
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

“2.2 The following C++ program will not compile because the lines have been mixed up.
cout

A

“2.2

// It's a mad, mad program
#include 
using namespace std;
int main()
{
cout
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
"2.3 Study the following program and show what it will print on the screen.
// The Works of Wolfgang
#include 
using namespace std;
int main()
{
cout
A

“2.3

The works of Wolfgang
include the following
The Turkish March
and Symphony No. 40 in G minor.”

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

2.4 On paper, write a program that will display your name on the first line, your street address on the second line, your city, state, and ZIP code on the third line, and your telephone number on the fourth line. Place a comment with today’s date at the top of the program. Test your program by entering, compiling, and running it.

A

“2.4

// Today's Date: September 3, 2012
#include 
using namespace std;
int main()
{
cout
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
"2.5 Examine the following program.
// This program uses variables and literals.
#include 
using namespace std;
int main()
{
int little;
int big;
little = 2;
big = 2000;
cout
A

“2.5

Variables: little and big.
Constants: 2, 2000, “The little number is ”, “The big number is ””

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

“2.6 What will the following program display on the screen?
#include
using namespace std;

int main()
{
int number;
number = 712;
cout
A

2.6 The value is number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
"2.7 Which of the following are illegal variable names, and why?
x
99bottles
july97
theSalesFigureForFiscalYear98
r&d
grade_report"
A

“2.7

99bottles: Variable names cannot begin with a number.
r&d: Variable names may only use alphabetic letters, digits, or underscores”

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

2.8 Is the variable name Sales the same as sales? Why or why not?

A

2.8 No. Variable names are case sensitive.

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

“2.9 Refer to the data types listed in Table 2-6 for these questions.
A) If a variable needs to hold numbers in the range 32 to 6,000, what data type would be best?
B) If a variable needs to hold numbers in the range 40,000 to +40,000, what data type would be best?
C) Which of the following literals uses more memory? 20 or 20L”

A

“2.9

A) short, or unsigned short.
B) int
C) They both use the same amount of memory.”

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

2.10 On any computer, which data type uses more memory, an integer or an unsigned integer?

A

2.10 They both use the same amount of memory.

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

“2.11 What are the ASCII codes for the following characters? (Refer to Appendix B)
C
F
W”

A

“2.11

67, 70, 87”

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

“2.12 Which of the following is a character literal?
‘B’
““B”””

A

“2.12

‘B’”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
"2.13 Assuming the char data type uses 1 byte of memory, how many bytes do the following
literals use?
'Q'
""Q""
""Sales""
'\n'"
A

“2.13

‘Q’ uses one byte
“Q” uses two bytes
“Sales” uses six bytes
‘\n’ uses one byte”

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

2.14 Write a program that has the following character variables: first, middle, and last. Store your initials in these variables and then display them on the screen.

A

“2.14

#include 
using namespace std;
int main()
{
char first, middle, last;
first = 'T';
middle = 'E';
last = 'G';
cout << first << "" "" << middle << "" "" << last << endl;
return 0;
}"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

“2.15 What is wrong with the following program statement?

char letter = ““Z””;”

A

2.15 The string constant “Z” is being stored in the character variable letter.

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

2.16 What header file must you include in order to use string objects?

A

2.16 The string header file

17
Q

2.17 Write a program that stores your name, address, and phone number in three separate string objects. Display the contents of the string objects on the screen.

A

“2.17

#include 
#include 
using namespace std;
int main()
{
string name = ""John Smith"";
string address = ""224 Maple Street\nClyde, NC 28721"";
string phone = ""555-5050"";
cout << name << endl;
cout << address << endl;
cout << phone << endl << endl;
return 0;
}"
18
Q

2.18 Yes or No: Is there an unsigned floating point data type? If so, what is it?

A

2.18 No

19
Q

“2.19 How would the following number in scientific notation be represented in E notation?
6.31 * 10^17”

A

2.19 6.31E17

20
Q

“2.20 Write a program that defines an integer variable named age and a float variable named weight. Store your age and weight, as literals, in the variables. The program should display these values on the screen in a manner similar to the following:

Program Output

My age is 26 and my weight is 180 pounds.

(Feel free to lie to the computer about your age and your weight
it ll never know!)”

A

“2.20

#include 
using namespace std;
int main()
{
int age;
float weight;
age = 26;
weight = 180;
cout
21
Q

“2.21 Is the following assignment statement valid or invalid? If it is invalid, why?
72 = amount;”

A

2.21 Invalid. The value on the left of the = operator must be an lvalue.

22
Q

“2.22 How would you consolidate the following definitions into one statement?
int x = 7;
int y = 16;
int z = 28;”

A

2.22 int x = 7, y = 16, z = 28;

23
Q
"2.23 What is wrong with the following program? How would you correct it?
#include 
using namespace std;
int main()
{
number = 62.7;
double number;
cout
A

“2.23

The variable number is assigned a value before it is defined. Correct the program by moving the statement

number = 62.7;

to the point after the variable declaration. Here is the corrected program:

#include 
using namespace std;
int main()
{
double number;
number = 62.7;
cout
24
Q

“2.24 Is the following an example of integer division or floating-point division? What
value will be stored in portion?
portion = 70 / 3;”

A

2.24 Integer division. The value 23 will be stored in portion.

25
Q

“2.25 Write statements using the const qualifier to create named constants for the following
literal values:

Literal Value Description

2.71828 Euler s number (known in mathematics as e)
5.256E5 Number of minutes in a year
32.2 The gravitational acceleration constant (in feet per second2)
9.8 The gravitational acceleration constant (in meters per second2)
1609 Number of meters in a mile”

A

“2.25

const float E = 2.71828;
const int MINUTES_IN_A_YEAR = 5.256E5;
const float G_FEET = 32.2;
const float G_METERS = 9.8;
const int METERS_IN_A_MILE = 1609"