Introduction To C Verified Test Bank Ch.2 - Test Bank | C++ Control Structures 9e by Tony Gaddis. DOCX document preview.
Starting Out with C++ from Control Structures to Objects, 9e (Gaddis)
Chapter 2 Introduction to C++
TRUE/FALSE
1. The preprocessor reads a program before it is compiled and only executes those lines beginning with # symbol.
2. Because C++ is case-sensitive, all programs must have a function called main or Main.
3. In programming, the terms "line" and "statement" always mean the same thing.
4. In C++, key words are written in all lowercase letters.
5. The preprocessor executes after the compiler.
6. A value is stored in a variable with an assignment statement.
7. Programming style refers to the way a programmer uses elements such as identifiers, spaces, and blank lines.
8. When typing your source code into the computer, you should be careful since most of your C++ instructions, header files, and variable names are case sensitive.
9. In C++ you are required to name your variables so they indicate the purpose they will be used for.
10. Escape sequences are always stored internally as a single character.
11. Floating point constants are normally stored in memory as doubles.
12. C++ does not have a built-in data type for storing strings of data.
13. A named constant is like a variable, but it its content cannot be changed while the program is running.
14. C++ 11 introduced an alternative way to define variables, using the template key word and an initialization value.
MULTIPLE CHOICE
1. In a C++ program, two slash marks (//) indicate
a. | the end of a statement |
b. | the beginning of a comment |
c. | the end of a program |
d. | the beginning of a block of code |
e. | None of these |
2. A statement that starts with a hashtag (or pound) symbol (#) is called a
a. | comment |
b. | function |
c. | preprocessor directive |
d. | header file |
e. | None of these |
3. For every opening brace ({) in a C++ program, there must be a
a. | string literal |
b. | function |
c. | comment |
d. | closing brace |
e. | None of these |
4. The __________ is(are) used to display information on the computer's screen.
a. | opening and closing braces |
b. | opening and closing quotation marks |
c. | cout object |
d. | backslash |
e. | None of these |
5. In the following statement, the characters Hello! are a(n)
cout << "Hello!";
a. | variable |
b. | string literal |
c. | comment |
d. | object |
e. | None of these |
6. The __________ causes the content of another file to be inserted into a program.
a. | cout object |
b. | double slash (//) |
c. | #include directive |
d. | semicolon (;) |
e. | None of these |
7. Which of the following must be included in any program that uses the cout object?
a. | opening and closing braces |
b. | the header file iostream |
c. | comments |
d. | a namespace |
e. | None of these |
8. Character constants in C++ are always enclosed in
a. | brackets ( < > ) |
b. | braces ( { } ) |
c. | single quotation marks ( ' ' ) |
d. | pound sign and semicolon ( # ; ) |
e. | Any of these |
9. Every complete C++ program must have a
a. | comment |
b. | function named main |
c. | symbolic constant |
d. | cout statement |
e. | None of these |
10. In a cout statement, which of the following will advance the output position to the beginning of the next line?
a. | endl or \n |
b. | end1 or /n |
c. | \n or \t |
d. | \t or \b |
e. | \\ or \' |
11. What will the following code display?
cout << "Monday";
cout << "Tuesday";
cout << "Wednesday";
a. | Monday Tuesday Wednesday |
b. | Monday Tuesday Wednesday |
c. | MondayTuesdayWednesday |
d. | "Monday" "Tuesday" "Wednesday" |
e. | "Monday" "Tuesday" "Wednesday" |
12. What will the following code display?
int number = 23;
cout << "The number is " << "number" << endl;
a. | The number is 23 |
b. | The number is23 |
c. | The number is number |
d. | The number is null |
e. | The number is |
13. What will the following code display?
cout << "Four\n" << "score\n";
cout << "and" << "\nseven";
cout << "\nyears" << " ago" << endl;
a. | Four score and seven years ago |
b. | Four score and seven years ago |
c. | Four score and seven years ago |
d. | Four score and seven years ago |
14. What will the following code display?
cout << "Roses " << "are red";
cout << "and " << "violets/n"
cout << "are" << "blue" << endl;
a. | Roses are red and violets are blue |
b. | Roses are red and violets/nare blue |
c. | Roses are redand violets/nareblue |
d. | Roses are red and violets/n are blue |
15. Which control sequence is used to skip over to the next horizontal tab stop?
a. | \n |
b. | end1 |
c. | \t |
d. | \b |
e. | \' |
16. A(n) __________ represents a storage location in the computer's memory.
a. | literal |
b. | variable |
c. | comment |
d. | integer |
e. | None of these |
17. Data items whose values do not change while the program is running are
a. | literals |
b. | variables |
c. | characters |
d. | integers |
e. | None of these |
18. A variable definition tells the computer
a. | the variable's name and its value |
b. | the variable's data type and its value |
c. | the variable's name and the type of data it will hold |
d. | whether the variable is an integer or a floating-point number |
e. | None of these |
19. You must have a _____________ for every variable you intend to use in a program.
a. | purpose |
b. | variable definition |
c. | memory space |
d. | literal value |
e. | None of these |
20. Which of the following is not a valid C++ identifier?
a. | April2018 |
b. | employee_number |
c. | _1user |
d. | 1user |
e. | theLittleBrownFoxWhoRanAway |
21. What will the following code display?
int x = 23, y = 34, z = 45;
cout << x << y << z << endl;
a. | 23 34 45 |
b. | 23 34 45 |
c. | xyz |
d. | 233445 |
22. The numeric data types in C++ can be broken into two general categories which are
a. | numbers and characters |
b. | singles and doubles |
c. | integers and floating-point numbers |
d. | real and unreal numbers |
e. | numbers and literals |
23. Besides the decimal number system that is most common (base 10), two other number systems that can be used in C++ programs are
a. | octal and fractal |
b. | octal and hexadecimal |
c. | base 2 and base 4 |
d. | base 2 and binary |
e. | None of these |
24. A character literal is __________, whereas a string literal is __________
a. | enclosed in quotation marks, enclosed in brackets |
b. | enclosed in brackets, enclosed in quotation marks |
c. | enclosed in double quotation marks, enclosed in single quotation marks |
d. | enclosed in single quotation marks, enclosed in double quotation marks |
e. | None of these |
25. Which data type typically requires only one byte of storage?
a. | short |
b. | int |
c. | float |
d. | char |
e. | string |
26. In C++11, if you want an integer literal to be treated as a long long int, you can append __________ at the end of the number.
a. | L |
b. | <L L> |
c. | LONG LONG |
d. | LL |
e. | <LONG> |
27. The data type used to declare variables that can hold real numbers is
a. | short |
b. | int |
c. | float |
d. | char |
e. | double |
28. The float data type is considered ___________ precision and the double data type is considered __________ precision.
a. | single, double |
b. | double, single |
c. | floating-point, double |
d. | floating-point, integer |
e. | None of these |
29. Which of the following statements correctly assigns the character M to the variable named letter?
a. | letter = M |
b. | letter = "M"; |
c. | letter = 'M'; |
d. | letter = (M); |
e. | letter = M; |
30. Which of the following lines must be included in a program that has string variables?
a. | #include (string class) |
b. | #include namespace std; |
c. | #include <string> |
d. | string var; |
e. | None of these |
31. Assuming that a program has the following string object definition, which statement correctly assigns the string literal "Jane" to the string object?
string name;
a. | name = Jane; |
b. | name = 'Jane'; |
c. | name = "Jane"; |
d. | name = <Jane>; |
e. | string name = {Jane}; |
32. In memory, C++ automatically places a(n) __________ at the end of string literals which __________.
a. | semicolon, indicates the end of the statement |
b. | \n, indicates an escape sequence |
c. | null terminator, marks the end of the string |
d. | bracket, marks the end of the string |
e. | None of these |
33. Which of the following defines a double-precision floating-point variable named payCheck?
a. | float payCheck; |
b. | double payCheck; |
c. | payCheck double; |
d. | Double payCheck; |
34. The data type of a variable whose value can be either true or false is
a. | int |
b. | binary |
c. | bool |
d. | Boolean |
e. | T/F |
35. What will be the output after the following lines of code execute?
bool choice;
choice = true;
cout << "Your choice is " << choice << endl;
a. | true |
b. | Your choice is true |
c. | Your choice is 1 |
d. | Your choice is choice |
e. | None of these |
36. Using C++11: What data type does the compiler determine for the variable cost in the following statement?
auto cost = 14.95;
a. | int |
b. | double |
c. | bool |
d. | char |
e. | string |
37. A variable's __________ is the part of the program that has access to the variable.
a. | data type |
b. | value |
c. | scope |
d. | assignment |
e. | None of these |
38. What is the value stored in the variable myNum after the following assignment statement executes?
myNum = 23 % 5
a. | 3 |
b. | 4 |
c. | 4.6 |
d. | 115 |
e. | None of these |
39. What is the value of cookies after the following statements execute?
int number = 38, children = 4, cookies;
cookies = number % children;
a. | 2 |
b. | 4 |
c. | 9 |
d. | 9.5 |
e. | .5 |
40. What is the value of number after the following statements execute?
int number;
number = 18 / 4;
a. | 4.5 |
b. | 4 |
c. | 2 |
d. | 0 |
e. | unknown |
41. What is the value of number after the following statements execute?
int number;
number = 18 % 4 + 2;
a. | 3 |
b. | 4 |
c. | 6.5 |
d. | 0 |
e. | unknown |
42. What is output of the following statement?
cout << 4 * (15 / (1 + 3)) << endl;
a. | 15 |
b. | 12 |
c. | 63 |
d. | 72 |
e. | None of these |
43. Which part of the following line is ignored by the compiler?
double userName = "janedoe"; // user's name is janedoe
a. | "janedoe" |
b. | user's name is |
c. | user's name is janedoe |
d. | // |
e. | None of these |
44. A multi-line comment
a. | begins with /* and ends with */ |
b. | can be used to mark as many lines as desired as comments |
c. | allows everything in the selected lines to be ignored |
d. | All of these are true |
45. Which of the following statements correctly defines a named constant named TAX_RATE that holds the value 0.075?
a. | double TAX_RATE = 0.075; |
b. | const TAX_RATE; double TAX_RATE = 0.075; |
c. | const double TAX_RATE = 0.075; |
d. | double TAX_RATE; const TAX_RATE = 0.075; |
e. | const TAX_RATE = 0.075; |
46. Given the following program, which line(s) cause(s) output to be displayed on the screen?
1 // This program displays my gross wages.
2 // I worked 40 hours and I make $20.00 per hour.
3 #include <iostream>
4 using namespace std;
5
6 int main()
7 {
8 int hours;
9 double payRate, grossPay;
10
11 hours = 40;
12 payRate = 20.0;
13 grossPay = hours * payRate;
14 cout << "My gross pay is $" << grossPay << endl;
15 return 0;
16 }
a. | lines 13 and 14 |
b. | lines 8 and 9 |
c. | line 14 |
d. | lines 14 and 15 |
e. | line 15 |
MULTIPLE RESPONSE
1. Select all that apply. Which of the following statements is(are) true about named constants?
a. | A named constant must be all uppercase. |
b. | The content of a named constant is read-only. |
c. | The value of a named constant cannot be changed while the program is running. |
d. | A named constant is defined using the const qualifier. |
e. | None of these |
2. Select all that apply. Using C++11: Which of the following can be used to initialize an integer variable named dozen with the value of 12?
a. | int dozen = 12; |
b. | int dozen(12); |
c. | int dozen = {12}; |
d. | int dozen = (12); |
e. | int dozen {12}; |