TEST BANK forStarting Out with C++ from Control Structures to Objects, 9e (Gaddis)

Page 1


Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 1 Introduction to Computers and Programming TRUE/FALSE 1. Software engineering is a field that encompasses designing, writing, testing, debugging, documenting, modifying, and maintaining computer programs. ANS: T 2. Pseudocode is a form of a program statement that will always evaluate to "false." ANS: F 3. In programming, the terms "line" and "statement" always mean the same thing. ANS: F 4. In C++, key words are written in all lowercase letters. ANS: T 5. The preprocessor executes after the compiler. ANS: F 6. Machine language is an example of a high-level language. ANS: F 7. A CPU only understands machine language instructions. ANS: T 8. Programs are often referred to as hardware. ANS: F 9. The CPU is the most important component in a computer because without it, the computer could not run software. ANS: T 10. The term "bit" stands for binary digit. ANS: T


MULTIPLE CHOICE 1. What does the term hardware refer to? a. b. c. d. e.

The relative difficulty of writing computer programs The physical components that make up a computer The way a computer's storage space is organized The logical flow of instructions None of these

ANS: B 2. A(n) __________ is a set of instructions that the computer follows to solve a problem. a. b. c. d. e.

compiler linker program operator None of these

ANS: C 3. Computer programs are also known as a. b. c. d. e.

hardware firmware software Any of these None of these

ANS: C 4. At the heart of a computer is its central processing unit. The CPU's job is: a. b. c. d. e.

To fetch instructions To carry out the operations commanded by the instructions To produce some outcome or resultant information All of these None of these

ANS: D 5. A computer stores a program while it is running a. b. c. d. e.

in main memory on a hard disk on the computer monitor in the CPU None of these

ANS: A 6. The __________ decodes an instruction and generates an electronic signal. a. b. c. d. e.

Arithmetic and Logic Unit Main memory BIOS Control Unit None of these


ANS: D 7. The CPU's control unit retrieves the next instruction in a sequence of program instructions from main memory in the __________ stage. a. b. c. d.

fetch decode execute portability

ANS: A 8. During which stage does the central processing unit analyze the instruction and encode it in the form of a number, and then generate an electronic signal? a. b. c. d.

fetch decode execute portability

ANS: B 9. The two parts of the CPU are a. b. c. d. e.

the output device and the input device the software and the hardware the Control Unit and the Arithmetic and Logic Unit the single-task device and the multi-task device None of these

ANS: C 10. A volatile type of memory that is used for temporary storage is a. b. c. d. e.

an address the ALU RAM a disk drive None of these

ANS: C 11. The purpose of a memory address is: a. b. c. d. e.

to identify the location of a byte in memory to prevent multitasking to obtain an algorithm to improve the speed of processing None of these

ANS: A 12. Programs are normally stored in __________ and loaded into main memory as needed. a. b. c. d.

the input device the output device secondary storage the CPU


e. None of these ANS: C 13. A computer monitor is a type of a. b. c. d. e.

input device output device storage device software None of these

ANS: A 14. Which of the following is not a common input device? a. b. c. d. e.

keyboard mouse digital camera printer All are common input devices

ANS: D 15. Which of the following is not one of the major components of a computer system? a. b. c. d. e.

the preprocessor the CPU main memory input/output devices secondary storage

ANS: A 16. A set of well-defined steps for performing a task or solving a problem is known as a(n): a. b. c. d. e.

hierarchy chart algorithm instruction set statement None of these

ANS: B 17. When a programmer saves to a file the statements he or she writes to create a program, these statements are a. b. c. d. e.

high level source code a preprocessor file object code None of these

ANS: B 18. The programmer usually enters source code into a computer with a. a hierarchy chart b. a text editor


c. a compiler d. pseudocode e. None of these ANS: B 19. In the process of translating a source file into an executable file, which of the following is the correct sequence? a. Source code, preprocessor, modified source code, linker, object code, compiler, executable code b. Preprocessor, source code, compiler, executable code, linker, modified source code, object code c. Source code, compiler, modified source code, preprocessor, object code, linker, executable code. d. Source code, preprocessor, modified source code, compiler, object code, linker, executable code. e. Source code, linker, object code, compiler, modified source code, preprocessor, executable code. ANS: D 20. An Integrated Development Environment (IDE) typically consists of a. b. c. d. e.

a text editor a compiler a debugger All of the above None of these

ANS: D 21. ________ are used to translate each source code instruction into the appropriate machine language instruction. a. b. c. d. e.

modules runtime libraries compilers preprocessor directives None of these

ANS: C 22. This is a set of rules that must be followed when constructing a program: a. b. c. d. e.

syntax punctuation key words operators identifiers

ANS: A 23. Words that have a special meaning and may be used only for their intended purpose are known as a. operators b. programmer defined words c. key words


d. syntax e. None of these ANS: C 24. Which of the following best describes an operator? a. An operator is a rule that must be followed when constructing a program. b. An operator allows you to perform operations on one or more pieces of data. c. An operator marks the beginning or ending of a statement, or is used to separate items in a list. d. An operator is a word that has a special meaning. e. An operator is a symbolic name that refers to a variable. ANS: B 25. This is used in a program to mark the beginning or ending of a statement, or separate items in a list: a. b. c. d. e.

separators punctuation operators key words None of these

ANS: B 26. Characters or symbols that perform operations on one or more operands are: a. b. c. d. e.

separators op codes operators key words None of these

ANS: C 27. This is a complete instruction that causes the computer to perform some action: a. b. c. d. e.

line statement variable key word None of these

ANS: B 28. A named storage location in the computer's memory that holds a piece of information is a(n): a. b. c. d. e.

variable operator key word statement None of these

ANS: A 29. A variable definition defines the name of a variable that will be used in a program, as well as: a. the type of data it will be used to hold


b. c. d. e.

the operators that will be used on it the number of times it will be used in the program the value it will hold None of these

ANS: A 30. Three primary activities of a program are: a. b. c. d. e.

variable definitions, operators, lists of key words lines, statements, punctuation input, processing, output integer, floating-point, character definitions None of these

ANS: C 31. Which step uncovers any syntax errors in your program? a. b. c. d. e.

editing compiling linking executing None of these

ANS: B 32. Mistakes that cause a running program to produce incorrect results are called: a. b. c. d. e.

syntax errors logic errors compiler errors linker errors None of these

ANS: B 33. The programming process consists of several steps, which include: a. b. c. d. e.

key words, operator definitions, punctuation design, creation, testing, debugging input, processing, output syntax, logic, error handling None of these

ANS: B 34. The first step in writing a program is to a. b. c. d. e.

type the code visualize the program running on a computer visualize logical errors clearly define what the program is to do None of these

ANS: D


35. A model often used when creating a program that begins with the overall task and refines it into smaller subtasks is a(n) a. b. c. d. e.

flowchart UML diagram blueprint hierarchy chart None of these

ANS: D 36. The term that refers to the programmer reading the program from the beginning and stepping through each statement is a. b. c. d. e.

pseudocoding software engineering desk checking spot checking None of these

ANS: C 37. The two methods used by C++ to write computer programs are: a. b. c. d. e.

top-down programming and procedural programming procedural programming and object-oriented programming pseudocoding and object-oriented programming flowcharting and procedural programming None of these

ANS: B

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. ANS: T 2. Because C++ is case-sensitive, all programs must have a function called main or Main. ANS: F 3. In programming, the terms "line" and "statement" always mean the same thing. ANS: F 4. In C++, key words are written in all lowercase letters. ANS: T


