Chapter 02 Flashcards
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 crazy mixed up program #include using namespace std; int main() { cout
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
2.5 Variables: little and big . Constants: 2, 2000, “The little number is ”, “The big number is ”
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.
2.4 // Today's Date: September 3, 2012 #include using namespace std; int main() { cout
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
2.3 The works of Wolfgang include the following The Turkish March and Symphony No. 40 in G minor.
2.6 What will the following program display on the screen? #include using namespace std; int main() { int number; number = 712; cout
2.6 The value is number
2.7 Which of the following are illegal variable names, and why? x 99bottles july97 theSalesFigureForFiscalYear98 r&d grade_report
2.7
99bottles
: Variable names cannot begin with a number.
r&d
: Variable names may only use alphabetic letters, digits, or underscores
2.8 Is the variable name Sales the same as sales? Why or why not?
2.8 No. Variable names are case sensitive.
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
2.9 A) short, or unsigned short . B) int C) They both use the same amount of memory.
2.10 On any computer, which data type uses more memory, an integer or an unsigned
integer?
2.10 They both use the same amount of memory.
2.11 What are the ASCII codes for the following characters? (Refer to Appendix B)
C
F
W
2.11 67, 70, 87
2.12 Which of the following is a character literal?
‘B’
“B”
2.12 ‘B’
2.13 Assuming the char data type uses 1 byte of memory, how many bytes do the following literals use? 'Q' "Q" "Sales" '\n'
2.13 ‘Q’ uses one byte
“Q” uses two bytes
“Sales” uses six bytes
‘\n’ uses one byte
- 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.
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; }
2.15 What is wrong with the following program statement?
char letter = “Z”;
2.15 The string constant “Z” is being stored in the character variable
letter
2.16 What header file must you include in order to use string objects?
2.16 The
string
header file
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.
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; }
2.18 Yes or No: Is there an unsigned floating point data type? If so, what is it?
no
- 19 How would the following number in scienti c notation be represented in E notation?
- 31 * 10^17
2.19 6.31E17
2.21 Is the following assignment statement valid or invalid? If it is invalid, why?
72 = amount;
2.21 Invalid. The value on the left of the = operator must be an lvalue.
2.22 How would you consolidate the following definitions into one statement?
int x = 7;
int y = 16;
int z = 28;
2.22
int x = 7, y = 16, z = 28;
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 << number << endl; return 0; }
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 << number << endl; return 0; }