Test Bank for Problem Solving with C++: The Object of Programming, 10/e Chapter 8 Strings and Vectors
TRUE/FALSE
1. The following code declares a vector of characters. vector characters
2. The following code declares a vector of integers named numbers that reserves space for 100 integers. vector
3. Vectors can have any type as the base type ANSWER: TRUE
4. Vectors and arrays are the same data type. ANSWER: FALSE
5. Using the == operator on a string variable results in the same value as using strcmp on two c-strings. ANSWER: FALSE
6. Using the [i] on a string variable does not check for illegal values of i. ANSWER: TRUE
7. A string variable and a c-string are the same data type. ANSWER: FALSE
8. The function used to 'put two c-strings together into one\ANSWER: strcat
9. The following declares a c-string and initializes it to \char str[]=\ANSWER: TRUE
10. The following declares a c-string variable that will hold 10 letters. char str[10];
ANSWER: FALSE
11. Vector assignment is well behaved. ANSWER: TRUE
12. If v is a vector and i is an int variable, then in the following the value of i can be any nonnegative int value: v[i] = i; ANSWER: FALSE
13. If we use an out of range index with a vector, there will be an error message from the compiler. ANSWER: FALSE. 14. Using the resize member function alone, you can increase the capacity of an STL vector.
ANSWER: TRUE
15. If vector v has fewer than 24 elements and you call v.resize(24) the newly allocated elements are not initialized. ANSWER: FALSE.
16. You can explicitly use the vector member function resize to increase the capacity of a vector.
Test Bank for Problem Solving with C++: The Object of Programming, 10/e Chapter 8 Strings and Vectors
ANSWER: TRUE
17. A vector v will automatically increase the allocated size when more than v.size( ) elements are inserted with v.push_back( newElement). ANSWER: TRUE.
18. Vector indexing warns about out-of-bounds index values. ANSWER: FALSE.
Short Answer
1. The character '\\0' is called the __________ character. ANSWER: NULL
2. All c-strings must be terminated by ________ ANSWER: the null character
3. To compare two c-strings you use the __________ function. ANSWER: strcmp or strncmp
4. To use the functions for manipulating and comparing c-strings, you must include ___________
ANSWER:
5. What is the c-string function to determine the number of characters in a c-string? ANSWER: strlen
6. What is the difference between strcat and strncat?
ANSWER: strncat will concatenate at most n letters (where n has an appropriate value).
7. How do you call the function to read a whole line of input(up to 80 characters) from the keyboard into a c-string named str? ANSWER: cin.getline(str,80);
8. What is the name of the function to convert from a c-string that contains only digits to an integer? ANSWER: atoi
9. The c-string to number conversion functions are in the _________ library. ANSWER: cstdlib
10. To use the string class, you must include which library? ANSWER:
11. The ________ class lets you treat string values and variables like other pre-defined data types (such as int). ANSWER: string
12. How do you concatenate two string values (str1, str2)? ANSWER: str1 = str1 + str2; or str1 += str2;
13. What is the code to print out the third character in a string variable named str? ANSWER: cout << str[2];
14. Which string function returns the first occurrence of str1 in a string named str? ANSWER: find
15. ____________ can be thought of as an array that can grow and shrink as needed. ANSWER: Vectors
Test Bank for Problem Solving with C++: The Object of Programming, 10/e Chapter 8 Strings and Vectors
16. The declaration of an STL vector doubleVec that can hold values of type double, one writes ______________.
ANSWER: vector
17. To change the space already allocated for a vector, one uses the ______________ member function. ANSWER: reserve
18. To change the size of a vector, one uses the ______________ member function. ANSWER: resize
19. To convert a string object that stores an integer to a variable of type int one can use the C++11 function ____________. ANSWER: stoi
20. To convert an integer to a variable of type string one can use the C++11 function _____________. ANSWER: to_string
Multiple Choice
1. A character array terminated with the null character is most correctly called
a. a c-string
b. a character array c. a string
d. none of the above ANSWER: A
2. Which of the following declarations correctly creates a c-string that can hold the value \
a. char s1; b. char s1[9]; c. char s1=10; d. char s1[10]; ANSWER: D
3. To declare a c-string and initialize it to the value of \
a. char s1=phonebook;
b. char s1[10]=\c. c-string phonebook; d. char s1[10]=phonebook; ANSWER: B
4. Which of the following will print out the value in str? char str[30]; cin >> str;
a. cout << str;
b. for(int i=0;i<30;i++)
cout << str[i]; c. int i=0;
while(i<30 && str[i] != '\\0') cout << str[i]; d. All of the above