5. The preprocessor executes after the compiler. ANS: F 6. A value is stored in a variable with an assignment statement. ANS: T 7. Programming style refers to the way a programmer uses elements such as identifiers, spaces, and blank lines. ANS: T 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. ANS: T 9. In C++ you are required to name your variables so they indicate the purpose they will be used for. ANS: F 10. Escape sequences are always stored internally as a single character. ANS: T 11. Floating point constants are normally stored in memory as doubles. ANS: T 12. C++ does not have a built-in data type for storing strings of data. ANS: T 13. A named constant is like a variable, but it its content cannot be changed while the program is running. ANS: T 14. C++ 11 introduced an alternative way to define variables, using the template key word and an initialization value. ANS: F MULTIPLE CHOICE 1. In a C++ program, two slash marks (//) indicate a. the end of a statement


b. c. d. e.

the beginning of a comment the end of a program the beginning of a block of code None of these

ANS: B 2. A statement that starts with a hashtag (or pound) symbol (#) is called a a. b. c. d. e.

comment function preprocessor directive header file None of these

ANS: C 3. For every opening brace ({) in a C++ program, there must be a a. b. c. d. e.

string literal function comment closing brace None of these

ANS: D 4. The __________ is(are) used to display information on the computer's screen. a. b. c. d. e.

opening and closing braces opening and closing quotation marks cout object backslash None of these

ANS: C 5. In the following statement, the characters Hello! are a(n) cout << "Hello!"; a. b. c. d. e.

variable string literal comment object None of these

ANS: B 6. The __________ causes the content of another file to be inserted into a program. a. b. c. d. e.

cout object double slash (//) #include directive semicolon (;) None of these

ANS: C


7. Which of the following must be included in any program that uses the cout object? a. b. c. d. e.

opening and closing braces the header file iostream comments a namespace None of these

ANS: B 8. Character constants in C++ are always enclosed in a. b. c. d. e.

brackets ( < > ) braces ( { } ) single quotation marks ( ' ' ) pound sign and semicolon ( # ; ) Any of these

ANS: C 9. Every complete C++ program must have a a. b. c. d. e.

comment function named main symbolic constant cout statement None of these

ANS: B 10. In a cout statement, which of the following will advance the output position to the beginning of the next line? a. b. c. d. e.

endl or \n end1 or /n \n or \t \t or \b \\ or \'

ANS: A 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"


ANS: C 12. What will the following code display? int number = 23; cout << "The number is " << "number" << endl; a. b. c. d. e.

The number is 23 The number is23 The number is number The number is null The number is

ANS: C 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 ANS: A 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 ANS: C 15. Which control sequence is used to skip over to the next horizontal tab stop? a. \n b. end1 c. \t


d. \b e. \' ANS: C 16. A(n) __________ represents a storage location in the computer's memory. a. b. c. d. e.

literal variable comment integer None of these

ANS: B 17. Data items whose values do not change while the program is running are a. b. c. d. e.

literals variables characters integers None of these

ANS: A 18. A variable definition tells the computer a. b. c. d. e.

the variable's name and its value the variable's data type and its value the variable's name and the type of data it will hold whether the variable is an integer or a floating-point number None of these

ANS: C 19. You must have a _____________ for every variable you intend to use in a program. a. b. c. d. e.

purpose variable definition memory space literal value None of these

ANS: B 20. Which of the following is not a valid C++ identifier? a. b. c. d. e.

April2018 employee_number _1user 1user theLittleBrownFoxWhoRanAway

ANS: D 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 ANS: D 22. The numeric data types in C++ can be broken into two general categories which are a. b. c. d. e.

numbers and characters singles and doubles integers and floating-point numbers real and unreal numbers numbers and literals

ANS: C 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. b. c. d. e.

octal and fractal octal and hexadecimal base 2 and base 4 base 2 and binary None of these

ANS: B 24. A character literal is __________, whereas a string literal is __________ a. b. c. d. e.

enclosed in quotation marks, enclosed in brackets enclosed in brackets, enclosed in quotation marks enclosed in double quotation marks, enclosed in single quotation marks enclosed in single quotation marks, enclosed in double quotation marks None of these

ANS: D 25. Which data type typically requires only one byte of storage? a. b. c. d. e.

short int float char string

ANS: D 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. b. c. d. e.

L <L L> LONG LONG LL <LONG>


ANS: D 27. The data type used to declare variables that can hold real numbers is a. b. c. d. e.

short int float char double

ANS: C 28. The float data type is considered ___________ precision and the double data type is considered __________ precision. a. b. c. d. e.

single, double double, single floating-point, double floating-point, integer None of these

ANS: A 29. Which of the following statements correctly assigns the character M to the variable named letter? a. b. c. d. e.

letter = M letter = "M"; letter = 'M'; letter = (M); letter = M;

ANS: C 30. Which of the following lines must be included in a program that has string variables? a. b. c. d. e.

#include (string class) #include namespace std; #include <string> string var; None of these

ANS: C 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. b. c. d. e.

name = Jane; name = 'Jane'; name = "Jane"; name = <Jane>; string name = {Jane};

ANS: C 32. In memory, C++ automatically places a(n) __________ at the end of string literals which __________.


a. b. c. d. e.

semicolon, indicates the end of the statement \n, indicates an escape sequence null terminator, marks the end of the string bracket, marks the end of the string None of these

ANS: C 33. Which of the following defines a double-precision floating-point variable named payCheck? a. b. c. d.

float payCheck; double payCheck; payCheck double; Double payCheck;

ANS: B 34. The data type of a variable whose value can be either true or false is a. b. c. d. e.

int binary bool Boolean T/F

ANS: C 35. What will be the output after the following lines of code execute? bool choice; choice = true; cout << "Your choice is " << choice << endl; a. b. c. d. e.

true Your choice is true Your choice is 1 Your choice is choice None of these

ANS: C 36. Using C++11: What data type does the compiler determine for the variable cost in the following statement? auto cost = 14.95; a. b. c. d. e.

int double bool char string

ANS: B 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 ANS: C 38. What is the value stored in the variable myNum after the following assignment statement executes? myNum = 23 % 5 a. b. c. d. e.

3 4 4.6 115 None of these

ANS: A 39. What is the value of cookies after the following statements execute? int number = 38, children = 4, cookies; cookies = number % children; a. b. c. d. e.

2 4 9 9.5 .5

ANS: A 40. What is the value of number after the following statements execute? int number; number = 18 / 4; a. b. c. d. e.

4.5 4 2 0 unknown

ANS: B 41. What is the value of number after the following statements execute? int number; number = 18 % 4 + 2; a. b. c. d. e.

3 4 6.5 0 unknown

ANS: B 42. What is output of the following statement? cout << 4 * (15 / (1 + 3)) << endl;


a. b. c. d. e.

15 12 63 72 None of these

ANS: B 43. Which part of the following line is ignored by the compiler? double userName = "janedoe"; a. b. c. d. e.

// user's name is janedoe

"janedoe" user's name is user's name is janedoe // None of these

ANS: C 44. A multi-line comment a. b. c. d.

begins with /* and ends with */ can be used to mark as many lines as desired as comments allows everything in the selected lines to be ignored All of these are true

ANS: D 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; ANS: C 46. Given the following program, which line(s) cause(s) output to be displayed on the screen? 1 2 3 4 5 6 7 8 9 10 11 12 13 14

// This program displays my gross wages. // I worked 40 hours and I make $20.00 per hour. #include <iostream> using namespace std; int main() { int hours; double payRate, grossPay; hours = 40; payRate = 20.0; grossPay = hours * payRate; cout << "My gross pay is $" << grossPay << endl;


15 16 a. b. c. d. e.

return 0; }

lines 13 and 14 lines 8 and 9 line 14 lines 14 and 15 line 15

ANS: C MULTIPLE RESPONSE 1. Select all that apply. Which of the following statements is(are) true about named constants? a. b. c. d. e.

A named constant must be all uppercase. The content of a named constant is read-only. The value of a named constant cannot be changed while the program is running. A named constant is defined using the const qualifier. None of these

ANS: B, C, D 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. b. c. d. e.

int dozen = 12; int dozen(12); int dozen = {12}; int dozen = (12); int dozen {12};

ANS: A, B, E

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 3 Expressions and Interactivity TRUE/FALSE 1. When the fixed manipulator is used, the value specified by the setprecision manipulator will be the number of digits to appear after the decimal point. ANS: T 2. The only difference between the get function and the >> operator is that get reads the first character typed, even if it is a space, tab, or the [Enter] key. ANS: T 3. The cin << statement will stop reading input when it encounters a newline character. ANS: T


4. If you want to know the length of the string that is stored in a string object, you can call the object's size member function. ANS: F 5. Arithmetic operators that share the same precedence have right to left associativity. ANS: F 6. When C++ is working with an operator, it strives to convert the operands to the same type. ANS: T 7. When a program uses the setw manipulator, the iosetwidth header file must be included in a preprocessor directive. ANS: F 8. The following statement will output $5.00 to the screen: cout << setprecision(5) << dollars << endl; ANS: F 9. In C++, it is impossible to display the number 34.789 in a field of 9 spaces with 2 decimal places of precision. ANS: F 10. The fixed manipulator causes a number to be displayed in scientific notation. ANS: F MULTIPLE CHOICE 1. The __________ causes a program to wait until information is typed at the keyboard and the [Enter] key is pressed. a. b. c. d. e.

output stream cin object cout object preprocessor None of these

ANS: B 2. The __________ operator always follows the cin object, and the __________ operator follows the cout object. a. binary, unary b. conditional, binary


c. >>, << d. <<, >> e. None of these ANS: C 3. Which of the following must be included in any program that uses the cin object? a. b. c. d. e.

compiler the header file iostream linker brackets None of these

ANS: B 4. When a user types values at the keyboard, those values are first stored a. b. c. d. e.

as ASCII characters in the header file iostream in the keyboard buffer as integers None of these

ANS: C 5. Which of the following will allow the user to input the values 15 and 20 and have them stored in variables named base and height, respectively? a. b. c. d. e.

cin << base << height; cin base, height; cin >> base >> height; cin base >> cin height; None of these

ANS: C 6. What will be displayed after the following statements execute? int num1 = 5; int num2 = 3; cout << "The result is " << (num1 * num2 + 10) << endl; a. b. c. d. e.

The result is 5 * 3 + 10 The result is (num1 * num2 + 10) The result is 25 The result is 65 None of these

ANS: C 7. What is the value of result after the following statement executes? result = (3 * 5) % 4 + 24 / (15 - (7 - 4)); a. b. c. d.

-6.4 5 1.6 2.25


e. None of these ANS: B 8. In the following statement, what will be executed first according to the order of precedence? result = 6 - 3 * 2 + 7 - 10 / 2; a. b. c. d. e.

6 - 3 3 * 2 2 + 7 10 / 2 7 - 10

ANS: B 9. Associativity is either right to left or a. b. c. d. e.

top to bottom front to back left to right undeterminable None of these

ANS: C 10. What is the value of x after the following code executes? int x = 0; int y = 5; int z = 4; x = x + y + z * 2; a. b. c. d. e.

18 0 13 26 unknown

ANS: C 11. What is the value of average after the following code executes? double average; average = 1.0 + 2.0 + 3.0 / 3.0; a. b. c. d. e.

2.0 3.0 4.0 2 unknown

ANS: C 12. What is the value of cube after the following code executes? double cube, side; side = 5.0; cube = pow(side, 3.0);


a. b. c. d. e.

25.0 15.0 125.0 8.0 unknown

ANS: C 13. When the final value of an expression is assigned to a variable, it will be converted to a. b. c. d. e.

the smallest C++ data type the largest C++ data type the data type of the variable the data type of the expression None of these

ANS: C 14. When C++ is working with an operator, it strives to convert operands to the same type. This is known as a. b. c. d. e.

type correction type conversion promotion demotion None of these

ANS: B 15. When a variable is assigned a number that is too large for its data type, it a. b. c. d. e.

underflows overflows reverses converts None of these

ANS: B 16. What is the value of x after the following code executes? int x; x = 3 / static_cast<int>(4.5 + 6.4); a. b. c. d. e.

0.3 0 0.275229 3.3 None of these

ANS: B 17. Which statement is equivalent to the following? number += 1; a. b. c. d.

number = number + 1; number = 1; number + 1; number =+ 1;


e. None of these ANS: A 18. Which statement is equivalent to the following? number = number * 2; a. b. c. d. e.

number = pow(number, 2); number *= 2; number = number * number; number * 2 = number; None of these

ANS: B 19. What is the value of number after the following statements execute? int number = 10; number += 5; number -= 2; number *= 3; a. b. c. d. e.

3 30 39 2 None of these

ANS: C 20. This manipulator is used to establish a field width for the value that follows it: a. b. c. d. e.

field_width set_field setw iomanip None of these

ANS: C 21. This manipulator causes the field to be left justified with padding spaces printed to the right: a. b. c. d. e.

left_justify right left left_pad None of these

ANS: C 22. You can control the number of significant digits in your output with the __________ manipulator. a. b. c. d. e.

setprecision set_precision to_fixed setfixed() None of these


ANS: A 23. This manipulator forces cout to print digits in fixed-point notation: a. b. c. d. e.

setprecision(2) setw(2) fixed setfixed(2) None of these

ANS: C 24. What is true about the following statement? cout << setw(4) << num4 << " a. b. c. d.

";

It allows four spaces for the value in num4. It outputs "setw(4)" before the value in num4. It is incorrect because it should use setw(10). It is incorrect because it should use setw(num4).

ANS: A 25. Which of the following statements will pause the screen until the [Enter] key is pressed? a. b. c. d. e.

cin; cin.getline(); cin.get(); cin.ignore(); cin.input();

ANS: C 26. Which of the following statements will allow the user to enter three values to be stored in variables length, width, and height, in that order? a. b. c. d. e.

cin << length, width, height; cin.get(height, width, length); cin.get(length, width, height); cin >> length; width; height; cin.get(length >> width >> height);

ANS: C 27. Which of the following functions tells the cin object to skip one or more characters in the keyboard buffer? a. b. c. d. e.

cin.ignore cin.jump cin.hop cin.skip None of these

ANS: A 28. __________ reads a line of input, including leading and embedded spaces, and stores it in a string object.


a. b. c. d. e.

cin.get getline cin.getline get None of these

ANS: B 29. Which of the following statements will read an entire line of input into the string object, address? a. b. c. d. e.

cin << address; cin address; cin.get(address); getline(cin, address); cin.get(length >> width >> height);

ANS: D 30. How many characters will the following statement read into the variable myString? cin >> setw(10) >> myString; a. b. c. d. e.

9 10 11 however many characters are in myString None of these

ANS: A 31. The function pow(x, y), requires which header file? a. b. c. d. e.

cstdlib cstring iostream cmath iomanip

ANS: D 32. To use the rand()function, you must include the __________ header file? a. b. c. d. e.

cstdlib cstring iostream cmath iomanip

ANS: A 33. Which of the following functions will return the value of x, rounded to the nearest whole number? a. b. c. d. e.

abs(x) fmod(x) sqrt(x) round(x) whole(x)


ANS: D 34. Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 const int MY_VAL = 77; 7 MY_VAL = 99; 8 cout << MY_VAL << endl; 9 return 0; 10 } a. b. c. d. e.

line 6 line 7 line 8 line 9 there will be no compiler error

ANS: B 35. A debugging process where you, the programmer, pretend you are a computer and step through each statement while recording the value of each variable at each step is known as a. b. c. d. e.

error checking hand writing hand tracing error handling None of these

ANS: C

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 4 Making Decisions TRUE/FALSE 1. If the expression on the left side of the following is true, the expression on the right side will not be checked. (a > = b) || (c == d) ANS: T 2. If the expression on the left side of the following is false, the expression on the right side will not be checked. (a > = b) && (c == d) ANS: T 3. The default section is required in a switch statement.


ANS: F 4. An expression that has any value other than 0 is considered true by an if statement. ANS: T 5. Both of the following if statements perform the same operation. 1.

if (sales > 10000) commissionRate = 0.15;

2.

if (sales > 10000) commissionRate = 0.15;

ANS: T 6. You should be careful when using the equality operator to compare floating point values because of potential round-off errors. ANS: T 7. As a rule of style, when writing an if statement you should indent the conditionally-executed statements. ANS: T 8. The following code correctly determines whether x contains a value in the range of 0 through 100, inclusive. if (x > 0 && <= 100) ANS: F 9. The value of result in the following expression will be 0 if x has the value of 12. result = x > 100 ? 0 : 1; ANS: F 10. The conditional operator takes two operands. ANS: F MULTIPLE CHOICE 1. Relational operators allow you to __________ numbers. a. b. c. d. e.

add multiply compare average None of these


ANS: C 2. After the following code executes, what is the value of my_value if the user enters 0? cin >> my_value; if (my_value > 5) my_value = my_value + 5; else if (my_value > 2) my_value = my_value + 10; else my_value = my_value + 15; a. b. c. d. e.

15 10 25 0 5

ANS: A 3. After the following code executes, what is the output if user enters 0? int x = -1; cout << "Enter a 0 or 1: "; cin >> x; if (c) cout << "true" << endl; else cout << "false" << endl; a. b. c. d. e.

nothing will be displayed false x true 0

ANS: B 4. What is assigned to the variable result given the statement below with the following assumptions: x = 10, y = 7, and x, result, and y are all int variables. result = x >= y; a. b. c. d. e.

10 7 x >= y 1 0

ANS: D 5. If you place a semicolon after the statement: if (x < y) a. b. c. d.

the code will not compile the compiler will interpret the semicolon as a null statement the if statement will always evaluate to false All of these are true


e. None of these ANS: B 6. When a relational expression is false, it has the value a. b. c. d. e.

1 0 0, 1, or -1 -1 None of these

ANS: B 7. What is the output of the following code segment? int x = 5; if (x = 2) cout << "This is true!" << endl; else cout << "This is false!" << endl; cout << "That's all, folks!" << endl; a. This is true! b. This is false! c. This is false! That's all, folks d. This is true! That's all folks e. This is true! This is false! That's all, folks! ANS: D 8. What is the output of the following code segment if the user enters 90 for the score? cout << "Enter your test score: "; cin >> test_score; if (test_score < 60) cout << "You failed the test." << endl; if (test_score > 60) cout << "You passed the test." else cout << "You need to study harder next time." << endl; You failed the test. You passed the test. You need to study harder next time. You failed the test. You need to study harder next time. e. You passed the test. You need to study harder next time. a. b. c. d.

ANS: B 9. Whereas < is called a relational operator, x < y is called a(n)


a. b. c. d. e.

arithmetic operator relative operator relational expression arithmetic expression None of these

ANS: C 10. The __________ is an equality (or comparison) operator. a. b. c. d. e.

== >= != = None of these

ANS: A 11. What is the output of the following code segment if the user enters 23? int number; cout << "Enter a number: "; cin >> number; if (number > 0) cout << "Hi, there!" << endl; else cout << "Good-bye." << endl; a. b. c. d. e.

Hi, there! Good-bye. Hi, there! Good-bye. "Hi, there!" nothing will output

ANS: B 12. What will be displayed after the following statements execute? int funny = 7, serious = 15; funny = serious % 2; if (funny != 1) { funny = 0; serious = 0; } else if (funny == 2) { funny = 10; serious = 10; } else { funny = 1; serious = 1; } cout << funny << " " << serious << endl; a. 7

15


b. c. d. e.

0 0 10 10 1 1 None of these

ANS: D 13. What is the value of donuts after the following statement executes? int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2; a. b. c. d. e.

12 10 0 1 None of these

ANS: A 14. What is the value of donuts after the following statement executes? int donuts = 10; if (donuts = 1) donuts = 0; else donuts += 2; a. b. c. d. e.

12 10 0 1 None of these

ANS: C 15. Which of the following expressions will determine whether x is less than or equal to y? a. b. c. d. e.

x > y x =< y x >= y x <= y x == y and x < y

ANS: D 16. What is the value of result after the following code executes? int a = 60; int b = 15; int result = 10; if (a = b) result *= 2; a. 10 b. 120


c. 20 d. 12 e. code will not execute ANS: C 17. When an if statement is placed within the conditionally-executed code of another if statement, this is known as a. b. c. d. e.

complexity overloading nesting validation None of these

ANS: C 18. If you intend to place a block of statements within an if statement, you must place __________ around the block: a. b. c. d. e.

parentheses ( ) square brackets [ ] angle brackets < > curly braces { } None of these

ANS: D 19. A variable, usually a bool or an int, that signals when a condition exists is known as a(n) a. b. c. d. e.

relational operator arithmetic operator logical operator flag None of these

ANS: D 20. This operator represents the logical AND: a. b. c. d. e.

++ || && @ None of these

ANS: C 21. This operator represents the logical OR: a. b. c. d. e.

-|| && # None of these

ANS: B


22. This operator takes an operand and reverses its truth or falsehood: a. b. c. d. e.

!& || =! ! None of these

ANS: D 23. What is the value of the following expression? true && false a. b. c. d. e.

true false -1 +1 None of these

ANS: B 24. What is the value of the following expression? true && true a. b. c. d. e.

true false -1 +1 None of these

ANS: A 25. What is the value of the following expression? true || false a. b. c. d. e.

true false -1 +1 None of these

ANS: A 26. What is the value of the following expression? true && !false a. b. c. d. e.

true false -1 +1 None of these

ANS: A 27. These operators connect two or more relational expressions into one, or reverse the logic of an expression.


a. b. c. d. e.

relational logical irrational negation None of these

ANS: B 28. Which value can be entered to cause the following code segment to display the message "That number is acceptable"? int number; cin >> number; if (number > 10 && number < 100) cout << "That number is acceptable.\n"; else cout << "That number is not acceptable.\n"; a. b. c. d. e.

100 10 99 0 all of these

ANS: C 29. Which of the following is evaluated first, given the expression: A && B || C && !D a. b. c. d.

A && B B || C C && !D !D

ANS: D 30. What is the output of the following code? int w = 98; int x = 99; int y = 0; int z = 1; if (x >= 99) { if (x < 99) cout << y << endl; else cout << z << endl; } else { if (x == 99) cout << x << endl; else cout << w << endl; }


a. b. c. d. e.

98 99 0 1 None of these

ANS: D 31. Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int number = 5; 6 if (number >= 0 && <= 100) 7 cout << "passed.\n"; 8 else 9 cout << "failed.\n"; 10 return 0; 11 } a. b. c. d. e.

line 3 line 6 line 7 line 9 None will cause an error

ANS: B 32. Given that x = 2, y = 1, z = 0, what will the following cout statement display? cout << "answer = " << (x || !y && z) << endl; a. b. c. d.

answer = 0 answer = 1 answer = 2 None of these

ANS: B 33. What is the output of the following segment of code if the value 4 is input by the user? int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 4: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout << total << endl;


a. b. c. d. e.

0 3 13 23 None of these

ANS: C 34. This statement uses the value of a variable or expression to determine where the program will branch to. a. b. c. d. e.

switch select association scope None of these

ANS: A 35. Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression. a. b. c. d. e.

break exit switch scope None of these

ANS: A 36. The default section of a switch statement performs a similar task similar to the __________ portion of an if/else if statement. a. b. c. d. e.

conditional break trailing else All of these None of these

ANS: C 37. Which statement allows you to properly check the char variable code to determine whether it is equal to a C and then output This is a check? a. if code is equal to C cout << "This is a check\n"; b. if (code = "C") cout << "This is a check\n"; if (code == 'C') c. cout << "This is a check\n"; d. if (code == C) cout << "This is a check" << endl; ANS: C 38. Given the following code segment, what is the output? int x = 1, y y = y + z;

= 1, z = 1;


x = x + y; cout << "result = " << (x < y ? y : x) << endl; a. b. c. d. e.

result = 0 result = 1 result = 2 result = 3 there will be no output

ANS: D 39. Given the if/else statement: if (a < 5) b = 12; else d = 30; Which of the following performs the same operation? a. b. c. d. e.

a < 5 ? b = 12 : d = 30; b < 5 ? b = 12 : d = 30; a >= 5 ? d = 30 : b = 12; d = 30 ? b = 12 : a = 5; None of these

ANS: A 40. When a program lets the user know that an invalid choice has been made, this is known as: a. b. c. d. e.

input validation output correction compiler criticism output validation None of these

ANS: A 41. Input values should always be checked for a. b. c. d. e.

an appropriate range reasonableness division by zero, if division is taking place All of these None of these

ANS: D MULTIPLE RESPONSE 1. Select all that apply. Given: x = 5, y = 6, z = 8. Which of the following are false? 1. 2. 3. 4. 5. 6.

x == 5; x < (y + 2); z <= 4; y > (z - x); z >= (y + x) y <= 6


a. 1 b. 2 c. 3

d. 4 e. 5 f. 6

ANS: C, E

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 5 Loops and Files TRUE/FALSE 1. The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions. ANS: F 2. A while loop's body can contain multiple statements, as long as they are enclosed in braces. ANS: T 3. A while loop is somewhat limited because the counter can only be incremented by one each time through the loop. ANS: F 4. An initialization expression may be omitted from the for loop if no initialization is required. ANS: T 5. The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop. ANS: F 6. If you want to stop a loop before it goes through all of its iterations, the break statement may be used. ANS: T 7. You may not use both break and continue statements within the same set of nested loops. ANS: F 8. The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon. ANS: F 9. The update expression of a for loop can contain more than one statement, for example: for(i = 5; i <= 10; i++, total+= sales)


ANS: T 10. Multiple relational expressions cannot be placed into the test condition of a for loop. ANS: T 11. You may nest while and do-while loops but you may not nest for loops. ANS: F 12. You may not use the break statement in a nested loop. ANS: F 13. An output file is a file that data is written to. ANS: T 14. It is possible to define a file stream object and open a file in one statement. ANS: T 15. In C++ 11 you can pass a string object as an argument to a file stream object's open member function. ANS: T 16. string objects have a member function named c_str that returns the contents of the object formatted as a null-terminated C-string. ANS: T MULTIPLE CHOICE 1. These are operators that add and subtract one from their operands. a. b. c. d. e.

plus and minus ++ and -binary and unary conditional and relational binary and ternary

ANS: B 2. This operator increments the value of its operand and then uses the value in context. a. b. c. d.

prefix increment postfix increment prefix decrement postfix decrement


e. None of these ANS: A 3. This is a control structure that causes a statement or group of statements to repeat. a. b. c. d. e.

decision statement loop cout object selection structure None of these

ANS: B 4. The two important parts of a while loop are the expression that is tested for a true or false value and a. b. c. d.

a statement or block that is repeated as long as the expression is true a statement or block that is repeated only if the expression is false one line of code that is repeated once, if the expression is true a statement or block that is repeated once, if the expression is true

ANS: A 5. Something within a while loop must eventually cause the condition to become false or a(n) __________ results. a. b. c. d. e.

null value infinite loop unexpected exit compiler error None of these

ANS: B 6. The while loop is a __________ loop. a. b. c. d. e.

post-test pre-test infinite limited None of these

ANS: B 7. If you place a semicolon after the test expression in a while loop, it is assumed to be a(n) a. b. c. d. e.

pre-test loop post-test loop null statement infinite loop None of these

ANS: C 8. The statements in the body of a while loop may never be executed while the statements in the body of a do-while loop will be executed


a. b. c. d. e.

at least once at least twice never as many times as the user wishes None of these

ANS: A 9. When the increment operator precedes its operand, as ++num, the expression is in __________ mode. a. b. c. d. e.

postfix prefix preliminary binary None of these

ANS: B 10. This means to increase a value: a. b. c. d. e.

decrement increment modulus parse None of these

ANS: B 11. A statement that may be used to stop a loop's current iteration and begin the next one is a. b. c. d. e.

break terminate re-iterate continue None of these

ANS: D 12. A statement that causes a loop to terminate early is a. b. c. d. e.

break terminate re-iterate continue None of these

ANS: A 13. A variable that is regularly incremented or decremented each time a loop iterates is a a. b. c. d. e.

constant counter control null terminator None of these

ANS: B


14. A special value that marks the end of a list of values is a a. b. c. d. e.

constant counter variable sentinel None of these

ANS: D 15. What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' n++; a. b. c. d. e.

';

1 2 3 4 5 1 1 ... and on forever 1 2 3 4 5 6 1 2 3 4 2 3 4 5

ANS: B 16. In the following statement, which operator is used first? while (x++ < 10) a. b. c. d.

++ < neither; the expression is invalid cannot tell without the rest of the code

ANS: B 17. What will the following code display? int number = 6; number++; cout << number << endl; a. b. c. d.

6 5 7 0

ANS: C 18. What will the following code display? int number = 6; ++number; cout << number << endl; a. b. c. d.

6 5 7 0


ANS: C 19. What will the following code display? int number = 6; cout << number++ << endl; a. b. c. d.

6 5 7 0

ANS: A 20. What will the following code display? int number = 6; cout << ++number << endl; a. b. c. d.

6 5 7 0

ANS: C 21. What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl; a. b. c. d.

6 5 7 0

ANS: A 22. What will the following code display? int number = 6; int x = 0; x = --number; cout << x << endl; a. b. c. d.

6 5 7 0

ANS: B 23. A for statement contains three expressions: initialization, test, and a. b. c. d.

update reversal null validation


e. None of these ANS: A 24. In a for statement, this expression is executed only once: a. b. c. d. e.

the test null initialization validation None of these

ANS: C 25. You may define a __________ in the initialization expression of a for loop. a. b. c. d. e.

constant function variable new data type None of these

ANS: C 26. The do-while loop is considered a. b. c. d. e.

a pre-test loop a post-test loop an infinite loop a counter-controlled loop None of these

ANS: B 27. The __________ loop is ideal in situations where you want the loop to iterate at least once. a. b. c. d. e.

while for switch do-while pre-test

ANS: D 28. The __________ loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop. a. b. c. d. e.

while for switch do-while pre-test

ANS: B 29. The __________ loop is a good choice when you do not want the loop to iterate if the condition is false in the beginning. a. while


b. c. d. e.

infinite switch do-while post-test

ANS: A 30. If you want a user to enter exactly 20 values, which loop would be the best to use? a. b. c. d. e.

while for switch do-while None of these

ANS: B 31. What will the following code display? int x = 0; while (x < 5) { cout << x << " "; x++; } a. 0 1 2 3 4 5 b. 0 1 2 3 4 c. 0 1 2 3 4 d. This is an infinite loop ANS: B 32. What will the following code display? int x = 0; for (int count = 0; count < 3; count++) x += count; cout << x << endl; a. 0 1 2 b. 0 c. 6 d. 3 ANS: D 33. How many times will the following loop display "Hello world!"? for (int i = 0; i < 20; i++) cout << "Hello world!" << endl; a. 20


b. 19 c. 21 d. an infinite number of times ANS: A 34. How many times will the following loop display "Looping again!"? for (int i = 0; i <= 20; i++) cout << "Looping again!" << endl; a. b. c. d.

20 19 21 an infinite number of times

ANS: C 35. How many times will the following loop display "Looping!"? for (int i = 20; i > 0; i--) cout << "Looping!" << endl; a. b. c. d.

20 19 21 an infinite number of times

ANS: A 36. A loop that is inside another loop is called a(n) a. b. c. d. e.

infinite loop pre-test loop post-test loop nested loop None of these

ANS: D 37. A file must be __________ before data can be written to or read from it. a. b. c. d. e.

opened closed named buffered initialized

ANS: A 38. A file __________ is a small holding section of memory that file-bound information is first written to. a. b. c. d. e.

name number buffer segment None of these

ANS: C


39. To write information to a file, use the a. b. c. d. e.

cout object pen object output object stream insertion operator None of these

ANS: D 40. To allow file access in a program, you must use __________ in the header file. a. b. c. d.

#include file #include fileaccess #include fstream #include cfile

ANS: C 41. To write data to a file, you define an object of the __________ data type. a. b. c. d.

outputFile ifstream fstream ofstream

ANS: D 42. To write read data from a file, you define an object of the __________ data type. a. b. c. d.

inputFile ifstream fstream ofstream

ANS: B 43. Assuming outFile is a file stream object and number is a variable, which statement writes the contents of number to the file associated with outFile? a. b. c. d.

write(outFile, number); outFile >> number; outFile << number; number >> outFile;

ANS: C 44. Assuming dataFile is a file stream object, the following statement: dataFile.close(); a. b. c. d. e.

is illegal in C++ needs a filename argument to execute correctly closes a file is legal but risks losing valuable data None of these

ANS: C


Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 6 Functions TRUE/FALSE 1. When a function is called, flow of control moves to the function's prototype. ANS: F 2. A parameter is a special purpose variable that is declared inside the parentheses of a function definition. ANS: T 3. A local variable and a global variable may not have the same name within a program. ANS: F 4. A static variable that is defined within a function is initalized only once, the first time it is called. ANS: T 5. It is possible for a function to have some parameters with default arguments and some without. ANS: T 6. A function's return data type must be the same as the function's parameters. ANS: F 7. One reason for using functions is to break programs into manageable units or modules. ANS: T 8. You must always furnish an argument with a function call. ANS: F 9. Global variables are initialized to zero by default. ANS: T 10. Local variables are initialized to zero by default. ANS: F 11. It is not considered good programming practice to declare all your variables globally. ANS: T


12. You may use the exit() function to terminate a program, regardless of which control mechanism is executing. ANS: T MULTIPLE CHOICE 1. A collection of statements that performs a specific task is a(n) a. b. c. d. e.

infinite loop variable constant function decision

ANS: D 2. A function __________ contains the statements that make up the function. a. b. c. d. e.

prototype definition call expression parameter list

ANS: B 3. A function can have no parameters, one parameter, or many parameters and can return __________ value(s). a. b. c. d. e.

zero to many no only one a maximum of ten None of these

ANS: C 4. A function is executed when it is a. b. c. d. e.

defined prototyped declared called None of these

ANS: D 5. Functions are ideal for menu-driven programs. When the user selects a menu item, the program can __________ the appropriate function. a. b. c. d.

call append define declare


e. None of these ANS: A 6. This type of variable is defined inside a function and is not accessible outside the function. a. b. c. d. e.

global reference local counter None of these

ANS: C 7. The value in this type of variable persists between function calls. a. b. c. d. e.

global internal static dynamic None of these

ANS: C 8. These types of arguments are passed to parameters automatically if no argument is provided in the function call. a. b. c. d. e.

local default global reference None of these

ANS: B 9. When used as parameters, these types of variables allow a function to access the parameter's original argument: a. b. c. d. e.

reference floating-point counter undeclared None of these

ANS: A 10. __________ functions may have the same name as long as their parameter lists are different. a. b. c. d. e.

Only two Two or more No Un-prototyped None of these

ANS: B 11. This statement causes a function to end. a. end


b. c. d. e.

terminate return release None of these

ANS: C 12. This function causes a program to terminate, regardless of which function or control mechanism is executing. a. b. c. d. e.

exit() terminate() return() continue() None of these

ANS: A 13. Which of the following causes a function to execute? a. b. c. d. e.

a for loop a do-while loop a function prototype a function call None of these

ANS: D 14. It is good programming practice to __________ your functions by writing comments that describe what they do. a. b. c. d. e.

execute document retrieve create None of these

ANS: B 15. A(n) __________ is information that is passed to a function, and a(n) __________ is information that is received by a function. a. b. c. d. e.

function call, function header parameter, argument argument, parameter prototype, header None of these

ANS: C 16. A function _________ eliminates the need to place a function definition before all calls to the function. a. b. c. d. e.

header prototype argument parameter None of these


ANS: B 17. A _________ variable is declared outside all functions. a. b. c. d. e.

local global floating-point counter None of these

ANS: B 18. If a function is called more than once in a program, the values stored in the function's local variables do not _________ between function calls. a. b. c. d. e.

persist execute communicate change None of these

ANS: A 19. A __________ argument is passed to a parameter when the actual argument is left out of the function. a. b. c. d. e.

false true null default None of these

ANS: D 20. If a function does not have a prototype, default arguments may be specified in the __________. a. b. c. d. e.

function call function header execution return type None of these

ANS: B 21. The value in a __________ variable persists between function calls. a. b. c. d. e.

dynamic global floating-point static local counter

ANS: D 22. Given the following function: void calc (int a, int& b) { int c; c = a + 2;


a = a * 3; b = c + a; } What is the output of the following code segment that invokes calc(): int x = 1; int y = 2; int z = 3; calc(x, y); cout << x << " a. b. c. d. e.

" << y << "

"

<< z << endl;

1 2 3 1 6 3 3 6 3 1 14 9 2 3 4 5

ANS: B 23. EXIT_FAILURE and ________ are named constants that may be used to indicate success or failure when the exit() function is called. a. b. c. d.

EXIT_TERMINATE EXIT_SUCCESS EXIT_OK RETURN_OK

ANS: B 24. This is a dummy function that is called instead of the actual function it represents: a. b. c. d.

main stub a driver an overloaded function

ANS: B 25. What will the following code display? #include <iostream> using namespace std; void showDub(int); int main() { int x = 2; showDub(x); cout << x << endl; return 0; } void showDub(int num) { cout << (num * 2) << endl; } a. 2 2


b. 4 2 c. 2 4 d. 4 4 ANS: B 26. What will the following code display? #include <iostream> using namespace std; void doSomething(int); int main() { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int num) { num = 0; cout << num << endl; } a. 2 0 2 b. 2 2 2 c. 0 0 0 d. 2 0 0 ANS: A 27. What will the following code display? #include <iostream> using namespace std; void doSomething(int&); int main() { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int& num)


{ num = 0; cout << num << endl; } a. 2 0 2 b. 2 2 2 c. 0 0 0 d. 2 0 0 ANS: D 28. Which line in the following program contains the prototype showDub function? 1 2

3 4 5 6

7 8 9 10 11 12 13 14

a. b. c. d.

#include <iostream> using namespace std; void showDub(int); int main() { int x = 2; showDub(x); cout << x << endl; return 0; } void showDub(int num) { cout << (num * 2) << endl; }

line 3 line 4 line 7 line 11

ANS: A 29. Which line in the following program contains the header for the showDub function? 1 2

3 4 5 6

7 8 9

#include <iostream> using namespace std; void showDub(int); int main() { int x = 2; showDub(x); cout << x << endl; return 0;


} void showDub(int num) { cout << (num * 2) << endl; }

10 11 12 13 14

a. b. c. d.

line 3 line 4 line 7 line 11

ANS: D 30. Which line in the following program contains a call to the showDub function? 1 2

3 4 5 6

7 8 9 10 11 12 13 14

a. b. c. d.

#include <iostream> using namespace std; void showDub(int); int main() { int x = 2; showDub(x); cout << x << endl; return 0; } void showDub(int num) { cout << (num * 2) << endl; }

line 3 line 4 line 7 line 11

ANS: C 31. What is the data type of the following function prototype's parameter variable? int myFunction(double); a. b. c. d.

int double void cannot tell from the prototype

ANS: B 32. What is the data type of the following function prototype's return value? int myFunction(double); a. b. c. d.

int double void cannot tell from the prototype


ANS: A 33. In the following function prototype, how many parameter variables does this function have? int myFunction(double, double, double); a. b. c. d.

1 2 3 cannot tell from the prototype

ANS: C 34. What will the following code display? #include <iostream> using namespace std; int getValue(int); int main() { int x = 2; cout << getValue(x) << endl; return 0; } int getValue(int num) { return num + 5; } a. b. c. d.

5 2 7 getValue(x)

ANS: C 35. Given the following header for a function named computeValue, which of the following is a valid call to the function? void computeValue(int value) a. b. c. d.

computeValue(10) computeValue(10); void computeValue(10); void computeValue(int x);

ANS: B MULTIPLE RESPONSE 1. Select all that apply. Which of the following must be included in a function header? a. b. c. d.

the data type of each parameter the data type of the return value the name of the function the names of parameter variables

ANS: A, B, C, D


2. Select all that apply. Which of the following statement(s) about global variables is(are) true? a. A global variable is accessible only to the main function. b. A global variable is declared in the highest-level block in which it is used. c. A global variable can have the same name as a variable that is declared locally within a function. d. A global variable is the same as a named constant. e. If a function contains a local variable with the same name as a global variable, the global variable's name takes precedence within the function. ANS: A, B, C, E

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 7 Arrays and Vectors TRUE/FALSE 1. The amount of memory used by an array depends on the array's data type and the number of elements in the array. ANS: T 2. An array initialization must be all on one line. ANS: F 3. When you pass an array as an argument to a function, the function can modify the contents of the array. ANS: T 4. C++ limits the number of array dimensions to two. ANS: F 5. If you attempt to store data past an array's boundaries, it is guaranteed to cause a compiler error. ANS: F 6. An individual array element can be processed like any other type of C++ variable. ANS: T 7. In C++11 you cannot use a range-based for loop to modify the contents of an array unless you declare the range variable as a reference variable. ANS: T 8. In C++11 the range-based for loop is best used in situations where you need the element subscript for some purpose.


ANS: F 9. Although two-dimensional arrays are a novel idea, there is no known way to pass one to a function. ANS: F 10. Each individual element of an array can be accessed by the array name and the element subscript. ANS: T 11. If an array is partially initialized, the uninitialized elements will be set to zero. ANS: T 12. The following statement is a valid C++ definition: double money[25.00]; ANS: F 13. Assume array1 and array2 are the names of two arrays. To assign the contents of array2 to array1, you would use the following statement: array1 = array2; ANS: F 14. A vector object automatically expands in size to accommodate the items stored in it. ANS: T MULTIPLE CHOICE 1. Unlike regular variables, __________ can hold multiple values. a. b. c. d. e.

constants named constants arrays floats None of these

ANS: C 2. The individual values contained in an array are known as a. b. c. d. e.

parts items constants elements None of these

ANS: D


3. To access an array element, use the array name and the element's ___________. a. b. c. d. e.

data type subscript value name None of these

ANS: B 4. By using the same ___________ you can build relationships between data stored in two or more arrays. a. b. c. d. e.

array name data types subscript arguments None of these

ANS: C 5. The name of an array stores the __________ of the first array element. a. b. c. d. e.

value memory address element number data type None of these

ANS: B 6. Which of the following is a valid C++ array definition? a. b. c. d. e.

int nums[0]; float $payments[10]; void numbers[5]; int sizes[10]; None of these

ANS: D 7. Which of the following is a valid C++ array definition? a. b. c. d. e.

int array[0]; float $payments[10.23]; int numbers[5.6]; int scores[25]; None of these

ANS: D 8. If you leave out the size declarator in an array definition a. b. c. d. e.

you must furnish an initialization list you are not required to initialize array elements all array elements default to zero values your array will contain no elements None of these


ANS: A 9. An array can store a group of values, but the values must be a. b. c. d. e.

the same data type integers and floating-point numbers integers constants None of these

ANS: A 10. An array's size declarator must be a __________ with a value greater than __________. a. b. c. d. e.

number, one number, zero constant integer expression, zero variable, -1 None of these

ANS: C 11. Subscript numbering in C++ a. b. c. d. e.

can be set at runtime can begin with a value defined by the programmer varies from program to program begins with zero None of these

ANS: D 12. Arrays must be __________ at the time they are __________. a. b. c. d. e.

sized, executed re-scoped, deleted initialized, declared compiled, typed None of these

ANS: C 13. This following statement shows an example of ___________. int grades][ ] = {100, 90, 99, 80}; a. b. c. d. e.

