Deck 8: Strings and Vectors

Full screen (f)
exit full mode
Question
All c-strings must be terminated by ________
Use Space or
up arrow
down arrow
to flip the card.
Question
Vectors and arrays are the same data type.
Question
The following code declares a vector of characters.
vector characters
Question
Vector indexing warns about out-of-bounds index values.
Question
A vector v will automatically increase the allocated size when more than v.size ) elements are inserted with v.push_back newElement).
Question
The following code declares a vector of integers named numbers that reserves space for 100 integers.
vector numbers100);
Question
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;
Question
You can explicitly use the vector member function resize to increase the capacity of a vector.
Question
The function used to 'put two c-strings together into one" is called
Question
A string variable and a c-string are the same data type.
Question
Using the == operator on a string variable results in the same value as using strcmp on two c-strings.
Question
The following declares a c-string and initializes it to "speaker"
char str[]="speaker";
Question
If we use an out of range index with a vector, there will be an error message from the compiler.
Question
Using the [i] on a string variable does not check for illegal values of i.
Question
Using the resize member function alone, you can increase the capacity of an STL vector.
Question
The character '\0' is called the __________ character.
Question
Vector assignment is well behaved.
Question
If vector v has fewer than 24 elements and you call v.resize24) the newly allocated elements are not initialized.
Question
The following declares a c-string variable that will hold 10 letters.
char str[10];
Question
Vectors can have any type as the base type
Question
To use the string class, you must include which library?
Question
What is the name of the function to convert from a c-string that contains only digits to an integer?
Question
Which string function returns the first occurrence of str1 in a string named str?
Question
How do you call the function to read a whole line of inputup to 80 characters) from the keyboard into a c-string named str?
Question
____________ can be thought of as an array that can grow and shrink as needed.
Question
What is the code to print out the third character in a string variable named str?
Question
What is the c-string function to determine the number of characters in a c-string?
Question
What is the difference between strcat and strncat?
Question
Which of the following declarations correctly creates a c-string that can hold the value "phonebook"

A) char s1;
B) char s1[9];
C) char s1=10;
D) char s1[10];
Question
How do you concatenate two string values str1, str2)?
Question
The ________ class lets you treat string values and variables like other pre-defined data types such as int).
Question
To compare two c-strings you use the __________ function.
Question
The c-string to number conversion functions are in the _________ library.
Question
The declaration of an STL vector doubleVec that can hold values of type double, one writes ______________.
Question
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
Question
To change the space already allocated for a vector, one uses the ______________ member function.
Question
To convert an integer to a variable of type string one can use the C++11 function _____________.
Question
To use the functions for manipulating and comparing c-strings, you must include ___________
Question
To convert a string object that stores an integer to a variable of type int one can use the C++11 function ____________.
Question
To change the size of a vector, one uses the ______________ member function.
Question
What is wrong with the following attempted c-string declaration and initialization?
Char str1[5]={'a', 'b', 'c'};

A) There are only 3 values in the braces
B) The single quotes should be double quotes
C) The values do not constitute a c-string
D) nothing
Question
The notation vector means that the vector is

A) an array
B) a template class
C) primitive data type
D) all of the above
Question
What is the value of str after the following code?
String str;

A) a garbage string
B) the empty string
C) the null character
D) unknown
Question
strcmpfirst, second) returns

A) <0 if first < second, 0 if first == second, positive otherwise
B) true if first=second, false otherwise
C) nothing, it's a void function
D) >0 if first < second, 0 if first > second, <0 otherwise
Question
What is the value of numbers.size) after the following code?
Vector numbers;

A) 0
B) 10
C) 100
D) unknown
Question
Which is the proper way to determine how many characters are in the string variable named str?

A) str.getLength)
B) str.length)
C) lengthstr)
D) getLengthstr)
Question
What is the proper way to declare a vector of strings named names?

A) vector strings names;
B) vector string;
C) vector names;
D) all of the above
Question
Which assignment statements will copy the value " toaster" into a string variable str1)?

A) strcpystr1,"toaster");
B) str1 = "toaster";
C) str1 = toaster;
D) str1 += toaster;
Question
What is wrong with the following code fragment?
Char str1[10]="Mark", str2[15]="What's my name";
Strcpystr1,str2);

