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; }
2.24 Is the following an example of integer division or floating-point division? What
value will be stored in portion?
portion = 70 / 3;
2.24 Integer division. The value 23 will be stored in
portion
2.25 Write statements using the const quali er 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
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
- How many operands does each of the following types of operators require?
_______ Unary
_______ Binary
_______ Ternary
1
2
3
- How may the float variables temp, weight, and age be defined in one statement?
float temp, weight, age;
- How may the int variables months, days, and years be defined in one statement,
with months initialized to 2 and years initialized to 3?
int months=2, days, years=3;
- Write assignment statements that perform the following operations with the variables
a, b, and c.
A) Adds 2 to a and stores the result in b.
B) Multiplies b times 4 and stores the result in a.
C) Divides a by 3.14 and stores the result in b.
D) Subtracts 8 from b and stores the result in a.
E) Stores the value 27 in a.
F) Stores the character K in c.
G) Stores the ASCII code for B in c.
A) b=a + 2; B) a = 4 * b; C) b = a/3.14; D) a = b - 8; E) a= 27; F) c = 'K'; G) char c; c = 'B';
5. Is the following comment written using single-line or multi-line comment symbols? /* This program was written by M. A. Codewriter*/
multi
6. Is the following comment written using single-line or multi-line comment symbols? // This program was written by M. A. Codewriter
sigle
7. Modify the following program so it prints two blank lines between each line of text. #include using namespace std; int main() { cout
7. #include int main() { cout
8. What will the following programs print on the screen? A) #include using namespace std; int main() { int freeze = 32, boil = 212; freeze = 0; boil = 100; cout
0
100
B) #include using namespace std; int main() { int x = 0, y = 2; x = y * 4; cout << x << endl << y << endl; return 0; }
8
2
C) #include using namespace std; int main() { cout << "I am the incredible"; cout << "computing\nmachine"; cout << "\nand I will\namaze\n"; cout << "you."; return 0; }
I am the incredible computing machine and i will amaze you
D) #include using namespace std; int main() { cout << "Be careful\n"; cout << "This might/n be a trick "; cout << "question\n"; return 0; }
Be careful
This might/n be a trick question
E) #include using namespace std; int main() { int a, x = 23; a = x % 2; cout << x << endl << a << endl; return 0; }
23
1
Multiple Choice 9. Every complete statement ends with a A) period B) # symbol C) semicolon D) ending brace
c
10. Which of the following statements is correct? A) #include (iostream) B) #include {iostream} C) #include D) #include [iostream] E) All of the above
c
11. Every C++ program must have a A) cout statement. B) function main. C) #include statement. D) All of the above
b
12. Preprocessor directives begin with a A) # B) ! C) < D) * E) None of the above
a
13. The following data 72 'A' "Hello World" 2.8712 are all examples of A) Variables B) Literals or constants C) Strings D) None of the above
b
14. A group of statements, such as the contents of a function, is enclosed in A) Braces {} B) Parentheses () C) Brackets <> D) All of the above will do
a
15. Which of the following are not valid assignment statements? (Circle all that apply.) A) total = 9; B) 72 = amount; C) profit = 129 D) letter = 'W';
bc
- Which of the following are not valid cout statements? (Circle all that apply.)
A) cout
bcd
17. Assume w = 5, x = 4, y = 8, and z = 2. What value will be stored in result in each of the following statements? A) result = x + y; B) result = z * 2; C) result = y / x; D) result = y - z; E) result = w % 2;
A) 12 B)4 C)2 D)6 E)1
18. How would each of the following numbers be represented in E notation? A) 3.287 * 10^6 B) -978.65 * 10^12 C) 7.65491 * 10^-3 D) -58710.23 * 10^-4
3.287E6
-9.7865E14
765491E-3
-5.871023E0
19. The negation operator is A) Unary B) Binary C) Ternary D) None of the above
A
20. A(n) \_\_\_\_\_\_\_\_\_\_\_ is like a variable, but its value is read-only and cannot be changed during the program s execution. A) secure variable B) uninitialized variable C) named constant D) locked variable
C
- When do preprocessor directives execute?
A) Before the compiler compiles your program
B) After the compiler compiles your program
C) At the same time as the compiler compiles your program
D) None of the above
A
- T F A variable must be defined before it can be used.
T
- T F Variable names may begin with a number.
f
- T F Variable names may be up to 31 characters long.
t
- T F A left brace in a C++ program should always be followed by a right brace
later in the program.
t
- T F You cannot initialize a named constant that is declared with the const
modifier.
f