default arguments an illegal array declaration an illegal array initialization implicit array sizing None of these

ANS: D 14. Given the following declaration, where is the value 77 stored in the scores array? int scores[] = {83, 62, 77, 97, 86}


a. b. c. d. e.

scores[0] scores[1] scores[2] scores[3] scores[5]

ANS: C 15. An array can easily be stepped through by using a a. b. c. d. e.

a for loop a reference variable an array prototype a null value None of these

ANS: A 16. The range-based for loop in C++11 is designed to work with a built-in variable known as a. b. c. d. e.

the counter the i variable an iterator the range variable None of these

ANS: D 17. A(n) __________ can be used to specify the starting values of an array. a. b. c. d. e.

initialization list array name subscript element None of these

ANS: A 18. An array with no elements is a. b. c. d. e.

legal in C++ illegal in C++ automatically furnished with one element whose value is set to zero automatically furnished with one element, the null terminator None of these

ANS: B 19. It is _________ to pass an argument to a function that contains an individual array element, such as scores[3]. a. b. c. d. e.

illegal in C++11 legal in C++ not recommended by the ANSI committee not good programming practice None of these

ANS: B


20. What is the last legal subscript that can be used with the following array? int values[5]; a. b. c. d. e.

0 5 6 4 1

ANS: D 21. How many elements does the following array have? int values[1000]; a. b. c. d.

1000 999 1001 cannot tell from the code

ANS: A 22. To assign the contents of one array to another, you must use a. b. c. d. e.

the assignment operator with the array names the equality operator with the array names a loop to assign the elements of one array to the other array Any of these None of these

ANS: C 23. To pass an array as an argument to a function, pass the __________ of the array. a. b. c. d. e.

contents size, expressed as an integer name value of the first element None of these

ANS: C 24. The __________ is automatically appended to a character array when it is initialized with a string constant. a. b. c. d. e.

array name number of elements value of the first element null terminator None of these

ANS: D 25. An array of string objects that will hold five names would be declared with which of the following statements? a. string names[5]; b. string names(5);


c. string names 5; d. String[5] = names; ANS: A 26. A two-dimensional array can be viewed as a. b. c. d. e.

rows and columns arguments and parameters increments and decrements All of these None of these

ANS: A 27. An element of a two-dimensional array is referred to by a. b. c. d. e.

the array name followed by the column number of the element the row subscript of the element followed by the column subscript of the element a comma followed by a semicolon the row subscript of the element followed by the array name None of these

ANS: B 28. When writing functions that accept multi-dimensional arrays as arguments, __________ must be explicitly stated in the parameter list. a. b. c. d. e.

all dimensions all but the first dimension the size declarator of the first dimension all element values None of these

ANS: B 29. A two-dimensional array can have elements of __________ data type(s). a. b. c. d. e.

one two four any number of None of these

ANS: A 30. A two-dimensional array of characters can contain a. b. c. d. e.

strings of the same length strings of different lengths uninitialized elements All of these None of these

ANS: D 31. What will the following code display? int numbers[] = {99, 87, 66, 55, 101};


cout << numbers[3] << endl; a. b. c. d.