A) Nothing
B) str2 has white space in it
C) str1 does not have enough room
D) str2 does not have enough room
Question
If the name of a file to open is in the string variable name fileName, which of the following will correctly open the file for output?

A) out_file.openfileName);
B) out_file.open"fileName");
C) fileName.openoutfile);
D) out_file.openfileName.c_str));
Question
How can you assign the value "toaster" to a c-string name str of size 10?

A) str="toaster;
B) str=toaster;
C) strcpystr,"toaster");
D) str.strcpy"toaster");
Question
If you want to read into a c-string, you must ensure that the user does not enter more characters than

A) The size of the c-string
B) The size of the c-string + 1
C) The size of the c-string -1
D) It doesn't matter.
Question
Which of the following would correctly read an entire line from an input file stream named fin into a string variable named line.

A) getlinefin, line);
B) fin.getlineline);
C) fin.getlineline,'\n\');
D) fin.getlineline,80);
Question
To add an element to a vector of integers named numbers at the next available position in the vector, you would use:

A) numbers[numbers.size)+1] = newValue;
B) numbers = newValue;
C) numbers.pushBacknewValue);
D) numbers.push_backnewValue);
Question
Which of the following returns the fourth character in the string variable named str and checks if there is a fourth character in the string?

A) str3);
B) str.at3);
C) str[3];
D) All of the above
Question
To declare a c-string and initialize it to the value of "phonebook",

A) char s1=phonebook;
B) char s1[10]="phonebook";
C) c-string phonebook;
D) char s1[10]=phonebook;
Question
The base type for a vector can be

A) int
B) float or double
C) char
D) any data type
Question
When the extraction operator is used to read data into a string,

A) it skips all white spaces
B) it skips only new lines
C) it reads everything on the line
D) it reads as many characters that will fit into the c-string
Question
What is the difference between strcmp and strncmp?

A) No difference
B) they both compare, one expects an integer for the number of characters to compare.
C) one copies, the other compares
D) They are in different libraries
Question
Given the following declarations, which of the following is legal syntax?
String str="Your name";
Char c_string[20]="My name";

A) str = c_string;
B) c_string = str;
C) strcpyc_string, str.c_str));
D) strcpyc_string, str);
E) A and C
Question
What is the value of numbers.size) after the following code?
Vector numbers;
Numbers.reserve100)

A) 0
B) 10
C) 100
D) unknown
Question
Given the following code, what is the correct statement to insert the string str2 into str1, directly after the 'd'?
String str1="abcdefg";
String str2="ABCDE";

A) str1.insert4,str2);
B) str2.insert4,str1);
C) insertstr1,4)=str2;
D) insertstr2,4)=str1;
Question
What is the value of numbers.size) after the following code?
Vector numbers100);

A) 0
B) 10
C) 100
D) unknown
Question
In a vector, which of the following statements is true?

A) Indexing vector access is range checked.
B) The range of legal index values for a vector is 0 to the value of v.size)-1
C) To add a value use the member function v.push_front )
D) To increase or decrease a vector's size v.new_sizenewSize);
Question
If the capacity of a vector named names is 20 and the size of names is 19, which of the following statements are legal?

A) names.push_back"myName");
B) names[18]="myNmae";
C) all of the above
D) none of the above
Question
If a vector named numbers has 20 elements in it, what is the result of executing the following statement?
Numbers.resize10);

A) no change
B) the first 10 elements are removed
C) the last 10 elements are removed
D) this causes a run-time error
Question
Which of the following are correct statements about vector member functions studied in this chapter?

A) push_front baseTypeObject) puts object on the front of a vector
B) indexing uses index values 0 through size )
C) size ) tells how many base type objects have been inserted into the vector
D) When a vector runs out of space all implementations are required to double the amount of allocated space .
Question
What is the value of numbers.capacity) after the following code?
Vector numbers;
Numbers.reserve100)

A) 0
B) 10
C) 100
D) unknown
Question
When a vector is assigned to another vector

