Deck 8: Strings and Vectors
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/63
Play
Full screen (f)
Deck 8: Strings and Vectors
1
Vector assignment is well behaved.
True
2
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;
v[i] = i;
False
3
Using the [i] on a string variable does not check for illegal values of i.
True
4
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 63 flashcards in this deck.
Unlock Deck
k this deck
5
Vectors can have any type as the base type
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
6
The following code declares a vector of characters.
vector characters
vector characters
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
7
All c-strings must be terminated by ________
Unlock Deck
Unlock for access to all 63 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 63 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 63 flashcards in this deck.
Unlock Deck
k this deck
10
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 63 flashcards in this deck.
Unlock Deck
k this deck
11
Using the resize member function alone,you can increase the capacity of an STL vector.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
12
The following declares a c-string variable that will hold 10 letters.
char str[10];
char str[10];
Unlock Deck
Unlock for access to all 63 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 63 flashcards in this deck.
Unlock Deck
k this deck
14
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 63 flashcards in this deck.
Unlock Deck
k this deck
15
Vector indexing warns about out-of-bounds index values.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
16
The following code declares a vector of integers named numbers that reserves space for 100 integers.
vector numbers100);
vector
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
17
The character '\0' is called the __________ character.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
18
Vectors and arrays are the same data type.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
19
A string variable and a c-string are the same data type.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
20
The following declares a c-string and initializes it to "speaker"
char str[]="speaker";
char str[]="speaker";
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
21
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
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 63 flashcards in this deck.
Unlock Deck
k this deck
22
The c-string to number conversion functions are in the _________ library.
Unlock Deck
Unlock for access to all 63 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 63 flashcards in this deck.
Unlock Deck
k this deck
24
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;
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 63 flashcards in this deck.
Unlock Deck
k this deck
25
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 63 flashcards in this deck.
Unlock Deck
k this deck
26
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
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 63 flashcards in this deck.
Unlock Deck
k this deck
27
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
A)a c-string
B)a character array
C)a string
D)none of the above
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
28
What is the c-string function to determine the number of characters in a c-string?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
29
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
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 63 flashcards in this deck.
Unlock Deck
k this deck
30
To compare two c-strings you use the __________ function.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
31
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.
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 63 flashcards in this deck.
Unlock Deck
k this deck
32
To change the size of a vector,one uses the ______________ member function.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
33
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 63 flashcards in this deck.
Unlock Deck
k this deck
34
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 63 flashcards in this deck.
Unlock Deck
k this deck
35
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];
A)char s1;
B)char s1[9];
C)char s1=10;
D)char s1[10];
Unlock Deck
Unlock for access to all 63 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 63 flashcards in this deck.
Unlock Deck
k this deck
37
The declaration of an STL vector doubleVec that can hold values of type double,one writes ______________.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
38
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");
A)str="toaster;
B)str=toaster;
C)strcpystr,"toaster");
D)str.strcpy"toaster");
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
39
____________ can be thought of as an array that can grow and shrink as needed.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
40
What is the difference between strcat and strncat?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
41
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
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 63 flashcards in this deck.
Unlock Deck
k this deck
42
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
String str;
A)a garbage string
B)the empty string
C)the null character
D)unknown
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
43
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
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 63 flashcards in this deck.
Unlock Deck
k this deck
44
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);
A)getlinefin,line);
B)fin.getlineline);
C)fin.getlineline,'\n\');
D)fin.getlineline,80);
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
45
What is the value of numbers.size)after the following code?
Vector numbers100);
A)0
B)10
C)100
D)unknown
Vector
A)0
B)10
C)100
D)unknown
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
46
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
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 63 flashcards in this deck.
Unlock Deck
k this deck
47
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
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 63 flashcards in this deck.
Unlock Deck
k this deck
48
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
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 63 flashcards in this deck.
Unlock Deck
k this deck
49
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
A)str3);
B)str.at3);
C)str[3];
D)All of the above
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
50
What is the value of numbers.capacity)after the following code?
Vector numbers;
Numbers.reserve100)
A)0
B)10
C)100
D)unknown
Vector
Numbers.reserve100)
A)0
B)10
C)100
D)unknown
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
51
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));
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 63 flashcards in this deck.
Unlock Deck
k this deck
52
The notation vector means that the vector is
A)an array
B)a template class
C)primitive data type
D)all of the above
A)an array
B)a template class
C)primitive data type
D)all of the above
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
53
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;
A)strcpystr1,"toaster");
B)str1 = "toaster";
C)str1 = toaster;
D)str1 += toaster;
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
54
The base type for a vector can be
A)int
B)float or double
C)char
D)any data type
A)int
B)float or double
C)char
D)any data type
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
55
What is the value of numbers.size)after the following code?
Vector numbers;
A)0
B)10
C)100
D)unknown
Vector
A)0
B)10
C)100
D)unknown
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
56
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);
A)numbers[numbers.size)+1] = newValue;
B)numbers = newValue;
C)numbers.pushBacknewValue);
D)numbers.push_backnewValue);
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
57
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
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 63 flashcards in this deck.
Unlock Deck
k this deck
58
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
A)vector strings names;
B)vector
C)vector
D)all of the above
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
59
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)
A)str.getLength)
B)str.length)
C)lengthstr)
D)getLengthstr)
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
60
What is the value of numbers.size)after the following code?
Vector numbers;
Numbers.reserve100)
A)0
B)10
C)100
D)unknown
Vector
Numbers.reserve100)
A)0
B)10
C)100
D)unknown
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
61
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);
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 63 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;
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 63 flashcards in this deck.
Unlock Deck
k this deck
63
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 .
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 63 flashcards in this deck.
Unlock Deck
k this deck