55 66 101 87

ANS: A 32. What will the following code display? int numbers[] = {99, 87, 66, 55, 101}; for (int i = 1; i < 4; i++) cout << numbers[i] << " "; a. b. c. d.

99 87 66 55 101 87 66 55 101 87 66 55 Nothing. This code has an error.

ANS: C 33. What will the following code display? int numbers[4] = {99, 87}; cout << numbers[3] << endl; a. b. c. d. e.

87 0 99 garbage This code will not compile

ANS: B 34. What does the following code do? const int SIZE = 5; double x[SIZE]; for (int i = 2; i <= SIZE; i++) { x[i] = 0.0; } a. b. c. d.

Each element in the array is initialized to 0.0. Each element in the array except the first is initialized to 0.0. Each element in the array except the first and last is initialized to 0.0. An error will occur when the code runs.

ANS: D 35. Which statement correctly defines a vector object for holding integers? a. b. c. d.

vector v<int>; int vector v; int<vector> v; vector<int> v;

ANS: D


36. Which statement correctly uses C++11 to initalize a vector of ints named n with the values 10 and 20? a. b. c. d.

vector n<int>(10, 20); vector<int> n = {10, 20}; vector<int> n {10, 20}; int vector n ({10}, {20});

ANS: C 37. What does the following statement do? vector<int> v(10); a. b. c. d.

It creates a vector object and initializes all its elements to the value 10. It creates a vector object with a starting size of 10. It creates a vector object and initializes the first element with the value 10. It creates a vector object that can only store values of 10 or less.

ANS: B 38. What does the following statement do? vector<int> v(10, 2); a. It creates a vector object and initializes the first two elements with the values 10 and 2. b. It creates a vector object with a starting size of 2 and initializes the first element with the value 10. c. It creates a vector object with a starting size of 10 and initializes the first element with the value 2. d. It creates a vector object with a starting size of 10 and initializes all the elements with the value 2. ANS: D 39. This vector function is used to insert an item into a vector. a. b. c. d.

insert_item add_item store push_back

ANS: D 40. This vector function removes an item from a vector. a. b. c. d.

remove_item delete_item erase pop_back

ANS: D 41. This vector function returns the number of elements in a vector. a. size b. num_elements


c. elements d. length ANS: A 42. This vector function returns true if the vector has no elements. a. b. c. d.

has_no_elements null_size empty is_empty

ANS: C 43. What will the following C++11 code display? vector<int> numbers {3, 5}; for (int val : numbers) cout << val << endl; a. 5 5 5 b. 3 3 3 3 3 c. 3 5 d. Nothing. This code has an error. ANS: C

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 8 Searching and Sorting Arrays TRUE/FALSE 1. The bubble sort is an easy way to arrange data in ascending order but it cannot arrange data in descending order. ANS: F 2. The number of comparisons made by a binary search is expressed in powers of two. ANS: T 3. On average, an item is just as likely to be found near the beginning of an array as near the end. ANS: T 4. A linear search can only be implemented with integer values.


ANS: F 5. Before you can perform a selection sort, the data must be stored in ascending order. ANS: F 6. Before you can perform a bubble sort, the data must be stored in descending order. ANS: F 7. You are more likely to find an item by using a binary search than by using a linear search. ANS: F 8. If you are using the bubble sort algorithm to sort an array in descending order, the smaller values move toward the end. ANS: T 9. A selection sort and a binary search can be applied to STL vectors as well as arrays. ANS: T 10. The linear search repeatedly divides the portion of an array being searched in half. ANS: F MULTIPLE CHOICE 1. A __________ algorithm is a method of locating a specific item of information in a larger collection of data. a. b. c. d. e.

sort search standard linear None of these

ANS: B 2. The advantage of a linear search is its a. b. c. d. e.

complexity efficiency simplicity speed None of these

ANS: C 3. A __________ search is more efficient than a __________ search.


a. b. c. d. e.

character, string integer, double binary, linear linear, binary None of these

ANS: C 4. A binary search begins with the __________ element of an array. a. b. c. d. e.

first last largest middle None of these

ANS: D 5. The __________ sort usually performs fewer exchanges than the __________ sort. a. b. c. d. e.

bubble, selection binary, linear selection, bubble ANSI, ASCII None of these

ANS: C 6. Array elements must __________ before a binary search can be performed. a. b. c. d. e.

summed set to zero positive integers sorted None of these

ANS: D 7. Using a linear search to find a value that is stored in the last element of an array that contains 20,000 elements, __________ elements must be compared. a. b. c. d. e.

20,000 only the first two only half 2,000 None of these

ANS: A 8. A(n) __________ search uses a loop to sequentially step through an array. a. b. c. d. e.

binary unary linear relative None of these

ANS: C


9. Data that is to be sorted in ascending order is ordered a. b. c. d. e.

from lowest value to highest value from highest value to lowest value with a binary search algorithm by identifying the middle value and going up and down from there None of these

ANS: A 10. Regardless of the algorithm being used, a search through an array is always performed a. b. c. d. e.

from lowest to highest element from highest to lowest element beginning with the middle element using a binary search algorithm None of these

ANS: E 11. When an array is sorted from highest to lowest, it is said to be in a. b. c. d. e.

reverse order forward order ascending order descending order None of these

ANS: D 12. The __________ is adequate for searching through small arrays. a. b. c. d. e.

binary search the linear search unary search bubble sort None of these

ANS: B 13. Algorithms used to arrange random data in some order are __________ algorithms. a. b. c. d. e.

standard search sorting linear binary search None of these

ANS: B 14. Assume you have two integer variables, num1 and num2. Which of the following is the correct way to swap the values in these two variables? a. int temp = num1; num2 = num1; num1 = num2; b. int temp = num2;


num2 = num1; num1 = temp; c. num1 = num2; num2 = num1; d. int temp = num1; num2 = temp; temp = num2; num1 = temp; e. None of these ANS: B 15. The following function should swap the values contained in two integer variables, num1 and num2. What, if anything, is wrong with this function? void swap(int num1, int num2) { int temp = num2; num2 = num1; num1 = temp; } a. b. c. d. e.

You must first initalize temp to 0 before using it. The variable temp should first be set to num1, not num2. The swap function must use reference parameters. The last line should be temp = num1. Nothing is wrong with this function.

ANS: C 16. The following is the pseudocode for which type of algorithm? Set found to false Set position to -1 Set index to 0 While found is false and index < number of elements If list[index] is equal to search value found = true position = index End If Add 1 to index End While Return position a. b. c. d. e.

linear sort linear search binary search selection sort None of these

ANS: B 17. The following is the pseudocode for which type of algorithm? Set first to 0 Set last to the last subscript in the array


Set found to false Set position to -1 While found is not true and first is less than or equal to last Set middle to the subscript halfway between array[first] and array[last] If array[middle] equals the desired value Set found to true Set position to middle Else If array[middle] is greater than the desired value Set last to middle - 1 Else Set first to middle + 1 End If End While Return position a. b. c. d. e.

linear sort linear search binary search selection sort None of these

ANS: C 18. The following is the pseudocode for which type of algorithm? For maxElement = each subscript in the array, from the last to the first For index = 0 To maxElement - 1 If array[index] > array[index + 1] swap array[index] with array[index + 1] End If End For End For a. b. c. d. e.

bubble sort binary sort bubble search selection sort None of these

ANS: A 19. The following is the pseudocode for which type of algorithm? For start = each array subscript, from the first to the next-to-last minIndex = start minValue = array[start] For index = start + 1 To size - 1 If array[index] < minValue minValue = array[index] minIndex = index End If End For swap array[minIndex] with array[start] End For a. bubble sort b. binary sort


c. bubble search d. selection sort e. None of these ANS: D 20. What is the output after the following code executes? int numerator = 5; int denominator = 25; int temp = 0; temp = numerator; numerator = denominator; denominator = temp; cout << numerator << "/" << denominator << " = " << (numerator/denominator) << endl; a. b. c. d. e.

5/25 = numerator/denominator 5/25 = 0 5/25 = 0.2 25/5 = 5 25/5 = 25/5

ANS: D

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 9 Pointers TRUE/FALSE 1. With pointer variables you can access but not modify data in other variables. ANS: F 2. An array name is a pointer constant because the address stored in it cannot be changed at runtime. ANS: T 3. In C++11, the nullptr keyword was introduced to represent the address 0. ANS: T 4. It is legal to subtract a pointer variable from another pointer variable. ANS: T 5. C++ does not perform array bounds checking, making it possible for you to assign a pointer the address of an element out of the boundaries of an array. ANS: T 6. A pointer can be used as a function argument, giving the function access to the original argument.


ANS: T 7. The ampersand (&) is used to dereference a pointer variable in C++. ANS: F 8. Assuming myValues is an array of int values and index is an int variable, both of the following statements do the same thing. 1.

cout << myValues[index] << endl;

2.

cout << *(myValues + index) << endl;

ANS: T 9. In C++11 you can use smart pointers to dynamically allocate memory and not worry about deleting the memory when you are finished using it. ANS: T 10. The weak_ptr can share ownership of a piece of dynamically allocated memory. ANS: F 11. The unique_ptr

is the sole owner of a piece of dynamically allocated memory.

ANS: T 12. To use any of the smart pointers in C++11 you must use the following directive in the header file: #include <memory> ANS: T MULTIPLE CHOICE 1. The __________, also known as the address operator, returns the memory address of a variable. a. b. c. d. e.

asterisk (* ) ampersand ( & ) percent sign ( % ) exclamation point ( ! ) None of these

ANS: B 2. With pointer variables you can __________ manipulate data stored in other variables. a. b. c. d.

never seldom indirectly All of these


e. None of these ANS: C 3. When you work with a dereferenced pointer, you are actually working with a. b. c. d.

a variable whose memory has been allocated a copy of the value pointed to by the pointer variable the actual value of the variable whose address is stored in the pointer variable None of these

ANS: C 4. A function may return a pointer but the programmer must ensure that the pointer a. b. c. d. e.

still points to a valid object after the function ends has not been assigned an address was received as a parameter by the function has not previously been returned by another function None of these

ANS: A 5. What does the following statement do? double *num2; a. b. c. d. e.

Declares a double variable named num2 Declares and initializes a pointer variable named num2 Initializes a pointer variable named num2 Declares a pointer variable named num2 None of these

ANS: D 6. In C++11, the __________ keyword was introduced to represent address 0. a. b. c. d. e.

nullptr NULL weak_ptr shared_ptr None of these

ANS: A

7. When the less than operator (<) is used between two pointer values, the expression is testing whether a. the value pointed to by the first is less than the value pointed to by the second b. the value pointed to by the first is greater than the value pointed to by the second c. the address of the first variable comes before the address of the second variable in the computer's memory d. the first variable was declared before the second variable e. None of these


ANS: C 8. Use the delete operator only on pointers that were a. never used b. not correctly initialized c. created with the new operator d. dereferenced inappropriately e. None of these ANS: C

9. Which of the following is true about this statement: sum += *array++; a. This statement is illegal in C++. b. This statement will cause a compiler error. c. This statement assigns the dereferenced pointer's value, then increments the pointer's address. d. This statement increments the dereferenced pointer's value by one, then assign that value. e. None of these ANS: C 10. In the following statement, what does int mean? int *ptr = nullptr; a. The variable named *ptr will store an integer value. b. The variable named *ptr will store an asterisk and an integer value c. ptr is a pointer variable and will store the address of an integer variable. d. The variable named *ptr will store the value in nullptr. e. None of these ANS: C 11. Assuming ptr is a pointer variable, what will the following statement output? cout << *ptr;


a. the value stored in the variable whose address is contained in ptr b. the string "*ptr" c. the address of the variable whose address is stored in ptr d. the address of the variable stored in ptr e. None of these ANS: A

12. Which of the following statements is not valid C++ code? a. int ptr = &num1; b. int ptr = int *num1; c. float num1 = &ptr2; d. All of these are valid e. All of these are invalid ANS: E

13. Which of the following statements deletes memory that has been dynamically allocated for an array? a. int array = delete memory; b. int delete[ ]; c. delete [] array; d. new array = delete; e. None of these ANS: C 14. Which of the following statements displays the address of the variable numb? a. cout << numb; b. cout << *numb; c. cin >> &numb; d. cout << &numb; e. None of these ANS: D

15. What will the following statement output?


cout << &num1; a. the value stored in the variable named num1 b. the memory address of the variable named num1 c. the number 1 d. the string &num1 e. None of these ANS: B

16. The following statement __________ cin >> *num3; a. stores the keyboard input in the variable num3 b. stores the keyboard input into the pointer num3 c. is illegal in C++ d. stores the keyboard input into the variable pointed to by num3 e. None of these ANS: D

17. The following statement __________ int *ptr = new int; a. results in a compiler error b. assigns an integer less than 32767 to the variable ptr c. assigns an address to the variable ptr d. creates a new pointer named int e. None of these ANS: C

18. A pointer variable is designed to store a. any legal C++ value b. only floating-point values c. an integer d. a memory address


e. None of these ANS: D

19. The __________ and __________ operators can be used to increment or decrement a pointer variable. a. addition, subtraction b. ++, -c. modulus, division d. All of these e. None of these ANS: B

20. Not all arithmetic operations can be performed on pointers. For example, you cannot __________ or __________ pointers. a. multiply, divide b. +=, -= c. add, subtract d. increment, decrement e. None of these ANS: A

21. Dynamic memory allocation occurs a. when a new variable is created by the compiler b. when a new variable is created at runtime c. when a pointer fails to dereference the right variable d. when a pointer is assigned an incorrect address e. None of these ANS: B

22. Every byte in the computer's memory is assigned a unique a. pointer b. address c. dynamic allocation


d. name e. None of these ANS: B

23. A pointer variable may be initialized with a. any nonzero integer value b. a valid address in the computer's memory c. an address less than zero d. any nonzero number e. None of these ANS: B

24. If a variable uses more than one byte of memory, for pointer purposes its address is a. the address of the last byte of storage b. the average of all the addresses used to store that variable c. the address of the first byte of storage d. the address of the second byte of storage e. None of these ANS: C

25. If you are using an older computer that does not support the C++11 standard, you should initialize pointers with a. the integer 0 or the value NULL b. the null terminator '\0' c. a nonzero value d. Any of these e. None of these ANS: A

26. When you pass a pointer as an argument to a function, you must a. declare the pointer value again in the function call b. dereference the pointer value in the function prototype


c. use the #include<func.ptr.h> statement d. not dereference the pointer in the function's body e. None of these ANS: E

27. To help prevent memory leaks from occurring in C++11, a __________ automatically deletes a chunk of dynamically allocated memory when the memory is no longer being used. a. null pointer b. smart pointer c. dereferenced pointer d. None of these ANS: B

28. What will the following code output? int number = 22; int *var = &number; cout << *var << endl; a. the address of number b. 22 c. an asterisk followed by 22 d. an asterisk followed by the address of number ANS: B

29. What will the following code output? int number = 22; int *var = &number; cout << var << endl; a. the address of number b. 22 c. an asterisk followed by 22 d. an asterisk followed by the address of number


ANS: A

30. What will the following code output? int *numbers = new int[5]; for (int i = 0; i <= 4; i++) *(numbers + i) = i; cout << numbers[2] << endl; a. five memory addresses b. 0 c. 3 d. 2 e. 1 ANS: D

31. After the code shown executes, which of the following statements is true? int numbers[] = {0, 1, 2, 3, 4}; int *ptr = numbers; ptr++; a. ptr will hold the address of numbers[0] b. ptr will hold the address of the second byte within the element numbers[0] c. ptr will hold the address of numbers[1] d. this code will not compile ANS: C 32. Which of the following defines a unique_ptr named uniq that points to a dynamically allocated int? a. unique_ptr<uniq> int( new int ); b. unique_ptr<int> int( new uniq ); c. unique_ptr<uniq> uniq( new int ); d. unique_ptr<int> uniq( new int ); e. None of these ANS: D


MULTIPLE RESPONSE

1. Select all that apply. Which of the following can be used as pointers? a. array names b. numeric constants c. keywords d. None of these ANS: A

2. Select all that apply. Select as many of the following options that make this sentence true: The contents of pointer variables may be changed with mathematical statements that perform a. multiplication b. division c. addition d. subtraction e. modulus ANS: C, D

3. Select all that apply. Of the following, which statements have the same meaning? a. int *ptr = nullptr; b. int ptr = nullptr; c. *int ptr = nullptr; d. int* ptr = nullptr; e. int ptr* = nullptr; ANS: A, D

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 10 Characters, C-Strings, and More about the string Class

TRUE/FALSE


1. A test using the isupper function will return false if the argument is an uppercase character.

ANS: F 2. The isdigit function will return true if the argument is a digit between 0 and 9.

ANS: T 3. When using the strcat function you must be careful not to overwrite the bounds of an array.

ANS: T 4. The itoa function is similar to atoi but it works in reverse.

ANS: T

5. The C++ compiler performs strict array bounds checking when it encounters an array of characters.

ANS: F 6. The strlen function returns a C-string's length and adds one for \0.

ANS: F 7. The ftoa function converts a floating-point value to an ASCII value.

ANS: F

8. By being able to pass arrays as arguments, you can write your own functions for processing C-strings.

ANS: T

9. The C++ library provides functions for converting a string representation of a number to a numeric data type and vice-versa.


ANS: T 10. If an uppercase character is passed as an argument to the toupper function, the result will be an uppercase character.

ANS: T 11. You may use the <, >, <=, >=, ==, and != relational operators to compare string objects.

ANS: T 12. C++11 introduces a function named to_string that converts a numeric value to a string object.

ANS: T 13. The string class's front and back member functions were introduced in C++11.

ANS: T

14. Although C++ provides ample library functions to handle numeric values, we must write all of our own functions to manipulate character values.

ANS: F

15. The following string that uses a semicolon as a delimiter contains four tokens: "apple;cherry;lemon cream"

ANS: F

MULTIPLE CHOICE

1. To test whether a character is a numeric digit character, use the __________ function.


a. isnumber b. notAlpha c. isnumeric d. isdigit e. None of these ANS: D