A) only the location of the vector is copied
B) all the values in the vector are copied
C) if there is not enough room in the left-hand vector, then not all the values from the right side are copied
D) none of the above
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/69
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 8: Strings and Vectors
1
All c-strings must be terminated by ________
the null character
2
Vectors and arrays are the same data type.
False
3
The following code declares a vector of characters.
vector characters
False
4
Vector indexing warns about out-of-bounds index values.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
5
A vector v will automatically increase the allocated size when more than v.size ) elements are inserted with v.push_back newElement).
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
6
The following code declares a vector of integers named numbers that reserves space for 100 integers.
vector numbers100);
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
7
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;
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
8
You can explicitly use the vector member function resize to increase the capacity of a vector.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
9
The function used to 'put two c-strings together into one" is called
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
10
A string variable and a c-string are the same data type.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
11
Using the == operator on a string variable results in the same value as using strcmp on two c-strings.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
12
The following declares a c-string and initializes it to "speaker"
char str[]="speaker";
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
13
If we use an out of range index with a vector, there will be an error message from the compiler.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
14
Using the [i] on a string variable does not check for illegal values of i.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
15
Using the resize member function alone, you can increase the capacity of an STL vector.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
16
The character '\0' is called the __________ character.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
17
Vector assignment is well behaved.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
18
If vector v has fewer than 24 elements and you call v.resize24) the newly allocated elements are not initialized.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
19
The following declares a c-string variable that will hold 10 letters.
char str[10];
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
20
Vectors can have any type as the base type
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
21
To use the string class, you must include which library?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
22
What is the name of the function to convert from a c-string that contains only digits to an integer?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
23
Which string function returns the first occurrence of str1 in a string named str?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
24
How do you call the function to read a whole line of inputup to 80 characters) from the keyboard into a c-string named str?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
25
____________ can be thought of as an array that can grow and shrink as needed.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
26
What is the code to print out the third character in a string variable named str?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
27
What is the c-string function to determine the number of characters in a c-string?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
28
What is the difference between strcat and strncat?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
29
Which of the following declarations correctly creates a c-string that can hold the value "phonebook"

A) char s1;
B) char s1[9];
C) char s1=10;
D) char s1[10];
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
30
How do you concatenate two string values str1, str2)?
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
31
The ________ class lets you treat string values and variables like other pre-defined data types such as int).
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
32
To compare two c-strings you use the __________ function.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
33
The c-string to number conversion functions are in the _________ library.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
34
The declaration of an STL vector doubleVec that can hold values of type double, one writes ______________.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
35
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
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
36
To change the space already allocated for a vector, one uses the ______________ member function.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
37
To convert an integer to a variable of type string one can use the C++11 function _____________.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
38
To use the functions for manipulating and comparing c-strings, you must include ___________
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
39
To convert a string object that stores an integer to a variable of type int one can use the C++11 function ____________.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
40
To change the size of a vector, one uses the ______________ member function.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
41
What is wrong with the following attempted c-string declaration and initialization?
Char str1[5]={'a', 'b', 'c'};

A) There are only 3 values in the braces
B) The single quotes should be double quotes
C) The values do not constitute a c-string
D) nothing
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
42
The notation vector means that the vector is

A) an array
B) a template class
C) primitive data type
D) all of the above
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
43
What is the value of str after the following code?
String str;

A) a garbage string
B) the empty string
C) the null character
D) unknown
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
44
strcmpfirst, second) returns

A) <0 if first < second, 0 if first == second, positive otherwise
B) true if first=second, false otherwise
C) nothing, it's a void function
D) >0 if first < second, 0 if first > second, <0 otherwise
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
45
What is the value of numbers.size) after the following code?
Vector numbers;

A) 0
B) 10
C) 100
D) unknown
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
46
Which is the proper way to determine how many characters are in the string variable named str?

A) str.getLength)
B) str.length)
C) lengthstr)
D) getLengthstr)
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
47
What is the proper way to declare a vector of strings named names?

A) vector strings names;
B) vector string;
C) vector names;
D) all of the above
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
48
Which assignment statements will copy the value " toaster" into a string variable str1)?

A) strcpystr1,"toaster");
B) str1 = "toaster";
C) str1 = toaster;
D) str1 += toaster;
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
49
What is wrong with the following code fragment?
Char str1[10]="Mark", str2[15]="What's my name";
Strcpystr1,str2);