2. To test whether a character is a printable character, use the __________ function. a. isprint b. isprintable c. isprintok d. isoktoprint e. None of these ANS: A

3. The escape sequence that represents the null terminator is a. \n b. \t c. \0 d. nullptr e. None of these ANS: C

4. The function that accepts a pointer to a C-string as an argument and returns the length of the C-string, not including the null terminator is a. numchar b. strlength c. strlen d. countstring e. None of these ANS: C


5. The null terminator stands for ASCII code a. 57 b. 100 c. 1000 d. 0 e. None of these ANS: D 6. The arguments of the strcpy function are a. two C-strings b. two addresses c. three pointers d. one array and one pointer e. None of these ANS: B

7. A library function that can find one C-string inside another is a. strcmp b. strstr c. strfind d. strsearch e. None of these ANS: B

8. The function that accepts a C-string as an argument and converts the string to a long integer is a. atol b. strlong c. strtolong d. stringlong e. None of these ANS: A


9. The function that converts a C-string to an integer and returns the integer value is a. atoint b. strtoint c. strint d. atoi e. None of these ANS: D 10. Which of the following converts the string "10" to the integer value 10? a. itoa("ten"); b. atoi("ten"); c. atoi("10"); d. itoa(10); e. None of these ANS: C 11. Which of the following will return true if the argument is a printable character other than a digit, letter, or space? a. isprint b. ispunct c. ischar d. isnotdls e. None of these ANS: B 12. The C-string company[12] can hold a. twelve characters b. thirteen characters c. eleven characters and the null terminator d. twelve characters and the null terminator e. None of these ANS: C


13. The expression being tested by the statement shown below will evaluate to true if var1 is __________. if (!isdigit(var1)) a. an alphabetic character b. 9 c. a symbol such as $ or & d. both A and C e. None of these ANS: D

14. The function that accepts a C-string containing a number as its argument and returns the integer equivalent is a. strToInt b. itoa c. atoi d. int_from e. None of these ANS: C

15. To determine whether a character entered is a letter of the alphabet, use the __________ function. a. isdigit b. fromkeyboard c. alpaok d. isalpha e. None of these ANS: D

16. To determine whether a character entered is whitespace, use the __________ function. a. iswhite b. isspace c. iswhitespace


d. isblank e. None of these ANS: B

17. The __________ function will change a character argument from lowercase to uppercase. a. isupper b. toupper c. tolarge d. fromlower e. None of these ANS: B

18. The __________ function will change a character argument from uppercase to lowercase. a. islower b. tolower c. atoi d. toupper e. None of these ANS: B

19. What is the output of the following statement? cout << tolower(toupper('Z')) << endl; a. uppercase Z b. lowercase z c. a lowercase z followed by an uppercase Z d. a compiler error e. None of these ANS: B

20. In C++ a C-string is a sequence of characters stored in consecutive memory, terminated by a a. period


b. space c. null character d. semicolon e. None of these ANS: C

21. The __________ function concatenates the contents of one C-string with another C-string. a. strcopy b. strappend c. strcat d. stradd e. None of these ANS: C 22. To use the strlen function in a program you must include a. #include<strlen> b. #include<iostring> c. #include<cstring> d. #include<stringlib> e. None of these ANS: C

23. The function that accepts pointers to two C-strings and an integer argument that indicates how many characters to copy from the second string to the first is a. strcpy b. strncpy c. copystring d. strintcpy e. None of these ANS: B

24. The function that reverses the order of a C-string is


a. reversstr b. strrev c. reverseit d. back e. None of these ANS: E 25. What is the value stored in num after the following statement executes? num = atoi("1000"); a. 1000 b. 999 c. "1000" d. "1 thousand" e. None of these ANS: A

26. Which of the following statements appropriately defines a C-string that stores names of up to 25 characters? a. char name[25]; b. string name[25]; c. char name[26]; d. string name[24]; e. None of these ANS: C

27. Which of the following defines an array of C-strings that will hold 49 characters and the null terminator? a. char [49]; b. char str[50]; c. char[50] str; d. character str[50]; e. None of these ANS: B


28. What is the result after the following statement executes? char var1 = tolower('A'); a. var1 stores the character value 'A' b. var1 stores the ASCII value for lowercase 'a' c. the character A is output to the monitor d. the character a is output to the monitor e. None of these ANS: B

29. Given the string shown below, if the delimiter is a space, how many tokens are in the string? "Tony's email address is tony_g@mycollege.edu" a. 4 b. 5 c. 6 d. 7 e. 9 ANS: B 30. In C++11, assuming mychar is a char variable and mystring is a string, what is the value of mychar after the following statement executes? mychar = mystring.front(); a. the ASCII value of the first character of mystring b. the first character of mystring c. nothing, the function is missing an argument d. this will cause a compiler error ANS: B

MULTIPLE RESPONSE


1. Select all that apply. Which of the following would be appropriate in a program that allows the user to enter either an uppercase or lowercase character in response to a prompt? a. tolower b. ignorecase c. toupper d. ignoreupper e. ignorelower ANS: A, C

2. Select all that apply. Whitespace encompasses which of the following? a. a tab b. a newline c. a space d. None of these ANS: A, B, C

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 11 Structured Data

TRUE/FALSE 1. A struct can contain members with varying data types.

ANS: T

2. Any mathematical operation that can be performed on regular C++ variables can be performed on structure members.

ANS: T

3. Structure variables may be passed as arguments to functions.

ANS: T


4. The structure pointer operator is used to dereference a pointer to a structure, not a pointer that is a member of a structure.

ANS: T

5. If a function is legally prototyped to return an integer value, it can return a structure member that is an integer data type.

ANS: T

6. A function cannot modify the members of a structure.

ANS: F 7. The expression s->m; indicates that s is a structure pointer and m is a structure member.

ANS: T 8. It is possible to output the contents of all members of a structure variable using a cout << statement followed by the name of the structure variable.

ANS: F

9. When a programmer creates an abstract data type, he or she can decide what values are acceptable for the data type, as well as what operations may be performed on the data type.

ANS: T

10. It is possible for a structure variable to be a member of another structure variable.

ANS: T


11. The expression *s->p; indicates that s is a structure pointer and p, which is also a pointer, is a member of the structure pointed to by s.

ANS: T

12. It is possible for a structure to contain, as a member, a pointer to its own structure type.

ANS: T 13. You cannot directly assign an integer value to an enum variable.

ANS: T 14. You cannot directly assign an enumerator to an int variable.

ANS: F

15. When you use a strongly typed enumerator in C++11 you must prefix the enumerator with the name of the enum followed by the :: operator.

ANS: T

16. The names of enumerators in an enumerated data type must be enclosed in quotation marks.

ANS: F

17. In C++11 if you want to retrieve a strongly typed enumerator's underlying integer value, you must use a cast operator.

ANS: T 18. Given the structure definition shown, assume that circle1 and circle2 are variables of the Circle type and their members have been initialized.


struct Circle { double centerX; double centerY; double radius; }; Then, is it true or false that the following statement correctly determines whether the two variables' members contain the same data? if (circle1 == circle2)

ANS: F

MULTIPLE CHOICE

1. Which of the following describes only the general characteristics of an object? a. initialization b. abstraction c. detailed specification d. initiation e. None of these ANS: B

2. Which of the following is required after the closing brace of the structure definition? a. square bracket b. period c. semicolon d. colon e. None of these ANS: C

3. Which of the following is an example of a C++ primitive data type? a. unsigned short int


b. long double c. unsigned char d. All of these e. None of these ANS: D

4. The following statement __________. bookList[2].publisher[3] = 't'; a. is illegal in C++ b. will change the name of the second book in bookList to 't' c. will store the character 't' in the fourth element of the publisher member of bookList[2] d. will result in a runtime error e. None of these ANS: C

5. When a structure is passed __________ to a function, its members are not copied. a. by reference b. by value c. Either of these d. Neither of these ANS: A

6. Passing a structure as a constant reference parameter to a function a. can potentially result in changes to the structure's members b. guarantees not to result in changes to the structure's members c. will always change the structure's members d. None of these ANS: B

7. You may use a pointer to a structure as a


a. function parameter b. structure member c. function return type d. All of these e. None of these ANS: D

8. Data types that are created by the programmer are known as a. variables b. abstract data types (ADTs) c. functions d. parameters e. None of these ANS: B

9. Before a structure can be used it must be a. declared b. dereferenced c. initialized d. All of these e. None of these ANS: A

10. The name of a structure is referred to as its a. data type b. argument c. parameter d. tag e. None of these ANS: E

11. A function __________ return a structure.


a. may b. may not c. will always d. can never e. None of these ANS: A

12. A structure pointer contains a. the address of a structure variable b. the dereferenced address of a structure tag c. the name and address of the structure tag d. the address of a structure tag e. None of these ANS: A 13. Which of the following assigns a value to the hourlyWage member of employee[2]? a. employee[2] -> hourlyWage = 50.00; b. employee2.hourlyWage = 7.50; c. hourlyWage[2].employee = 29.75; d. employee[2].hourlyWage = 75.00; e. None of these ANS: D 14. If Circle is a structure tag, then the following statement can be the header line for a function that __________. Circle doSomething(Circle c2) a. determines and returns the area of a circle b. takes a Circle structure as a parameter, does something, and returns a Circle structure c. operates on a constant reference to a Circle structure d. takes two Circle parameters and does something e. None of these ANS: B


15. Which of the following will allow you to access structure members? a. the structure access operator b. the dot operator c. the #include<structaccess> directive d. the getmember function e. None of these ANS: B

16. To dereference a structure pointer, the appropriate operator is a. the ampersand (&) b. an asterisk (*) c. the -> operator d. the <- operator (<-) e. None of these ANS: C 17. If a is a structure variable and p, a pointer, is a member of the structure, what will the following statement do? cout << *a.p; a. output the dereferenced value pointed to by p b. result in a compiler error c. output the address stored in p d. output the value stored in a e. None of these ANS: A

18. A structure __________ contain members of the same data type. a. cannot b. can c. shouldn't d. None of these


ANS: B 19. Which of the following statements outputs the value of the gpa member of element [1] of the student array? a. cout << student1.gpa; b. cout << firstStudent.gpa; c. cout << student[1].gpa; d. cout << student1->gpa; e. None of these ANS: C 20. If Circle is a structure, what does the following statement do? Circle *pcirc = nullptr; a. It declares an empty structure variable named *pcirc. b. It declares a structure pointer called pcirc initialized with a null pointer. c. The statement is illegal in C++. d. It initializes a null pointer with the value of the Circle pointer. e. None of these ANS: B

21. A good reason to pass a structure as a constant reference is a. to prevent changes to the structure's members b. to ensure changes to the structure's members c. to slow down the function's execution which helps prevent errors d. to speed up the function's modification of the structure's members e. None of these ANS: A

22. With an enumerated data type, the enumerators are stored in memory as a. strings b. integers


c. characters d. doubles ANS: B 23. In C++11, you can use a new type of enum known as a(n) __________ (also known as an enum class) to have multiple enumerators with the same name, within the same scope. a. universal enum b. auto enum c. multi-cast enum d. strongly typed enum e. None of these ANS: D

24. A declaration for an enumerated type begins with the __________ key word. a. enumerated b. enum_type c. enum d. ENUM e. None of these ANS: C 25. After the following statement executes, what value will the MAPLE enumerator be stored as, in memory? enum Tree { OAK, MAPLE, PINE }; a. "MAPLE" b. 2 c. 'M' d. 1 e. 1.0 ANS: D

26. Given the following decaration: enum Tree { OAK, MAPLE, PINE };


What is the value of the following relational expression? OAK > PINE a. true b. false c. This is an error. You cannot compare enumerators with relational operators. ANS: B 27. Given the following structure decaration, Employee is struct Employee { string name; int idNum; }; a. a member b. an array c. a tag d. None of these ANS: C 28. Given the following structure decaration, idNum is struct Employee { string name; int idNum; }; a. a member b. an array c. a tag d. None of these ANS: A

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 12 Advanced File Operations


TRUE/FALSE

1. When you store data in a variable, it is automatically saved in a file.

ANS: F

2. Only one file stream object can be declared per C++ program.

ANS: F 3. The ios::out flag causes the file's existing contents to be deleted if the file already exists.

ANS: T 4. When used by itself, the ios::app flag causes the file's existing contents to be deleted if the file already exists.

ANS: F 5. An alternative to using the open member function is to use the file stream object declaration itself to open the file. For example: fstream DataFile("names.dat", ios::in | ios::out);

ANS: T

6. File output may be formatted the same way as console screen output.

ANS: T

7. When data is read from a file, it is automatically stored in a variable.

ANS: F


8. By default, files are opened in binary mode.

ANS: F 9. If a file already exists, you can open it with the flags ios::in | ios::out to preserve its contents.

ANS: T 10. To write to a file, you use the file_write function.

ANS: F 11. The setprecision manipulator cannot be used to format data written to a file.

ANS: F 12. The ios::hardfail bit is set when an unrecoverable error occurs.

ANS: T

13. When passing a file stream object to a function, you should always pass it by reference.

ANS: T

MULTIPLE CHOICE

1. Data stored __________ disappears once the program stops running or the computer is powered down. a. on a CD b. in RAM c. on a flash drive d. on the disk drive e. None of these


ANS: B

2. In order, the three-step process of using a file in a C++ program involves a. insert a disk, open the file, remove the disk b. create the file contents, close the file, name the file c. open the file, read/write/save data, close the file d. name the file, open the file, delete the file e. None of these ANS: C

3. Which of the following is the data type that can be used to create files and write information to them but cannot be used to read information from them? a. ofstream b. ifstream c. afstream d. outstream e. None of these ANS: A

4. Which of the following is the data type that can be used to create files, read data from them, and write data to them? a. ofstream b. ifstream c. fstream d. stream e. None of these ANS: C

5. Which of the following statements opens a file and links it to a file stream object? a. open(aFile) = link(anObject); b. file.open("filename.txt"); c. linkstream("filename.txt");


d. link(open("filename.txt"); e. None of these ANS: B 6. Which of the following statements opens a file named info.txt for both input and output? a. dataFile.open("info.txt", ios::in && ios::out); b. dataFile.open("info.txt", ios::in, ios::out); c. dataFile.open("info.txt", input || output); d. dataFile.open("info.txt", ios::in | ios::out); e. None of these ANS: D

7. Which of the following is the state bit that can be tested to see if the end of an input stream is encountered? a. ios::eof b. ios::eofbit c. ios::failbit d. ios::badbit e. None of these ANS: B

8. Which of the following is the member function that reads a single character from a file? a. read b. get c. put d. input e. None of these ANS: B

9. Which of the following is the member function that can be used to store binary data to a file? a. binary.out b. write


c. put << d. dataout(binary) e. None of these ANS: B

10. Which of the following is the directive you must use to access files from a C++ program? a. #include <fileaccess> b. #include <filestream> c. #include <fstream> d. #include <iostream> e. None of these ANS: C

11. The __________ marker is the character that marks the end of a file and is automatically written when the file is closed. a. End Of File (EOF) b. No More Data (NMD) c. Data Stream Close (DSC) d. Data Read Stop (DRS) e. None of these ANS: A

12. What is true about the following statement? out.open("values.dat", ios::app); a. If the file already exists, its contents are preserved and all output is written to the end of the file. b. If the file already exists, it should be replaced with a new copy of values.dat. c. If the file already exists, it can be opened but not modified. d. None of these ANS: A

13. To set up a file to perform I/O you must declare


a. at least one variable, the contents of which will be written to the file b. one or more file stream objects c. a string object to store the file contents d. All of these e. None of these ANS: B 14. ofstream, ifstream, and fstream are a. header files b. libraries c. data types d. string arrays e. None of these ANS: C

15. Outside of a C++ program, a file is identified by its __________while inside a C++ program, a file is identified by a(n) __________. a. file number, file name b. file name, file number c. name, address d. name, file stream object e. None of these ANS: D

16. Which of the following data types can be used to create files and read information from them into memory? a. ofstream b. istream c. ifstream d. instream e. None of these ANS: C


17. Which statement opens a file in such a way that information will only be written to the end of the file? a. dataFile(open.append("info.dat"); b. dataFile.open("info.dat", ios::out | ios::app); c. datFile.open = "c:\\info.dat" ios::append; d. open(dataFile.append); e. None of these ANS: B

18. Which of the following access flags, when used by itself, causes a file's contents to be deleted if the file already exists? a. ios::app b. ios::in c. ios::out d. Any of these e. None of these ANS: C

19. Closing a file causes any unsaved information still held in the file buffer to be a. saved to the file b. deleted c. retained in the buffer for safekeeping d. duplicated e. None of these ANS: A

20. The end-of-file marker is automatically written a. when a file is opened with ios::eof b. when a file is opened with ios::app c. when a file is closed d. when the program ends e. None of these


ANS: C

21. When a file is opened, the file stream object's "read position" is a. at the end of the file b. at the beginning of the file c. nonexistent until the programmer declares it d. in the middle of the file e. None of these ANS: B

22. All stream objects have __________ which indicate the position of the stream. a. error state bits b. condition statements c. markers d. intrinsic error messages e. None of these ANS: A

23. Which of the following is the state bit that is set when an attempted operation fails? a. ios::failbit b. ios::badbit c. ios::hardfail d. ios::goodbit e. None of these ANS: A

24. Which of the following is the member function that writes a single character to a file? a. get b. write c. put d. insert e. None of these


ANS: C

25. Which of the following means accessing information from a file non-sequentially? a. linear access b. incremented acces c. random access d. protected access e. None of these ANS: C

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 13 Introduction to Classes

TRUE/FALSE

1. Whereas object-oriented programming centers on the object, procedural programming centers on functions.

ANS: T

2. Class objects can be defined prior to the class declaration.

ANS: F

3. The constructor function may not accept arguments.

ANS: F

4. A destructor function can have zero to many parameters.

ANS: F

5. More than one constructor function may be defined for a class.


ANS: T

6. More than one destructor function may be defined for a class.

ANS: F

7. You can use the technique known as a member intialization list to initialize members of a class.

ANS: T

8. In-place member initialization no longer is available in C++11.

ANS: F

9. In object-oriented programming, the object encapsulates both the data and the functions that operate on the data.

ANS: T

10. You must declare all data members of a class before you declare member functions.

ANS: F 11. You must use the private access specification for all data members of a class.

ANS: F

12. A private member function is useful for tasks that are internal to the class but it is not directly called by statements outside the class.

ANS: T

13. If you do not declare a destructor function, the compiler will furnish one automatically.


ANS: T

14. When an object is defined without an argument list for its constructor, the compiler automatically calls the object's default constructor.

ANS: T

15. Constructor functions are often used to allocate memory that will be needed by the object.

ANS: T

16. Destructor functions are often used to free memory that was allocated by the object.

ANS: T

17. While a class's member functions may be overloaded, the constructor cannot be overloaded.

ANS: F

18. When using smart pointers to dynamically allocate objects in C++ 11, it is unnecessary to delete the dynamically allocated objects because the smart pointer will automatically delete them.

ANS: T

MULTIPLE CHOICE

1. Objects are created from abstract data types that encapsulate __________ and __________ together. a. numbers, characters b. data, functions c. addresses, pointers d. integers, floating-point numbers e. None of these ANS: B


2. In OOP terminology, an object's member variables are often called its __________ and its member functions can be referred to as its behaviors or its __________. a. values, morals b. data, activities c. attributes, activities d. attributes, methods e. None of these ANS: D

3. A C++ class is similar to a(n) a. inline function b. header file c. library function d. structure e. None of these ANS: D

4. Where are class declarations usually stored? a. on separate disk volumes b. in their own header files c. in .cpp files, along with function definitions d. under pseudonyms e. None of these ANS: B

5. In a procedural program you typically have __________ stored in a collection of variables and a set of __________ that perform operations on the data. a. numbers, arguments b. parameters, arguments c. strings, operators d. data, functions


e. None of these ANS: D

6. A class is a(n) __________ that is defined by the programmer. a. data type b. function c. method d. attribute e. None of these ANS: A

7. Examples of access specifiers are the key words a. near and far b. opened and closed c. private and public d. table and row e. None of these ANS: C

8. Which of the following is used to protect important data? a. the public access specifier b. the private access specifier c. the protect member function d. the class protection operator, @ e. None of these ANS: B

9. Which of the following is a directive used to create an "include guard" that allows a program to be conditionally compiled to prevent a header file from accidentally being included more than once? a. #include b. #guard


c. #ifndef d. #endif e. None of these ANS: C

10. When the body of a member function is defined inside a class declaration, it is said to be a. static b. global c. inline d. conditional e. None of these ANS: C

11. A __________ is a member function that is automatically called when a class object is __________. a. constructor, created b. destructor, created c. static function, deallocated d. utility function, declared e. None of these ANS: A

12. The constructor function's return type is a. int b. float c. char d. structure pointer e. None of these ANS: E

13. The destructor function's return type is a. int b. float


c. char d. Nothing; destructors have no return type e. None of these ANS: D

14. When a constructor function accepts no arguments, or does not have to accept arguments because of default arguments, it is called a(n) a. empty constructor b. default constructor c. stand-alone function d. arbitrator function e. None of these ANS: B

15. When a constructor has a member initialization list, the initializations take place a. after any statements in the body of the constructor execute b. before any statements in the body of the constructor execute c. when a member is used in the execution of the program d. None of these ANS: B

16. In C++11 you can use __________ to initialize a member variable in its declaration statement. a. general member initialization b. default initialization c. in-place initialization d. initialization overload e. None of these ANS: C

17. The type of member function that may be called from a statement outside the class is a. public b. private


c. undeclared d. global e. None of these ANS: A

18. If you do not declare an access specification, the default for members of a class is a. inline b. private c. public d. global e. None of these ANS: B

19. Members of the class object are accessed with the a. dot operator b. cin object c. extraction operator d. stream insertion operator e. None of these ANS: A 20. Assuming that Rectangle is a class name, what can you say is true, given the following statement? Rectangle *BoxPtr; a. The statement declares an object of the class Rectangle. b. The statement assigns the value of *BoxPtr to the object Rectangle. c. The statement defines a Rectangle pointer variable named *BoxPtr. d. The statement is illegal in C++. e. None of these ANS: C

21. When you dereference an object pointer, use the


a. -> operator b. <> operator c. dot operator d. & operator e. None of these ANS: A

22. Which type of member function may only be called from a function that is a member of the same class? a. public b. private c. global d. local e. None of these ANS: B

23. The constructor function always has the same name as a. the first private data member b. the first public data member c. the class d. the first object of the class e. None of these ANS: C

24. Which of the following is automatically called when an object is destroyed? a. the constructor function b. the specification deallocator c. the destructor function d. the destruction function e. None of these ANS: C

25. How many default constructors can a class have?


a. only one b. two or more c. only two d. any number e. None of these ANS: A

26. Objects in an array are accessed with __________. a. subscripts b. parentheses c. #include statements d. output format manipulators e. None of these ANS: A

27. The process of object-oriented analysis can be viewed as the following steps: a. identify objects, then define each object's attributes, behaviors, and relationships b. define data members and member functions, then assign the class name c. declare public and private variables, prototype functions, and then write code d. write the main() function, then determine which classes are needed e. None of these ANS: A

28. When a member function is defined outside of the class declaration, the function name must be qualified with the a. class name, followed by a semicolon b. name of the first object c. class name, followed by the scope resolution operator d. private access specifier e. None of these ANS: C


29. If a local variable and a global variable have the same name within the same program, the __________ resolution operator must be used. a. variable b. ambiguity c. scope d. global e. None of these ANS: C

30. For the following code, which statement is not true? class Point { private: double y; double z; public: double x; }; a. x is available to code that is written outside the class. b. The name of the class is Point. c. x, y, and z are called members of the class. d. z is not available to code that is written outside the class. e. All of these are true. ANS: D

31. The following code shows an example of __________ class Point { private: double y = 5.70; double z = 3.0; public: Public member functions go here...


}; a. constructor delegation b. in-place initialization c. a default constructor creation d. an illegal initialization ANS: B

32. In C++11 you can have one constructor call another constructor in the same class by using a. constructor delegation b. in-place initialization c. a member initialization list d. None of these ANS: A 33. Assume that myCar is an instance of the Car class and that the Car class has a member function named accelerate. Which of the following is a valid call to the accelerate member function? a. Car -> accelerate(); b. myCar::accelerate(); c. myCar.accelerate(); d. myCar:accelerate(); e. None of these ANS: C

34. What is the output of the following program? #include <iostream> using namespace std; class TestClass { public: TestClass(int x) { cout << x << endl; } TestClass() { cout << "Hello!" << endl; }


}; int main() { TestClass test; return 0; } a. the program runs but there is no output. b. 0 c. Hello! d. the program will not compile ANS: C

35. What is the output of the following program? #include <iostream> using namespace std; class TestClass { public: TestClass(int x) { cout << x << endl; } TestClass() { cout << "Hello!" << endl; } }; int main() { TestClass test(77); return 0; } a. the program runs but there is no output. b. 77 c. Hello! d. the program will not compile ANS: B


36. What is the output of the following program? #include <iostream> using namespace std; class TestClass { private: int val; void showVal() { cout << val << endl; } public: TestClass(int x) { val = x; } }; int main() { TestClass test(77); test.showVal(); return 0; } a. the program runs but there is no output. b. 77 c. 0 d. the program will not compile ANS: D

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 14 More about Classes

TRUE/FALSE

1. When a class declares an entire class as its friend, the friendship status is reciprocal. That is, each class's member functions have free access to the other's private members.

ANS: F


2. When you overload an operator, you can change the operator's original meaning to something entirely different.

ANS: T

3. By default, when an object is assigned to another object, each member of one object is copied to its counterpart in the other object.

ANS: T 4. The overloaded = operator copies data from one object to another so it is known as the overload copy operator.

ANS: F 5. If you overload the prefix ++ operator, the postfix ++ operator is automatically overloaded.

ANS: F 6. C++ permits you to overload the sizeof operator and the this pointer.

ANS: F 7. When you overload the << operator you must also overload the >> operator.

ANS: F

8. A public data member may be declared a friend of a private function.

ANS: F

9. A static member variable can be used when there are no objects of the class in existence.


ANS: T

10. You can overload the conditional operator to make it function as an unconditional operator.

ANS: F

11. You may overload any C++ operator and you may use the operator function to define non-standard operations, such as @ or ^.

ANS: F 12. In C++ if you overload the < operator, you must also overload the > operator.

ANS: F

13. A static member function does not need to be called by a specific object of the class.

ANS: T

14. It is possible to declare an entire class as a friend of another class.

ANS: T

15. An aggregate class's constructor can use a member initialization list to call the constructors for each of its member objects.

ANS: T 16. The this pointer is a special built-in pointer that is automatically passed as a hidden argument to all non-static member functions.

ANS: T


17. A non-static member function may not access a static member variable.

ANS: F

18. A move operation transfers resources from a source object to a target object.

ANS: T

19. In C++11 an rvalue reference is a reference variable that can refer only to temporary objects.

ANS: T

MULTIPLE CHOICE

1. Each object of a class has its own copy of the class's a. member functions b. member variables c. constructor and destructor functions d. All of these e. None of these ANS: B

2. The __________ member variable may be accessed before any objects of the class have been created. a. private b. public c. inline d. static e. None of these ANS: D

3. C++ requires that a copy constructor's parameter be a(n) a. integer data type


b. floating-point data type c. pointer variable d. reference object e. None of these ANS: D

4. C++ allows you to redefine the way __________ work when used with class objects. a. compiler errors b. preprocessor directives c. standard operators d. undefined variables e. None of these ANS: C

5. When objects contain pointers, it is a good idea to create an explicit __________ function. a. destructor b. copy constructor c. static constructor d. inline constructor e. None of these ANS: B

6. A good reason to overload an operator is to enable it to a. outperform its C language counterparts b. work in its usual way, but with programmer-defined data types c. operate on more operands than in its standard definition d. operate on no operands e. None of these ANS: B

7. Which of the following operators may be used to assign one object to another? a. =


b. == c. <> d. @ e. None of these ANS: A

8. When a class contains an instance of another class, it is known as a. object overloading b. operator overloading c. object composition d. dynamic composition e. None of these ANS: C

9. If you do not furnish a(n) __________, an automatic memberwise copy will be performed when one object is assigned to another object. a. overloaded constructor function b. overloaded assignment operator c. default constructor function d. overloaded copy operator e. None of these ANS: B

10. It is a good idea to make a copy constructor's parameters __________ by specifying the __________ key word in the parameter list. a. inline, inline b. static, static c. constant, const d. global, global e. None of these ANS: C


11. A reason to overload the __________ is to allow you to write classes that have array-like behaviors. a. parentheses ( ) operator b. curly braces { } operator c. square brackets [ ] operator d. colon :: operator e. None of these ANS: C

12. If a member is declared __________, all objects of that class have access to that variable. a. static b. dynamic c. inline d. default e. None of these ANS: A

13. A member function that is declared __________ may not access any non-static data members in the class. a. private b. public c. static d. inline e. None of these ANS: C

14. Which type of function is not a member of a class but has access to the private members of the class? a. static b. constructor c. destructor d. friend e. None of these ANS: D


15. A(n) __________ is a special function that is called whenever a new object is created and initialized with another object's data. a. static function b. destructor c. copy constructor d. assignment function e. None of these ANS: C

16. If you do not furnish a __________, a default one will be provided by the compiler. a. constructor b. destructor c. copy constructor d. All of these e. None of these ANS: D

17. When you redefine the way a standard operator works when it is used with class objects, you have __________ the operator. a. reassigned b. reformatted c. overloaded d. referenced e. None of these ANS: C 18. To overload the + operator, you would write a function named a. overload + b. operator + c. function + d. operator.overload(+)


e. None of these ANS: B

19. A(n) __________ is a special built-in pointer that is available to a class's member functions.. a. this pointer b. &constructor pointer c. ~destructor *ptr d. overload operator, -> e. None of these ANS: A

20. An ___________ operator can work with programmer-defined data types. a. inline b. unconditional c. overloaded d. undefined e. None of these ANS: C

21. When you overload an operator, you cannot change the number of __________ taken by the operator. a. arguments b. parameters c. operations d. operands e. None of these ANS: D

22. A(n) __________ informs the compiler that a class will be declared later in the program. a. static function b. private data member c. forward declaration d. object conversion


e. None of these ANS: C 23. In the following function header, the word int is known as a(n) __________. FeetInches FeetInches::operator++(int) a. parameterless data type b. incomplete argument c. dummy parameter d. incomplete parameter e. None of these ANS: C

24. In C++11 values that persist beyond the statement that created them and have names that make them accessible to other statements in the program are called a. rvalues b. lvalues c. temporary values d. semantics e. None of these ANS: B

25. In C++11 reference variables that can refer only to temporary objects that would otherwise have no name are called __________ and are declared with a __________. a. rvalues, ampersand (&) b. lvalues, ampersand (&) c. lvalues, double ampersand (&&) d. rvalues, double ampersand (&&) e. None of these ANS: D

26. In C++11 the __________ operator swaps the members of the object being assigned with the temporary object.


a. move assignment b. swap assignment c. temp assignment d. semantic assignment e. None of these ANS: A

MULTIPLE RESPONSE

1. Select all that apply. Given the following code fragment, which of the things shown below happen when the statement on line 8 executes? 1

int square(int a)

2

{ return a * a;

3 4

}

5

int main()

6

{

7

int x = 0;

8

x = square(5);

9

cout << x << endl;

10

return 0;

11

}

a. The square function is called and the value 5 is passed as an argument. b. The square function calculates 5*5 and stores the result, 25, as a temporary value. c. The temporary value is copied (assigned) to the variable x. d. The temporary value is discarded by the system. e. None of these ANS: A, B, C, D

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 15 Inheritance, Polymorphism, and Virtual Functions


TRUE/FALSE

1. In an inheritance situation, you may not pass arguments to a base class constructor.

ANS: F

2. More than one class may be derived from a base class.

ANS: T

3. A derived class may become a base class if another class is derived from it.

ANS: T

4. The base class access specification can be viewed as a filter that base class members must pass through when becoming inherited members of a derived class.

ANS: T

5. When arguments must be passed to the base class constructor, they are passed from the derived class constructor's header line.

ANS: T

6. A member function of a derived class may not have the same name as a member function of a base class.

ANS: F

7. Static binding occurs when the compiler binds a function call with the function call that resides in the same class as the call itself.

ANS: T

8. Pointers to a base class may be assigned the address of a derived class object.


ANS: T

9. A derived class may not have any classes derived from it.

ANS: F 10. In C++11, if a derived class attempts to override a final member function, the compiler generates an error.

ANS: T

11. C++11 provides a way for a derived class to inherit some of the base class's constructors.

ANS: T

12. In C++11, using constructor inheritance, it is possible for any of the base class's constructors to be inherited.

ANS: F

MULTIPLE CHOICE

1. In OOP programming, __________ allows you to create new classes based on existing classes. a. polymorphism b. inheritance c. function overloading d. the copy constructor e. None of these ANS: B

2. When you derive a class from an existing class, you __________ add new data and functions. a. never


b. must c. may d. None of these ANS: C

3. The __________ members of a base class are never accessible to a derived class. a. private b. public c. protected d. All of these e. None of these ANS: A

4. The __________ constructor is called before the __________ constructor. a. base, derived b. derived, base c. public, private d. private, public e. None of these ANS: A

5. A __________ of a base class expects to be overridden in a derived class. a. constructor function b. destructor function c. static function d. virtual function e. None of these ANS: D

6. The term __________ means the ability to take many forms. a. inheritance b. polymorphism


c. member function d. encapsulation e. None of these ANS: B

7. When the compiler binds a member function call with the version of the function that resides in the same class as the call itself, it is considered a. local binding b. safe binding c. static binding d. dynamic binding e. None of these ANS: C

8. The compiler performs __________ on virtual functions. a. local binding b. additional error checking c. static binding d. dynamic binding e. None of these ANS: D

9. When more than one class is derived from a base class, the situation is called a. polymorphism b. multiplicity c. population d. encapsulation e. None of these ANS: E

10. When a derived class has two or more base classes, the situation is called a. multiple inheritance


b. multiplicity c. polymorphism d. encapsulation e. None of these ANS: A

11. Multiple inheritance opens the opportunity for a derived class to have ___________ members. a. dynamic b. private c. public d. ambiguous e. None of these ANS: D

12. Which is the base class in the following statement? class Car : public Vehicle a. Car b. Vehicle c. public d. class e. None of these ANS: B 13. The following statement allows the __________ members of the Car class to access __________ members of the Vehicle class. class Car : public Vehicle a. private, private b. public, private c. protected, private d. public, protected e. None of these ANS: E


14. Which is the derived class in the following statement? class Car : protected Vehicle a. Car b. Vehicle c. protected d. There is no way to tell. e. None of these ANS: A

15. What is being protected in the following statement? class Car : protected Vehicle a. derived class functions b. base class members c. derived class data d. future inherited classes e. None of these ANS: B

16. Arguments are passed to the base class by the __________ class __________ function. a. derived, constructor b. derived, destructor c. base, constructor d. base, destructor e. None of these ANS: E

17. Arguments are passed to the base class destructor by the __________ class __________ function. a. derived, constructor b. derived, destructor c. base, constructor d. base, destructor


e. None of these ANS: E

18. Protected members of a base class are like __________, but they may be accessed by derived classes. a. constructor functions b. static members c. private members d. public members e. None of these ANS: C

19. The __________ destructor is called before the __________ destructor. a. base, derived b. derived, base c. public, private d. private, public e. None of these ANS: B

20. Which of the following is commonly used to extend a class or to give it additional capabilities? a. inheritance b. privacy c. the constructor d. the destructor e. None of these ANS: A

21. When member functions behave differently depending on which object performed the call, this is an example of a. chaos theory b. virtual insubordination c. polymorphism


d. encapsulation e. None of these ANS: C

22. A virtual function is a function that expects to be __________ in a derived class. a. ignored b. called frequently c. overridden d. private e. None of these ANS: C

23. Multiple inheritance is when a __________ class has __________ base classes. a. base, no b. derived, two or more c. derived, no d. compound, more than two e. None of these ANS: B

24. A virtual function is declared by placing the __________ key word in front of the return type in the base class's function declaration. a. virtual b. private c. public d. protected e. None of these ANS: A

25. Polymorphism is when __________ in a class hierarchy perform differently, depending on which object performs the call. a. base class constructors


b. derived class constructors c. member functions d. derived class destructors e. None of these ANS: C

26. Functions that are dynamically bound by the compiler are __________ functions. a. constructor b. destructor c. static d. virtual e. None of these ANS: D

27. C++11 introduced the __________ key word to help prevent subtle errors when overriding virtual functions. a. const b. final c. override d. virtual e. None of these ANS: C

MULTIPLE RESPONSE

1. Select all that apply. The base class's __________ affects the way its members are inherited by the derived class. a. name b. return data type c. access specification d. construction e. None of these


ANS: C

2. Select all that apply. In an inheritance situation, the new class that you create from an existing class is known as the a. derived class b. inheritor c. child class d. parental class e. None of these ANS: A, C

3. Select all that apply. Which of the following constructors cannot be inherited through constructor inheritance? a. the default constructor b. the virtual constructor c. the move constructor d. the copy constructor e. the grand constructor ANS: A, C, D

4. Select all that apply. The base class access specification determines how __________ members in the base class may be accessed by derived classes. a. private b. public c. constructed d. protected e. None of these ANS: A, B, D

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 16 Exceptions and Templates

TRUE/FALSE


1. If an exception is not caught, it is stored for later use.

ANS: F

2. The try/catch/throw construct is able to handle only one type of exception in a try block.

ANS: F

3. There is no difference between declaring an object of an ordinary class and an object of a template class.

ANS: F

4. A program may not contain both a "regular" version of a function and a template version of the function.

ANS: F 5. The line containing a throw statement is known as the throw point.

ANS: T

6. Function templates allow you to write a single function definition that works with many data types.

ANS: T

7. Using a function template requires less code than overloading a function.

ANS: T

8. Exceptions are used to signal errors or unexpected results that occur when a program is running.

ANS: T


9. The try block of a try/catch construct is used to display the definition of an exception parameter.

ANS: F

10. If an exception is not caught, the program will abort execution.

ANS: T

11. A generic function that can work with any data type is called an exception template.

ANS: F

MULTIPLE CHOICE

1. Which of the following is used to signal errors or unexpected results that happen as a program runs? a. virtual functions b. destructors c. exceptions d. templates e. None of these ANS: C 2. The __________ starts with the key word try and is followed by a block of code that executes any statement that might cause an exception to be thrown.. a. try block b. try/catch class c. try function d. catch function e. None of these ANS: A

3. When an error occurs, an exception is


a. created b. thrown c. passed d. ignored e. None of these ANS: B

4. An exception thrown from outside a try block will a. be caught outside the catch block b. be caught inside the catch block c. remain inside the throw block d. cause the program to abort execution e. None of these ANS: D

5. In the following code, which statement is the throw point? double divide(int numer, int denom) { if (denom == 0) throw "ERROR: Cannot divide by zero.\n"; else return static_cast<double>(numer)/denom; } a. if (denom == 0) b. throw "ERROR: Cannot divide by zero.\n"; c. return static_cast<double>(numer)/denom; d. There is no throw point because there is no try block. e. None of these ANS: B 6. Given the following code, what will happen if the value of denom is 0? double divide(int numer, int denom) {


if (denom == 0) throw "ERROR: Cannot divide by zero.\n"; else return static_cast<double>(numer)/denom; } a. Nothing will happen because there is no catch block. b. The program will display ERROR: Cannot divide by zero.and then move to the statement following the if/else block. c. The program will display ERROR: Cannot divide by zero.and then halt. d. There is no throw point because there is no try block. e. None of these ANS: C

7. If an exception is thrown by a member function of a class object, the class __________ is called. a. constructor b. destructor c. catcher d. handler e. None of these ANS: B

8. In a function template, the programmer substitutes __________ for __________. a. parameters, data types b. parameters, arguments c. arguments, parameters d. data types, parentheses e. None of these ANS: A

9. A(n) __________ is used in a function template to specify a generic data type. a. dummy variable b. exception


c. catch block d. type parameter e. None of these ANS: D

10. The beginning of a function template is marked by a a. return type b. parameter list c. template prefix d. semicolon e. None of these ANS: C 11. What does the T represent in the following statement? template < class T > a. the name of the function template b. T stands for Template c. a generic data type that is used in a function template d. the int data type e. None of these ANS: C 12. What does the word class indicate in the following statement? template < class T > a. class is a key word that is used to precede the type parameter. b. It indicates that a class is being derived from an existing class named T. c. It indicates that a class named T is being derived from a class named template. d. A class named T will automatically be created by the compiler. e. None of these ANS: A


13. Class templates allow you to create one general version of a class without having to a. write any code b. use member functions c. use private members d. duplicate code to handle multiple data types e. None of these ANS: D

14. A(n) __________ is a value or an object that signals an error. a. destructor b. template c. throw d. exception e. None of these ANS: D

15. To handle an exception that has been thrown, a program must have a(n) a. throw function b. try/catch construct c. fatal error d. unrecoverable error e. None of these ANS: B

16. The try block is immediately followed by one or more a. errors b. error messages c. catch blocks d. throw blocks e. None of these ANS: C


17. Catch blocks serve as a. exception handlers b. receivers c. temporary variables d. permanent storage for trapped errors e. None of these ANS: A 18. When the __________ operator fails to allocate memory, C++ throws a bad_alloc exception. a. new b. alloc c. catch d. None of these ANS: A 19. If you want to catch a bad_alloc exception in a program, you should include a. #include <iostream> b. #include <alloc> c. #include <new> d. None of these ANS: C

20. A generic function that can work with any data type is a a. function argument b. function parameter c. function template d. member function e. None of these ANS: C

21. How much memory is reserved for a function template?


a. four bytes b. It depends on the size of the template. c. two bytes d. no memory e. None of these ANS: D

22. All type parameters defined in a function template must appear at least once in the a. function parameter list b. preprocessor directives c. function call d. type.h file e. None of these ANS: A

23. A function template's prefix contains __________ enclosed in angle brackets. a. one or more generic data types b. the function definition c. constant values d. the function's return type e. None of these ANS: A

24. An actual instance of the function is created in memory when the compiler encounters a. the template prefix b. a call to the template function c. a try block d. a catch block e. None of these ANS: B

25. A function template prefix is placed before the function header while a class template prefix is placed


a. following the public: access specification b. following the private: access specification c. before the class declaration d. before the class constructor function header e. None of these ANS: C

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 17 The Standard Template Library

TRUE/FALSE

1. The Standard Template Library (STL) contains templates for useful algorithms and data structures.

ANS: T

2. A sequence container organizes data in a sequential fashion, similar to an array.

ANS: T

3. When you declare an iterator to work with a container, the compiler automatically chooses the right type.

ANS: T

4. An iterator is a function that is used to access items in an array.

ANS: F

5. An associative container stores data in a nonsequential way so it is slower to locate elements than a sequence container.

ANS: F


6. Because the array class's [] operator will perform bounds checking, it is acceptable to use a subscript that is out of bounds.

ANS: F

7. A mutable iterator gives you read/write access to the element to which the iterator points.

ANS: T 8. A vector is an associative container that works like an array but is dynamic in size.

ANS: F

9. To improve runtime performance, C++11 introduced a new family of member functions that use the emplacement technique to insert new elements.

ANS: T

10. In a mapping, each element must have one of the following two parts: either a key or a value.

ANS: F

11. A set is an associative container containing elements that must all be unique.

ANS: T

12. The STL provides many algorithms which are implemented as function templates and are included in the <algorithm> header file.

ANS: T

13. A function object is an object that acts like a function and is also known as a functor.


ANS: T

14. You can use the name of a function to get that function's address in memory and, therefore, get a function pointer to point to the function's executable code.

ANS: T

MULTIPLE CHOICE

1. Two types of container classes in the STL are a. sequence and associative b. box and cylinder c. array and struct d. constant and literal e. None of these ANS: A

2. A(n) __________ is like a pointer. It is used to access the individual data elements in a container. a. element access operator b. subscript indicator c. global data finder d. iterator e. None of these ANS: D

3. The most important data structures in the STL are __________ a. arrays and structures b. lists and groups c. containers and iterators d. templates and prototypes e. None of these ANS: C


4. The three sequence container objects provided by the STL are a. set, multiset, and map b. vector, deque, and list c. map, list, and array d. multimap, map, and multilist e. None of these ANS: B

5. A(n) __________ is a class that stores data and organizes it in some fashion. a. iterator b. container c. template d. box e. None of these ANS: B

6. An associative container uses __________ to access elements rapidly. a. data b. functions c. keys d. complex sort algorithms e. None of these ANS: C 7. Which of the following defines an array object that holds three strings? a. array<int, 3> items = {blue, green, yellow} b. array<strings, 4> items = {'blue', 'green', 'yellow',} c. array<string, 4> items = {"blue", "green", "yellow"} d. array<4> string items {"blue", "green", "yellow"} e. None of these ANS: C


8. An iterator of the iterator type that gives you read/write access to the element to which the iterator points is known as a(n) a. immutable iterator b. mutable iterator c. auto iterator d. range iterator e. None of these ANS: B

9. A sequence container that is dynamic in size is a(n) a. array b. map c. vector d. multimap e. None of these ANS: C 10. Which of the following types of iterators is(are) not supported by the vector class? a. const_iterator b. reverse_iterator c. const_reverse_iterator d. All of these are supported by the vector class e. None of these are supported by the vector class ANS: D

11. A technique introduced by C++11 to improve runtime performance is known as a. insertion b. emplacement c. push d. multimove e. None of these


ANS: B 12. Because a vector container uses a dynamically allocated array to hold its elements, it is common for the vector class to allocate a. less memory than it needs b. more memory than it needs c. a separate memory location for copies of new elements d. None of these ANS: B 13. The vector class has a ________ member function which can be used to request an increase in the vector's capacity and a __________ member function to decrease a vector's capacity. a. size(), max_size() b. capacity(), reserve() c. max_size(), shrink() d. reserve(), shrink_to_fit() e. None of these ANS: D

14. The algorithms provided by the STL are implemented as __________ and perform various operations on elements of containers. a. virtual functions b. function templates c. global variables d. private data members e. None of these ANS: B 15. To use the map class, you must include a. #include<key> b. #include<key_value> c. #include<map>


d. #include<multimap> e. None of these ANS: C

16. When an element is stored in a map, it is stored as an object of the __________ type. a. map b. key c. pair d. key_value e. None of these ANS: C

17. To retrieve a value from a map, you call the __________ member function and pass the __________ that is associated with the desired value. a. at(), key b. at(), value c. key(), key d. pair(), key e. None of these ANS: A 18. The difference between the unordered_multimap class and the multimap class is that a. the unordered_multimap class uses the binary tree data structure for internal storage while the multimap class uses a hash table b. the multimap class uses the binary tree data structure for internal storage while the unordered_multimap class uses a hash table c. the keys in the unordered_multimap class are sorted while the keys in the multimap class are in no particular order d. None of these ANS: B 19. To use the binary_search() algorithm you need to include ___________


a. two arguments which are iterators b. two header files, algorithm.h and search.h c. three arguments which are two iterators and a value d. one argument which is a value e. None of these ANS: C

MULTIPLE RESPONSE 1. Select all that apply. Which of the following are member functions of the vector class that use emplacement? a. insert() b. push_back() c. emplace() d. emplace_back() e. None of these ANS: C, D

2. Select all that apply. Which of the following are types of iterators? a. input and output b. forward and bidirectional c. random-access d. None of these ANS: A, B, C

3. Select all that apply. Which of the following statement(s) is(are) not true? a. A map is an associative container. b. Each element in a map has two parts. c. Map elements are usually referred to as key-value pairs. d. The key value in a map is used to locate a value. e. The value element in a map is used to locate the key. ANS: E


4. Select all that apply. Which of the following statement(s) is(are) true about the set container? a. It is an associative container. b. All the elements in a set must be unique. c. A set container is virtually the same as a size container. d. The elements in a set are automatically sorted in ascending order. ANS: A, B, D

5. Select all that apply. An object of a class that overloads the function call operator is a(n) a. call operator b. function object c. functor d. overloader object ANS: B, C

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 18 Linked Lists

TRUE/FALSE

1. A linked list is called "linked" because each node in the series has a pointer that points to the next node in the list.

ANS: T

2. If there are no nodes in a linked list, you cannot append a node to the list.

ANS: F

3. When working with a linked list one of the basic operations you can perform is to destroy the list.

ANS: T


4. A new node must always be made the last node in the list.

ANS: F

5. A new node cannot become the first node in the list.

ANS: F

6. To delete an entire list, normally you must traverse the list, deleting each node, one by one.

ANS: T

7. When you create a linked list you must know in advance how many nodes the list will contain.

ANS: F

8. A linked list can consist of structs, objects, and other abstract data types.

ANS: T

9. A linked list can grow and shrink as a program runs.

ANS: T

10. Linked lists are less complex to code and manage than arrays.

ANS: F

11. Nodes in a linked list are stored in contiguous memory.

ANS: F

12. When you delete a node from a list, you must ensure that the links in the list are not permanently broken.


ANS: T 13. Deleting an entire list simply requires the use of the delete operator.

ANS: F

14. The Standard Template Library (STL) provides a linked list container.

ANS: T

15. A list that contains pointers to the previous node, the next node, and a node in the third dimension is known as a triple linked list.

ANS: F

MULTIPLE CHOICE

1. ADT stands for a. Algorithm Dependent Template b. Algorithm Driven Template c. Abstract Data Type d. Automatic Data Type e. None of these ANS: C

2. A linked list is a series of connected a. ADTs b. vectors c. algorithms d. nodes e. None of these


ANS: D

3. The __________ of a linked list points to the first node in the list. a. starter b. head c. tail d. declaration e. None of these ANS: B

4. To append a node to a list means to a. delete a node from the beginning of the list b. delete a node from the end of the list c. add a node to the beginning of the list d. add a node to the end of the list e. None of these ANS: D

5. The process of moving through a linked list is referred to as __________ the list. a. cruising b. traversing c. hopping d. revising e. None of these ANS: B 6. If the head pointer points to nullptr, this indicates a. the list has been previously created and then destroyed b. the list needs to be destroyed c. there are no nodes in the list d. the list is full and cannot accept any new nodes e. None of these


ANS: C

7. In an insertion or deletion routine: how many pointers are you required to create for use during the traversal process? a. two: one for the node under inspection and one for the previous node b. two: one for the node under inspection and one for the next node c. one: for the node being inserted or deleted d. three: one for the node under inspection, one for the next node, and one for the following node ANS: A

8. How many steps are involved in the process of deleting a node? a. one: delete the node from memory b. two: remove the node without breaking links, then delete it from memory c. three: create a blank node, remove the node being deleted, insert the blank node d. four: create a blank node, insert the blank node before the node being deleted, remove the node being deleted, delete the blank node e. None of these ANS: B

9. A doubly linked list keeps track of the next node in the list as well as a. itself b. the head node c. the tail node d. the previous node e. None of these ANS: D

10. The list container provided by the Standard Template Library is a template version of a a. singly linked list b. doubly linked list c. circular linked list


d. backward linked list e. None of these ANS: B

11. Appending a node means adding it to the end of a list, and __________ a node means putting a new node in the list, but not necessarily at the end. a. concatenating b. popping c. clamping d. inserting e. None of these ANS: D

12. In a circular linked list, the last node points to the a. head pointer b. tail pointer c. first node d. None of these ANS: C

13. If new data needs to be added to a linked list, the program simply __________ and inserts it into the series. a. allocates another node b. removes a node c. borrows a node from the compiler d. Either B or C e. None of these ANS: A 14. The advantage a linked list has over a vector is that a. a linked list can dynamically shrink or grow and a vector cannot b. a linked list is smaller than a vector


c. a node can be inserted or removed faster from a linked list than from a vector d. data removal and insertion are more accurate with a linked list than with a vector e. None of these ANS: C

15. To create a linked list you must first create a(n) a. header file b. function template c. exception d. struct e. None of these ANS: D

16. The last node in a linked list points to a. a null pointer b. the previous node c. the first node in the list d. nothing; the last node does not contain a pointer e. None of these ANS: A

17. A __________ is used to travel through a linked list and search for data. a. node b. pointer c. null pointer d. traversal operator e. None of these ANS: B

18. To build a list initally, you can use a(n) __________ routine. a. build b. append


c. constructor d. initialization e. None of these ANS: B

19. A linked list class must take care of removing the dynamically allocated nodes and this is done by a. the constructor function b. the destructor function c. overriding the removal function d. overloading the memory persistence operator e. None of these ANS: B

20. Which type of list does not contain a null pointer at the end of the list? a. backwards linked b. doubly linked c. circular linked d. null linked e. None of these ANS: C

21. To insert a new node in ascending order into a list, the list must be a. arranged in descending order b. randomly ordered c. empty d. arranged in ascending order e. None of these ANS: D

MULTIPLE RESPONSE

1. Select all that apply. Which of the following is a basic list operation?


a. appending a node b. traversing the list c. inserting a node d. deleting a node e. None of these ANS: A, B, C, D

2. Select all that apply. While traversing a list, a node pointer knows when it has reached the end of the list if a. it encounters the newline character b. it encounters a null pointer c. it finds itself back at the beginning of the list d. it encounters a sentinel such as 9999 ANS: B

3. Select all that apply. Variations of the linked list are a. doubly linked list b. circular linked list c. frontward linked list d. backward linked list ANS: A, B, D

4. Select all that apply. A __________ list contains pointers to the nodes before it and after it. a. multi linked b. circular linked c. singly linked d. doubly linked ANS: B, C, D

5. Select all that apply. Which of the following containers can add elements at their back without requiring traversal? a. the list container


b. the circular_list container c. the forward_list container d. the reversal_list container ANS: A, C

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 19 Stacks and Queues

TRUE/FALSE

1. The first item placed onto a stack is always the last item removed from the stack.

ANS: T

2. A dynamic stack starts as an empty linked list.

ANS: T 3. The pop function in the stack template of the STL does not retrieve the value from the top of the stack; it merely removes it.

ANS: T 4. The two most common queue operations are endeque and deque.

ANS: F

5. The STL provides containers for deque and queue.

ANS: T

6. A real-world example of the queue data structure can be seen in a stack of cafeteria trays where the last tray pushed onto the stack is the first tray removed.


ANS: F

7. In a static stack class, the constructor function can dynamically allocate memory for the stack array.

ANS: T

8. Stacks and queues can be implemented as arrays or linked lists.

ANS: T

9. The programmer must declare in advance the size of a dynamic stack or queue.

ANS: F

10. A stack that is implemented as a linked list is known as a deque.

ANS: F 11. The two most common queue operations are push and pop.

ANS: F

12. When the program knows the exact contents of a list and can access any element on demand, the data structure is known as a stacked deque.

ANS: F

13. For a compiler that is older than C++11 it is necessary to put spaces between the angled brackets that appear next to each other when defining a stack.

ANS: F


14. Computer systems use stacks. For example, when a function is called, they create local variables on a stack which are removed from the stack when the function terminates.

ANS: T

MULTIPLE CHOICE

1. A(n) __________ is an abstract data type that stores and retrieves items in a last-in-first-out manner. a. array b. queue c. stack d. vector e. None of these ANS: C

2. A dynamic stack has a __________ size and is implemented as a(n) __________. a. variable, linked list b. fixed, linked list c. variable, array d. fixed, array e. None of these ANS: A

3. The __________ operation allows an item to be stored on a stack. a. append b. add c. pop d. push e. None of these ANS: D

4. What does the following statement indicate?


stack< int, vector<int> > iStack; a. a new stack of integers, implemented as a vector b. a new stack of integers, implemented as a deque c. a new stack named vector, implemented as integers d. a new vector named stack, implemented with integers e. None of these ANS: A

5. Two primary queue operations are a. push and pop b. enqueue and dequeue c. insert and delete d. onqueue and offqueue e. None of these ANS: B

6. The two primary stack operations are a. push and pull b. push and pop c. insert and delete d. append and delete e. None of these ANS: B

7. The __________ operation allows an item to be removed from the stack. a. push b. pop c. delete d. remove e. None of these ANS: B


8. A stack queue can be implemented as a a. circular array b. stack c. dynamic linked list d. dynamic vector e. None of these ANS: A

9. Which of the following is a double-ended queue? a. two-headed stack b. two-tailed vector c. circular array d. deque e. None of these ANS: D

10. A __________ stack or queue is built around the linked list. a. dynamic b. static c. deque-based d. floating-point e. None of these ANS: A

11. A container that provides quick access to elements at the front and the back of the list is a a. stack b. queue c. deque d. All of these e. None of these ANS: C


12. A __________ queue is more intuitive and easier to understand than a __________ queue. a. static, dynamic b. dynamic, static c. deque-like, stack-like d. stack-like, deque-like e. None of these ANS: B

13. Stacks are useful data structures for algorithms that work __________ with the __________ saved element in the series. a. last, first b. first, last c. efficiently, first d. efficiently, last e. None of these ANS: B

14. Static stacks have a __________ size and are implemented as __________. a. fixed, linked lists b. variable, arrays c. fixed, arrays d. variable, linked lists e. None of these ANS: C 15. A dynamic stack may be implemented as a(n) __________ and can expand or shrink with each push or pop operation. a. array b. structure c. linked list d. Either A or B


e. None of these ANS: C

16. A queue is a data structure that stores and retrieves item in the __________ manner. a. last in, first out b. first in, last out c. first in, first out d. random e. None of these ANS: B

17. A __________ is processed in a manner similar to customers standing in a grocery store check-out line where the first customer in line is the first to be served. a. stack b. vector c. queue d. All of these e. None of these ANS: C

18. When an element is added to a queue, it is added to the rear. When an element is removed from the queue, it is removed from the a. rear b. middle c. front d. depends on how the programmer writes the code e. None of these ANS: C 19. In a dequeue operation, the element at the __________ of the queue is removed. a. middle b. front


c. declaration d. rear e. None of these ANS: B

20. A dynamic queue can be implemented as a a. dynamic linked list b. fixed-length array c. fixed-length circular array d. All of these e. None of these ANS: A

21. Queues that are implemented as arrays are called __________ queues. a. dynamic b. stack c. static d. deque e. None of these ANS: C

22. If data is transmitted faster than it can be processed, it can be held in a __________ for processing. a. stack b. queue c. static array d. static deque e. None of these ANS: B

23. A stack can be adapted to store __________ data types. a. all b. only built-in C++


c. only abstract d. deque-like e. None of these ANS: A 24. Given the following code, assume the myStack object is a stack that can hold integers and that value is an int variable. 1

myStack.push(0);

2

myStack.push(1);

3

myStack.push(2);

4

myStack.pop(value);

5

cout << value << endl;

Assume that the pop function, called on line 4, stores the number popped from the stack in the value variable. What will the statement on line 5 display? a. 0 b. 1 c. 2 d. None of these ANS: C 25. Given the following code, assume the myStack object is a stack that can hold integers and that value is an int variable. 1

myStack.push(0);

2

myStack.push(1);

3

myStack.push(2);

4

myStack.pop(value);

5

myStack.pop(value);

6

myStack.pop(value);

7

cout << value << endl;

Assume that the pop function, called on lines 4, 5, and 6, stores the number popped from the stack in the value variable. What will the statement on line 7 display?


a. 0 b. 1 c. 2 d. None of these ANS: A 26. Given the following code, assume the myStack object is a stack that can hold integers and that value is an int variable. 1

myStack.push(0);

2

myStack.push(1);

3

myStack.push(2);

4

myStack.pop(value);

5

myStack.push(3);

6

myStack.pop(value);

7

cout << value << endl;

Assume that the pop function, called on lines 4 and 6, stores the number popped from the stack in the value variable. What will the statement on line 7 display? a. 0 b. 1 c. 2 d. 3 e. None of these ANS: D 27. Given the following code, assume the myQueue object is a queue that can hold integers and that value is an int variable. 1

myQueue.enqueue(0);

2

myQueue.enqueue(1);

3

myQueue.enqueue(2);

4

myQueue.dequeue(value);

5

cout << value << endl;


Assume that the dequeue function, called on line 4, stores the number removed from the queue in the value variable. What will the statement on line 5 display? a. 0 b. 1 c. 2 d. None of these ANS: A 28. Given the following code, assume the myQueue object is a queue that can hold integers and that value is an int variable. 1

myQueue.enqueue(0);

2

myQueue.enqueue(1);

3

myQueue.enqueue(2);

4

myQueue.dequeue(value);

5

myQueue.dequeue(value);

6

myQueue.dequeue(value);

7

cout << value << endl;

Assume that the dequeue function, called on lines 4, 5, and 6, stores the number removed from the queue in the value variable. What will the statement on line 7 display? a. 0 b. 1 c. 2 d. None of these ANS: C 29. Given the following code, assume the myQueue object is a queue that can hold integers and that value is an int variable. 1

myQueue.enqueue(0);

2

myQueue.enqueue(1);

3

myQueue.enqueue(2);

4

myQueue.dequeue(value);

5

myQueue.enqueue(3);


6

myQueue.dequeue(value);

7

cout << value << endl;

Assume that the dequeue function, called on lines 4 and 6, stores the number removed from the queue in the value variable. What will the statement on line 7 display? a. 0 b. 1 c. 2 d. 3 e. None of these ANS: B

MULTIPLE RESPONSE

1. Select all that apply. The Standard Template Library offers a stack template that may be implemented as a(n) a. vector b. primary type c. linked list d. deque ANS: A, C, D

2. Select all that apply. The queue data structure is commonly applied in connection with a. managing the order of print jobs b. communications software c. operating systems d. None of these ANS: A, B, C

3. Select all that apply. Data structures that can dynamically store elements and can grow and shrink in size are a. stacks


b. arrays c. queues d. deques ANS: A, C, D

4. Select all that apply. A practical application of the stack data type in a computer system is a. storage of local variables b. tracking nested loops c. tracking nested function calls d. None of these ANS: A, B, C

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 20 Recursion

TRUE/FALSE

1. Like a loop, a recursive function must have some method to control the number of times it repeats.

ANS: T

2. When a recursive function directly calls itself, this is known as direct recursion.

ANS: T

3. A problem can be solved with recursion if it can be broken down into successive smaller problems that are the same as the overall problem.

ANS: T

4. To solve a problem recursively, you must identify at least one case in which the problem can be solved without recursion.


ANS: T

5. In a recursive solution, the base case is always the first case to be called.

ANS: F

6. Indirect recursion means that a function calls itself n number of times and then processing of the function starts from the first call.

ANS: F

7. A recursive function cannot call another function.

ANS: F

8. Recursive algorithms are less efficient than iterative algorithms.

ANS: T

9. Any algorithm that can be coded with recursion can also be coded with an iterative structure.

ANS: T

10. When recursion is used on a linked list, it will always display the contents of the list in reverse order.

ANS: F

11. The speed and amount of memory available to modern computers diminishes the performance impact of recursion so much that inefficiency is no longer a strong argument against it.

ANS: T

12. All mathematical problems are designed to be more efficient using recursive solutions.


ANS: F

MULTIPLE CHOICE

1. A __________ function is one that calls itself. a. dynamic b. static c. validation d. recursive e. None of these ANS: D

2. If a recursive algorithm does not contain a base case, it a. returns 0 and stops b. returns false and stops c. uses up all available stack memory, causing the program to crash d. reaches the recursive case and stops e. None of these ANS: C

3. The __________ algorithm uses recursion to sort a list. a. shell sort b. QuickSort c. binary sort d. red/black sort e. None of these ANS: B

4. The __________ of recursion is the number of times a recursive function calls itself. a. level b. breadth


c. type d. depth e. None of these ANS: D

5. The QuickSort algorithm works on the basis of a. three sublists b. two sublists and a pivot c. two pivots and a sublist d. three pivots e. None of these ANS: B

6. The programmer must ensure that a recursive function does not become a. a static function b. a virtual function c. an endless loop d. a dynamic function e. None of these ANS: C

7. When function A calls function B which, in turn, calls function A, this is known as a. direct recursion b. indirect recursion c. function swapping d. perfect recursion e. None of these ANS: B

8. A recursive function is designed to terminate when it reaches its a. return statement b. closing curly brace


c. last parameter d. base case e. None of these ANS: D

9. The QuickSort algorithm was developed in 1960 by a. Bjarne Stroustrup b. Tony Gaddis c. C.A.R. Hoare d. C.M. Turner e. None of these ANS: C

10. The QuickSort algorithm is used to sort a. lists stored in arrays or linear linked lists b. tree data structures c. randomly-ordered files d. All of these e. None of these ANS: A 11. The recursive factorial function calculates the factorial of its parameter. Its base case is when the parameter is a. returned b. received c. one d. zero e. None of these ANS: D 12. How many times will the following function call itself if 5 is passed as the argument? void showMessage(int n)


{ if (n > 0) { cout << "Good day!" << endl; showMessage(n - 1); } } a. 1 b. 4 c. 5 d. An infinite number of times ANS: C 13. How many times will the following function call itself if 5 is passed as the argument? void showMessage(int n) { if (n > 0) { cout << "Good day!" << endl; showMessage(n + 1); } } a. 1 b. 4 c. 5 d. An infinite number of times ANS: D

14. The following code is an example of a __________ recursive algorithm. int myRecursion(int array[], int first, int last, int val) { int num; if (first > last) return -1;


num = (first + last)/2; if (array[num] == val) return num; if (array[num] < val) return myRecursion(array, num + 1, last, val); else return myRecursion(array, first, num - 1, val); } a. Towers of Hanoi b. QuickSort c. binary search d. doubly linked list e. None of these ANS: C

MULTIPLE RESPONSE

1. Select all that apply. Which of the following problems can be solved using recursion? a. computing factorials b. finding the greatest common divisor of two numbers c. doing a Binary Search d. multiplying two numbers e. traversing a linked list ANS: A, B, C, E

Starting Out with C++ from Control Structures to Objects, 9e (Gaddis) Chapter 21 Binary Trees

TRUE/FALSE

1. The binary tree structure is called a "tree" because it resembles an upside-down tree.

ANS: T


2. The InOrder method of traversing a binary tree involves traversing the node's left subtree, processing the node's data, and then traversing the node's right subtree.

ANS: T

3. A subtree is an entire branch of a tree from one particular node down.

ANS: T

4. The height of a tree describes how many levels there are in the tree.

ANS: T

5. Binary trees are commonly used to organize key values that index database records.

ANS: T

6. Deleting a leaf node from a binary tree is not difficult but deleting a non-leaf node requires several steps.

ANS: T

7. In a binary tree, each node must have a minimum of two children.

ANS: F

8. To remove a node that has children, you must first remove the children.

ANS: F

9. The width of a tree is the largest number of nodes in the same level.

ANS: T


10. All nodes to the right of a node hold values greater than that node's value.

ANS: T 11. Output will be the same if you use InOrder, PostOrder, or PreOrder traversals of the same binary tree.

ANS: F 12. The PreOrder method of traversing a binary tree involves processing the node's data, traversing the node's left subtree, and then traversing the node's right subtree.

ANS: T 13. The PostOrder method of traversing a binary tree involves processing the node's data, traversing the node's right subtree, and then traversing the node's left subtree.

ANS: F 14. The intBinaryTree class has a public member function, findNode, that returns true if a value is not found and false if the value is found.

ANS: F

MULTIPLE CHOICE

1. In a binary tree, each node may point to __________ other node(s). a. no b. one c. two d. Any of these e. None of these


ANS: D

2. A good reason to use the binary tree structure is a. to expedite the process of searching large sets of information b. aesthetics and program design c. code readability d. that it is more flexible than the unary tree structure e. None of these ANS: A

3. The first node in a binary tree list is called the a. head pointer b. binary node c. root node d. pointer node e. None of these ANS: C

4. A node that has no children is known as a a. root node b. head node c. leaf node d. pure binary node e. None of these ANS: C

5. When a binary tree is used to facilitate a search, it is referred to as a a. binary queue b. binary ordered deque c. sort algorithm d. binary search tree e. None of these


ANS: D

6. When an application begins by searching a binary tree, it starts at a. the outermost leaf node b. the middle node, halfway between the root and the longest branch c. the rightmost child of the root node d. the root node e. None of these ANS: D

7. The __________ in a binary tree is similar to the head pointer in a linked list. a. root pointer b. leaf pointer c. null pointer d. binary pointer e. None of these ANS: A

8. The process of stepping through the nodes of a binary tree is known as a. climbing b. traversing c. stepping through d. branching out e. None of these ANS: B

9. When working with a binary tree, a node that has more than two children a. will be cut back by the compiler b. is theoretically impossible in a correctly developed binary tree structure c. is known as a triplet node d. None of these ANS: B


10. Values are typically stored in a binary search tree so that a node's __________ child holds data that is less than the __________ data. a. right, node's b. left, node's c. right, left child's d. left, right child's e. None of these ANS: B

11. Deleting a node that has two children offers an opportunity to use a. a function that returns a pointer to a pointer b. a function parameter that is a pointer to a pointer c. double indirection d. All of these e. None of these ANS: D

12. When you dereference a pointer to a pointer, the result is a. a value of the data type that is pointed to b. another pointer c. not possible to determine d. a null pointer e. None of these ANS: B

13. In a non-linear linked list, a node can point to a. only the next node in sequence b. only the previous node in sequence c. more than one other node, plus the previous node in sequence d. all of the other nodes in the list e. None of these


ANS: C

14. When the root node points to two other nodes, the nodes are referred to as a. child nodes, or children b. parent nodes, or parents c. binary nodes d. subnodes e. None of these ANS: A

15. All node pointers that do not point to other nodes are set to a. the root of the tree b. a parent node c. their leftmost child node d. a null pointer e. None of these ANS: D

16. Binary trees can be divided into a. branches b. leaves c. subtrees d. sawdust e. None of these ANS: C

17. A binary tree can be created using a struct or class containing a data value and a. a pointer to the first child node b. a pointer to the last child node c. two pointers, one for the left child and one for the right child d. two data nodes e. None of these


ANS: C

18. In a binary tree class you usually have a pointer as a member that is set to the a. leftmost child node b. first leaf node c. root of the tree d. deepest leaf node e. None of these ANS: C

19. The shape of a binary tree is a. always triangular b. always balanced c. determined by the programmer d. determined by the order in which values are inserted e. None of these ANS: D

20. A binary tree with a height of three has a. six nodes b. one root and three nodes with two children each c. three levels d. three subtrees e. None of these ANS: C 21. The InOrder, PreOrder, and PostOrder traversals can be accomplished using a. recursion b. no pointers c. no arguments d. no parameters e. None of these


ANS: A

MULTIPLE RESPONSE

1. Select all that apply. Binary trees may be implemented as templates, but any data types used with them must support the __________ operator. a. > b. < c. && d. == e. linked ANS: A, B, D

2. Select all that apply. The head pointer, anchored at the top of a binary tree, may be called the a. root node b. tree pointer c. binary pointer d. leaf pointer e. node pointer ANS: B

3. Select all that apply. Which of the following operations can be performed on a binary search tree? a. moving b. inserting c. finding d. placing e. deleting ANS: B, C, E

4. Select all that apply. Which of the following are methods of traversing a binary tree? a. LeftOrder traversal b. PreOrder traversal


c. FrontOrder traversal d. InOrder traversal e. PostOrder traversal ANS: B, D, E


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.