A) Nothing
B) str2 has white space in it
C) str1 does not have enough room
D) str2 does not have enough room
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
50
If the name of a file to open is in the string variable name fileName, which of the following will correctly open the file for output?

A) out_file.openfileName);
B) out_file.open"fileName");
C) fileName.openoutfile);
D) out_file.openfileName.c_str));
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
51
How can you assign the value "toaster" to a c-string name str of size 10?

A) str="toaster;
B) str=toaster;
C) strcpystr,"toaster");
D) str.strcpy"toaster");
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
52
If you want to read into a c-string, you must ensure that the user does not enter more characters than

A) The size of the c-string
B) The size of the c-string + 1
C) The size of the c-string -1
D) It doesn't matter.
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
53
Which of the following would correctly read an entire line from an input file stream named fin into a string variable named line.

A) getlinefin, line);
B) fin.getlineline);
C) fin.getlineline,'\n\');
D) fin.getlineline,80);
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
54
To add an element to a vector of integers named numbers at the next available position in the vector, you would use:

A) numbers[numbers.size)+1] = newValue;
B) numbers = newValue;
C) numbers.pushBacknewValue);
D) numbers.push_backnewValue);
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
55
Which of the following returns the fourth character in the string variable named str and checks if there is a fourth character in the string?

A) str3);
B) str.at3);
C) str[3];
D) All of the above
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
56
To declare a c-string and initialize it to the value of "phonebook",

A) char s1=phonebook;
B) char s1[10]="phonebook";
C) c-string phonebook;
D) char s1[10]=phonebook;
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
57
The base type for a vector can be

A) int
B) float or double
C) char
D) any data type
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
58
When the extraction operator is used to read data into a string,

A) it skips all white spaces
B) it skips only new lines
C) it reads everything on the line
D) it reads as many characters that will fit into the c-string
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
59
What is the difference between strcmp and strncmp?

A) No difference
B) they both compare, one expects an integer for the number of characters to compare.
C) one copies, the other compares
D) They are in different libraries
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
60
Given the following declarations, which of the following is legal syntax?
String str="Your name";
Char c_string[20]="My name";

A) str = c_string;
B) c_string = str;
C) strcpyc_string, str.c_str));
D) strcpyc_string, str);
E) A and C
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
61
What is the value of numbers.size) after the following code?
Vector numbers;
Numbers.reserve100)

A) 0
B) 10
C) 100
D) unknown
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
62
Given the following code, what is the correct statement to insert the string str2 into str1, directly after the 'd'?
String str1="abcdefg";
String str2="ABCDE";

A) str1.insert4,str2);
B) str2.insert4,str1);
C) insertstr1,4)=str2;
D) insertstr2,4)=str1;
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
63
What is the value of numbers.size) after the following code?
Vector numbers100);

A) 0
B) 10
C) 100
D) unknown
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
64
In a vector, which of the following statements is true?

A) Indexing vector access is range checked.
B) The range of legal index values for a vector is 0 to the value of v.size)-1
C) To add a value use the member function v.push_front )
D) To increase or decrease a vector's size v.new_sizenewSize);
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
65
If the capacity of a vector named names is 20 and the size of names is 19, which of the following statements are legal?

A) names.push_back"myName");
B) names[18]="myNmae";
C) all of the above
D) none of the above
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
66
If a vector named numbers has 20 elements in it, what is the result of executing the following statement?
Numbers.resize10);

A) no change
B) the first 10 elements are removed
C) the last 10 elements are removed
D) this causes a run-time error
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
67
Which of the following are correct statements about vector member functions studied in this chapter?

A) push_front baseTypeObject) puts object on the front of a vector
B) indexing uses index values 0 through size )
C) size ) tells how many base type objects have been inserted into the vector
D) When a vector runs out of space all implementations are required to double the amount of allocated space .
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
68
What is the value of numbers.capacity) after the following code?
Vector numbers;
Numbers.reserve100)

A) 0
B) 10
C) 100
D) unknown
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
69
When a vector is assigned to another vector

A) only the location of the vector is copied
B) all the values in the vector are copied
C) if there is not enough room in the left-hand vector, then not all the values from the right side are copied
D) none of the above
Unlock Deck
Unlock for access to all 69 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 69 flashcards in this deck.